Skip to content

Instantly share code, notes, and snippets.

import scala.collection.mutable.{Set => MutableSet}
import scala.io.Source
import java.security.MessageDigest
class BloomSet(numHashes: Int) {
protected val hashes = MutableSet.empty[String]
def add(word: String) {
hashes ++= digest(word)
import scala.collection.mutable.{Map => MutableMap}
import scala.io.Source
class AnagramMap {
protected val anagrams = MutableMap.empty[String, Set[String]]
def add(word: String) {
val normalised = normalise(word.toLowerCase)
val equivalents = anagrams.getOrElseUpdate(normalised, Set.empty[String])
import scala.io.Source
val words = for {
line <- Source.fromFile("/usr/share/dict/words").getLines
word = line.trim.toLowerCase
if word.length <= 6
} yield word
val sixLetterWords = words filter { _.length == 6 }
val candidateParts = words filter { _.length < 6 }
def fib_gen(first=1, second=2):
"Infinite Fibonacci generator"
while True:
next, first, second = first, second, first+second
yield next
@bmjames
bmjames / skicalc.erl
Created December 17, 2010 17:11
Evaluator for SKI combinator calculus trees
-module(skicalc).
-author("Ben James <benj@lshift.net>").
-export([eval/1, conv/1]).
% Valid terms to be evaluated here are binary trees represented as
% two-element tuples. Leaves can be anything, and the atoms s, k and i
% represent the symbols S, K and I in the calculus' alphabet.
% For example, an operator to reverse two elements can be defined as:
@bmjames
bmjames / skicalc.prolog
Created January 3, 2011 18:36
Prolog version of the SKI calculus evaluator
%% -*-mode: prolog-*-
%% Example of the reversal operator:
%% ?- R = [[s, [k, [s, i]]], k],
%% eval([[R, a], b], Result).
%%
%% R = [[s,[k,[s,i]]],k]
%% Result = [b,a] ?
%% eval delegates to eval1 to perform the transformations of the calculus,
@bmjames
bmjames / gevent_zmq_pubsub.py
Created April 17, 2011 23:45
Async ZeroMQ example using gevent_zeromq
import gevent
from gevent_zeromq import zmq
context = zmq.Context()
def sub():
subscriber = context.socket(zmq.SUB)
subscriber.connect('tcp://localhost:5561')
subscriber.setsockopt(zmq.SUBSCRIBE, '')
while True:
#!/usr/bin/env bash
if ([ -z "$1" ] || [ -z "$2" ]); then
echo "usage: $0 <alias> <branch>"
exit
fi
CMD="git symbolic-ref refs/heads/$1 refs/heads/$2"
echo $CMD
$CMD
@bmjames
bmjames / gist:1557596
Created January 3, 2012 23:39
totally non-complex way to list files in a directory
val openFile: String => IO[File]
= path => io { new File(path) }
val fileName: File => String
= _.toString
val putStrLn: String => IO[Unit]
= s => io { println(s) }
val listFiles: File => IO[Seq[File]]
@bmjames
bmjames / gist:2006228
Created March 9, 2012 11:51
Scalaz Logger monad example
type StringLogger[A] = Logger[String, A]
def startWith(i: Int): StringLogger[Int] = i.logger :+-> "Started with " + i
def thenAdd(j: Int)(i: Int): StringLogger[Int] = (i + j).logger :+-> "Added " + j
def thenMultBy(j: Int)(i: Int): StringLogger[Int] = (i * j).logger :+-> "Multiplied by " + j
val loggedResult = startWith(1) >>= thenAdd(4) >>= thenMultBy(5)