Skip to content

Instantly share code, notes, and snippets.

@poetix
poetix / example.java
Last active December 21, 2015 05:49
Lenses and magic records with spinoza / hume
package com.codepoetics.hume;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
import com.codepoetics.hume.api.Property;
import com.codepoetics.hume.api.Record;
import com.codepoetics.spinoza.Compose;
@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
@poetix
poetix / pyramid.idr
Last active October 26, 2017 22:07
module Main
forLoop : List a -> (a -> IO ()) -> IO ()
forLoop [] f = return ()
forLoop (x :: xs) f = do f x
forLoop xs f
syntax for {x} "in" [xs] ":" [body] = forLoop xs (\x => body)
spaces : Nat -> String
@poetix
poetix / Nonchalantly.java
Last active December 21, 2017 17:11
A Java 8 class for when you just don't give a fuck about that checked exception
public interface Nonchalantly<T> {
static <T, E extends Throwable> T invoke(Nonchalantly<T> f) throws E {
try {
return f.run();
} catch (Throwable e) {
throw (E) e;
}
}
T run() throws Throwable;
@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) {
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];
import java.util.function.*;
public final class Tunnel<E extends Exception> {
public static <E extends Exception, R> R call(Class<? extends E> exceptionClass, Function<Tunnel<E>, R> f) throws E {
try {
return f.apply(new Tunnel<>());
} catch (TunnelledException e) {
throw exceptionClass.cast(e.getCause());
}
import qualified Data.Map as Map
type SubPaths = Map.Map String Node
data Node = Content SubPaths | NoContent SubPaths deriving (Show)
content :: [(String, Node)] -> Node
content kvs = Content $ Map.fromList kvs
nocontent :: [(String, Node)] -> Node
nocontent kvs = NoContent $ Map.fromList kvs
@poetix
poetix / Table.kt
Last active June 2, 2016 09:51
SQL -> Data Class mapping without reflection
package com.codepoetics.kontinuous
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
data class Foo(val a: String, val b: Int, val c: Double)
interface RowN {
val selectSql: String
@poetix
poetix / Poly.purs
Last active July 4, 2017 16:35
Find and print all 35 Hexominoes
module Main where
import Prelude
import Control.Plus (empty)
import Data.Array as A
import Data.Set as S
import Control.Monad.Eff.Console (log)
import Data.Foldable (minimum, maximum)
import Data.Maybe (fromMaybe)
import Data.String (joinWith, fromCharArray)