This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: `griot fucking ridiculous` | |
function griot() { | |
lynx "http://rapgenius.com/search?q=`echo $@ | perl -p -e 's/\s+/+/g'`"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer