This file contains hidden or 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 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) |
This file contains hidden or 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 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]) |
This file contains hidden or 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 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 } |
This file contains hidden or 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 fib_gen(first=1, second=2): | |
"Infinite Fibonacci generator" | |
while True: | |
next, first, second = first, second, first+second | |
yield next |
This file contains hidden or 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
-module(skicalc). | |
-author("Ben James <[email protected]>"). | |
-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: |
This file contains hidden or 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
%% -*-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, |
This file contains hidden or 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 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: |
This file contains hidden or 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
#!/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 |
This file contains hidden or 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
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]] |
This file contains hidden or 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
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) |
OlderNewer