Skip to content

Instantly share code, notes, and snippets.

@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\"\"\""
module Domain.SomeTitle exposing (..)
--
import Json.Decode as Decode
exposing
( succeed
, fail
, map
, maybe
@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"
}
@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 / Data.swift
Last active April 15, 2022 03:47
Reading InputStream into Data
extension Data {
/**
Consumes the specified input stream, creating a new Data object
with its content.
- Parameter reading: The input stream to read data from.
- Note: Closes the specified stream.
*/
init(reading input: InputStream) {
self.init()
input.open()
@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 / 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 / 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 / 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 / def-comments-csSE.json
Last active September 4, 2018 05:16
Default comments for cs.SE for remote use with AutoReviewComments. They reflect the ones available at meta.cs.stackexchange.com/q/627
callback([
{ "name": "[Q] Problem dump",
"description": "We discourage posts that simply state a problem out of context, and expect the community to solve it. Assuming you tried to solve it yourself and got stuck, it may be helpful if you wrote your thoughts and what you could not figure out. It will definitely draw more answers to your post. Until then, the question will be voted to be closed / downvoted. You may also want to check out [these hints](http://meta.cs.stackexchange.com/q/1284/98), or use the search engine of this site to find similar questions that were already answered."
},
{ "name": "[Q] No effort shown, no specific question",
"description": "What have you tried? Where did you get stuck? We do not want to just hand you the solution; we want you to gain understanding. However, as it is we do not know what your underlying problem is, so we can not begin to help. See [here](http://meta.cs.stackexchange.com/q/1284/98) for tips on asking questions about exercise problems. If you are uncertain