Skip to content

Instantly share code, notes, and snippets.

View john-kurkowski's full-sized avatar

John Kurkowski john-kurkowski

View GitHub Profile
@john-kurkowski
john-kurkowski / gist:03efc9860a2a0e463085
Created June 3, 2014 21:00
(╯°□°)╯︵ ┻━┻.scala
➜ scala
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> case object Table extends Exception
defined module Table
scala> def ╯°□°╯┻┻ = throw Table
@john-kurkowski
john-kurkowski / psort.sh
Created March 23, 2011 06:23
Parallel Unix `sort` comparison
#!/bin/bash
# Compare Mac OS X 10.6's `sort` to latest GNU Coreutils `sort` which parallelizes by default.
# More info: http://www.cs.ucla.edu/classes/fall10/cs35L/assign/assign9.html
integers=10000000
file=`eval echo "~/Documents/integers1.txt"`
echo "Generating $integers integers..."
if [ ! -f $file ]; then
for i in $(eval echo "{1..$integers}"); do
scala> val m = Map(1 -> 2, 3 -> 4, 5 -> 6)
map: scala.collection.immutable.Map[Int,Int] = Map((1,2), (3,4), (5,6))
scala> val desiredKeys = Iterable(1, 2, 3)
desiredKeys: Iterable[Int] = List(1, 2, 3)
// Get several values out of a Map, or a default.
scala> desiredKeys map (m.getOrElse(_, -1))
res0: Iterable[Int] = List(2, -1, 4)
@john-kurkowski
john-kurkowski / ListBufferSizeVsLength.scala
Created January 4, 2012 19:29
In addition to Scala's Array.size gotcha, prefer .length to .size with ListBuffers
def timeit(fn: () => Any, times: Int = 100000) {
val start = System.currentTimeMillis
for (_ <- 0 until times) {
fn()
}
val end = System.currentTimeMillis
println((end - start) + "ms")
}
val n = 10000
@john-kurkowski
john-kurkowski / gist:1721480
Created February 2, 2012 04:27
org.semver.Version 0.9.11 weirdness
import org.semver.Version
val li = List(Version.parse("2.0.34"), Version.parse("1.1.0"), Version.parse("1.0.1"), Version.parse("34.1"))
// li: List[org.semver.Version] = List(2.0.4, 1.1.0, 1.0.1, 4.1.0) // HUH?
println(li.sorted)
// List(4.1.0, 2.0.4, 1.1.0, 1.0.1) // reversed!
@john-kurkowski
john-kurkowski / try-times.clj
Created August 26, 2012 23:32
Retry on exception, returning either accumulated failures or first successful value. Based on http://stackoverflow.com/questions/1879885/clojure-how-to-to-recur-upon-exception
(defn try-times-wait-millis* [times wait-millis thunk]
(let [attempts (repeatedly times
#(try (thunk)
(catch Exception ex (do
(Thread/sleep wait-millis)
{::failure ex}))))
successes (drop-while #(contains? % ::failure) attempts)]
(if (not (empty? successes))
{:success (first successes)}
class SlidingWindowMap(keys: Set[String], maxCount: Int, periodMs: Int) {
val times = new collection.mutable.HashMap[String, Vector[Long]]() with collection.mutable.SynchronizedMap[String, Vector[Long]]
times ++= keys.map(k => (k, Vector[Long]()))
def nextKey: Option[String] = {
val now = System.currentTimeMillis
times.synchronized {
val trimmedTimes = times.toStream map { case (k, usage) =>
val trimmedUsage = usage dropWhile (_ < now - periodMs)
@john-kurkowski
john-kurkowski / Fault-Tolerance.md
Last active October 22, 2015 01:17 — forked from pixelhandler/Fault-Tolerance.md
Fault Tolerance: Software is ruining the human experience

Fault Tolerance: Software is Ruining the Human Experience

Mostly a rant on a few bad experiences that could have been prevented in real life if engineers did a good job instead of a good enough job. We have no oath to "Do no harm!" So, we take license to ship whatever. This is bad for humans.

Intro: Fault Tolerance and Bad Human Experiences

Pumping Up the Tires

  1. Wow look new technology, pumping up tires is way better now
  2. Not so much, they added software to the process, oops
#!/usr/bin/env python
'''Convert tags with no leading "v" to NPM's default tag format.'''
import re
import subprocess
OLD_TAG_RE = re.compile(r'^\d+\.\d+')
{
"basePath": "/v2",
"definitions": {
"ApiResponse": {
"properties": {
"code": {
"format": "int32",
"type": "integer"
},
"message": {