Skip to content

Instantly share code, notes, and snippets.

@poetix
poetix / twentyfourtyeight.py
Last active August 29, 2015 13:57
2048 player
import random
def memoize(f):
cache = {}
def memoized(*arg):
if arg in cache:
return cache[arg]
result = f(*arg)
cache[arg] = result
return result
public class RotationalHash {
public static int hash(String beads) {
byte[] buf = beads.getBytes();
int hash = hashBytes(buf);
int i = 0;
int j = buf.length - 1;
while (i < j) {
buf[i] ^= buf[j];
buf[j] ^= buf[i];
@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 / 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 / Judiciously.java
Last active November 9, 2015 21:48
A useful Java 8 class for when you want to defer giving a fuck about that checked exception until it's convenient to do so.
@FunctionalInterface
public interface Judiciously<T> {
class WrappedException extends RuntimeException {
public WrappedException(Throwable cause) {
super(cause);
}
}
static <T> T attempt(Judiciously<T> f) {
@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"