Skip to content

Instantly share code, notes, and snippets.

View kjagiello's full-sized avatar

Krzysztof Jagiełło kjagiello

View GitHub Profile
@kjagiello
kjagiello / exceptional.py
Last active August 29, 2015 14:25
Making your life exceptionally easier
from functools import wraps
class NoExceptionRaisedError(Exception):
pass
def exceptional(func):
@wraps(func)
def wrapper(*args, **kwargs):
fun superExplode (delimiter, s) =
let
fun takeUntil (delimiter, []) = []
| takeUntil (delimiter, first::rest) =
if first = delimiter then
[]
else
first::(takeUntil (delimiter, rest))
fun superExplode' (delimiter, []) = []
fun removeElement ([], _) = []
| removeElement (first::rest, x) =
if first = x then
rest
else
first::removeElement (rest, x);
local
fun rmod x y = x - y * Real.realFloor (x / y);
val a = 16807.0;
val m = 2147483647.0;
val random_seed = ref 1.0;
in
fun random () =
let
val r = rmod (a * ! random_seed) m
in
fun orbList l =
foldr Word32.orb (Word32.fromInt 0) l
fun bin2word x =
let
fun bin2word' ("", _) = []
| bin2word' (s, i) =
let
val b = String.sub (s, 0)
val v = case b of
local
fun rmod x y = x - y * Real.realFloor (x / y);
val a = 16807.0;
val m = 2147483647.0;
val random_seed = ref 1.0;
in
fun random () =
let
val r = rmod (a * ! random_seed) m
in
fun measureTime f =
let
val t = Timer.startRealTimer ()
in
(f (), Timer.checkRealTimer t)
end
fun readAll (h, acc) =
let
val s = TextIO.inputLine h
@kjagiello
kjagiello / qt.sml
Last active December 11, 2015 21:48
(* REPRESENTATION CONVENTION: (left, top, right, bottom)
REPRESENTATION INVARIANT: left < right andalso bottom < top
*)
datatype rectangle = Rect of int * int * int * int;
(* REPRESENTATION CONVENTION: (extent, horizontal, vertical,
topLeft, topRight, bottomLeft, bottomRight)
REPRESENTATION INVARIANT:
*)
datatype quadTree = EmptyQuadTree
fun translate(Rect(l1, t1, r1, b1), Rect(l2, t2, r2, b2)) =
Rect(abs(l1 - l2), abs(t1 - t2), abs(r1 - r2), abs(b1 - b2))
fun calculateRelativePosition(rt as Rect(l, t, r, b), offset) =
let
val width = r - l
val height = t - b
val Rect(x, y, _, _) = translate(rt, offset)
val cx1 = Int.toString(width div 2 + x)
val cx2 = Int.toString(height + y)