Skip to content

Instantly share code, notes, and snippets.

@reitzig
reitzig / sebibtex.tex
Last active September 17, 2015 13:43
A small MWE for testing SE BibTeX export with biblatex+biber vs bibtex+natbib
\documentclass{article}
\def\testmode{old}
\def\testmode{new}
\usepackage{xifthen,url}
\newcommand{\ifnew}[2]{\ifthenelse{\equal{\testmode}{new}}{#1}{#2}}
\ifnew{%
\usepackage[backend=biber]{biblatex}
@reitzig
reitzig / math4slack.js
Created November 17, 2015 10:27
MathJax for Slack -- Attempt 1
( function () { // start of anonymous wrapper function (needed to restrict variable scope on Opera)
// Opera does not support @match, so re-check that we're on SE chat before doing anything
// if ( location.hostname != 'slack.com' ) return;
// Baseline MathJax URL and config, copied from SE:
var mathJaxURL = "//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full";
var config = {
"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: 50 },
tex2jax: { inlineMath: [ ["$", "$"], ["\\\\(","\\\\)"] ], displayMath: [ ["$$","$$"], ["\\[", "\\]"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno" },
@reitzig
reitzig / btTraverse.scala
Last active February 26, 2016 15:15
In-order binary tree traversal in Scala
object Main extends App {
def foreach[B, A >: B](f : A => Unit)(tree : BT[B]) {
tree match {
case Leaf(v) => f(v)
case Inner(v,l,r) => {
foreach(f)()lgis
f(v)
foreach(f)(r)
}
}
@reitzig
reitzig / weighted_rank.scala
Created March 10, 2016 15:27
Weighted ranking of athletes
// cf http://cs.stackexchange.com/q/54253/98
// Runnable version: http://ideone.com/4GyKeB
class Athlete(val name: String, val endurance : Int, val speed : Int) {
def performance(p : Double) = {
p * endurance / Athlete.maxEndurance + (1-p) * speed / Athlete.maxSpeed
}
override def toString = name
}
@reitzig
reitzig / CwlCountdownLatch.swift
Last active February 28, 2017 07:40
A countdown latch based ob CwlUtils primitives
//
// CwlCountdownLatch.swift
// CwlUtils
//
// Created by Raphael Reitzig on 2017/02/16.
// Copyright © 2017 Raphael Reitzig. All rights reserved.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
@reitzig
reitzig / ConstructorMagic.swift
Last active March 1, 2017 10:47
Constructing new values in a public function using initializers from an internal protocol
// Using Swift 3.0.1
// see also http://stackoverflow.com/q/42514600/539599
import Foundation
public protocol P {
var foo: Int { get }
}
internal protocol Q {
@reitzig
reitzig / StringCutter.swift
Last active April 7, 2017 12:54
String prefix and suffix for Swift 3.0
import Foundation
extension String {
/**
Drops characters from the front and the end of this string, returning
what remains.
- Parameter first: The number of characters to drop from the beginning.
Values smaller than 0 are treated as 0.
- Parameter last: The number of characters to drop from the end.
@reitzig
reitzig / convert.rb
Created April 15, 2017 09:05
Default comments for cs.SE for import into AutoReviewComments. Obtained from https://gist.github.com/reitzig/7ca05408f53292505294 using the script attached below.
#!/usr/bin/ruby
require 'json'
# Note: remove `callback(` and the matching `)` from the jsonp file; it should only contain a JSON array.
File.open(ARGV[0]) { |f|
comments = JSON.parse(f.read)
puts comments.map { |c|
"####{c["name"]}\n#{c["description"]}\n\n"
}
module Domain.SomeTitle exposing (..)
--
import Json.Decode as Decode
exposing
( succeed
, fail
, map
, maybe
@reitzig
reitzig / swift-mlsl.rb
Created February 6, 2018 13:13
Transforms a (long) string into a Swift multi-line string literal
#!/usr/bin/ruby
if ARGV.count < 2
puts "Usage: swift-mlsl.rb line-length string"
Process.exit(0)
end
blocksize = ARGV[0].to_i
parts = ARGV[1].scan(/.{1,#{blocksize}}/)
puts "\"\"\"\n" + parts.join("\\\n") + "\n\"\"\""