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 / 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 / StreamOuter.scala
Created August 15, 2011 17:41
Scala closures capture outer variables, which can lead to unhappy serialization when you don't expect a lazy data structure under the covers. The lesson is, for serialization, favor strict, concrete types.
class A
class B extends Serializable
val baos = new java.io.ByteArrayOutputStream(1024)
val oos = new java.io.ObjectOutputStream(baos)
val streamSurprise: Seq[A] = Stream.fill(3)(new A) // say you don't know it's a Stream under the covers
val transformation = streamSurprise map (a => new B)
oos.writeObject(transformation) // fails due to NotSerializableException: A
@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 / lyrics.sh
Created March 22, 2012 19:46
Search lyrics without leaving the terminal
# Usage: `griot fucking ridiculous`
function griot() {
lynx "http://rapgenius.com/search?q=`echo $@ | perl -p -e 's/\s+/+/g'`";
}
@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 / ForwardingFieldDescriptor.scala
Created February 19, 2013 02:38
Scalatra FieldDescriptor that shows .value and .validation as empty for optional, default-less fields. Useful when you want to distinguish these fields being provided or not, and when they have no convenient DefaultValue (zero).
package org.scalatra.commands
import org.scalatra.DefaultValue
import org.scalatra.util.conversion.TypeConverter
abstract class ForwardingFieldDescriptor[T](delegate: FieldDescriptor[T]) extends FieldDescriptor[T] {
protected def copy(newDelegate: FieldDescriptor[T]): ForwardingFieldDescriptor[T]
override def name = delegate.name
@john-kurkowski
john-kurkowski / V01.scala
Last active December 15, 2015 13:09
Accumulating More Than One Failure In A ValidationNEL
def evenNumber(n: Int) = if (n % 2 == 0) Some(n) else None
// an example for-comprehension …
for {
num <- List(1, 2, 3)
evenNum <- evenNumber(num)
} yield evenNum
// … which desugars to …
List(1, 2, 3) flatMap (evenNumber)