Skip to content

Instantly share code, notes, and snippets.

@poetix
poetix / slothful.js
Created February 23, 2011 17:46
Lazy streams in Javascript
'use strict';
var slothful = (function() {
function _(step) {
var getResult = function() { return step(); };
return function() {
var result = getResult();
getResult = function() { return result; }
return result;
@poetix
poetix / combinators.js
Created March 10, 2011 10:44
Javascript Combinator examples
"use strict";
// compose :: (b -> c) -> (a -> b) -> (a -> c)
function compose(g, f) {
return function() {
return g(f.apply(f, arguments));
};
}
// each :: [a] -> (a -> _) -> _
@poetix
poetix / poker.clj
Created November 17, 2011 18:14
Checking for various poker hand combinations
1 (ns poker.core
2 (:use clojure.contrib.string))
3
4 (defrecord Card [value suit])
5
6 (def VALUE {
7 "1" :one
8 "2" :two
9 "3" :three
10 "4" :four
@poetix
poetix / index.html
Created December 25, 2011 20:29
HTML5 canvas particle demo
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="scanva.js"></script>
<script src="particles.js"></script>
</head>
<body>
<canvas width="500px" height="500px" id="canvas"></canvas>
</body>
@poetix
poetix / iterators.js
Created December 27, 2011 22:28
Lazy map, filter and reduce in Javascript
Iterators = (function() {
var each, map, filter, reduce, toArray, toObject;
function _map(iter, f) {
return {
hasNext: iter.hasNext,
next: function() { return f(iter.next()); }
};
}
@poetix
poetix / RangeSetsTest.java
Created January 5, 2012 15:03
Range Sets test
public class RangeSetsTest {
@Test public void
an_empty_list_of_ranges_is_coalesced_to_an_empty_set() {
Collection<Range<Integer>> ranges = newArrayList();
assertThat(RangeSets.containing(ranges).size(), Matchers.is(0));
}
@SuppressWarnings("unchecked")
@poetix
poetix / json_to_csv.py
Created August 29, 2012 09:58
Short script to fetch JSON from MongoDB, flatten to dictionary of leaf nodes, and write everything out in CSV format
#!/usr/bin/python
import urllib2
import argparse
import json
parser = argparse.ArgumentParser(description="Fetch some JSON")
parser.add_argument("url")
args = parser.parse_args()
@poetix
poetix / RecorderSpec.scala
Created January 14, 2013 17:20
A monad which captures dynamic method invocations against multiple targets (for use in implementing object builders / matchers)
package com.youdevise.lofty
import org.specs2.mutable._
import scala.language.dynamics
trait State[S, A] {
val runState: Function[S, (S, A)]
def flatMap[B](f: A => Function[S, (S, B)]):Function[S, (S, B)] = (state:S) => {
val (newState, value) = runState(state)
f(value)(newState)
@poetix
poetix / builder.scala
Created January 15, 2013 12:26
Dynamic / Monadic object builder round 2
package com.youdevise.lofty
import org.specs2.mutable._
import scala.language.dynamics
import scala.language.reflectiveCalls
import scalaz._
import Scalaz._
import scala.reflect.runtime.universe._
trait Builder[T] {
@poetix
poetix / HasThePairsWorksheet.scala
Last active December 16, 2015 13:39
Demonstrating a Map matcher with improved failure reporting
package com.youdevise.matchless
import org.specs2.matcher.MustMatchers._
import com.youdevise.matchless.Collections._
object HasThePairsWorksheet {
val testMap = Map(
"a" -> "Apple",
"b" -> "Banana",
"c" -> "Carrot"