Skip to content

Instantly share code, notes, and snippets.

/**
* gets a record out of database by ID
*
* infers the type to use based on return
*
* RentalImpl r = Util.getById(10);
*
* @param c The implementation class name
* @param id the id of the instance you want
*/
@parsonsmatt
parsonsmatt / springer-free-maths-books.md
Created December 28, 2015 14:41 — forked from bishboria/springer-free-maths-books.md
Springer have made a bunch of maths books available for free, here are the direct links
@parsonsmatt
parsonsmatt / props.py
Created March 15, 2016 04:31
mmm property checking
def decode(data):
"""
Accepts a byte array and converts it into an Ack packet if possible. If the
decoding fails, then this method raises DecodeError. The following doc test
randomly samples legal values for the acknowledgment number and window size and
asserts that for all those values, Ack.decode(p.encode()) == p
>>> from random import *
>>> ack_nos = [randrange(int(1e6)) for i in range(1,10)]
>>> win_sizes = [randrange(int(1e3)) for i in range(1,10)]
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Lensy where
import Data.Functor.Identity
import Control.Lens
data Api f
@parsonsmatt
parsonsmatt / mtl.hs
Last active October 27, 2017 23:20
`mtl` style enables reinterpretation of a monad, like `free`
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
module Mtl where
import Control.Monad.State
import Control.Monad.Except
runMtl
:: Bool
@parsonsmatt
parsonsmatt / jobs.hs
Last active November 22, 2016 16:35
Multithreaded job system
module Jerbs where
import Control.Concurrent (forkIO, killThread)
import Control.Concurrent.STM (TQueue, atomically, newTQueueIO,
readTQueue, writeTQueue)
import Control.Exception (SomeException (..), try)
import Control.Monad (forever)
import Data.Foldable (for_)
import Data.Traversable (for)
@parsonsmatt
parsonsmatt / fails
Last active November 25, 2016 19:15
msgpack failing tests
Data.MsgPack
✗ that galois connection tho:
(Right Test.Main.TestVal {bar: 480996, baz: [0.5786002867755481], foo: "뎬�⸨�쭊"}) /= (Right Test.Main.TestVal {bar: 480996, baz: [0.5786002867755481], foo: "뎬�⸨�쭊"})
(Right Test.Main.TestVal {bar: -463187, baz: [0.8810286139515362, 0.7951747639082255], foo: "�"}) /= (Right Test.Main.TestVal {bar: -463187, baz: [0.8810286139515362, 0.7951747639082255], foo: "�"})
(Right Test.Main.TestVal {bar: 431163, baz: [], foo: "뒌閪�襪㎜"}) /= (Right Test.Main.TestVal {bar: 431163, baz: [], foo: "뒌閪�襪㎜"})
(Right Test.Main.TestVal {bar: 431939, baz: [0.3411815847927619, 0.7783211696792027, 0.019137335484445718], foo: "�"}) /= (Right Test.Main.TestVal {bar: 431939, baz: [0.3411815847927619, 0.7783211696792027, 0.019137335484445718], foo: "�"})
(Right Test.Main.TestVal {bar: -683034, baz: [0.09981450722544198, 0.28433427553825746, 0.6274012209043843, 0.41340820370866366, 0.8485718839096705, 0.6971856512581863], foo: "읤�"}) /= (Right Test.Main.TestVal {bar: -683034, baz
@parsonsmatt
parsonsmatt / tyfam.hs
Last active December 11, 2016 21:51
type families
{-# language TypeFamilies, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, DataKinds, TypeOperators, GADTs #-}
module TyFam where
-- A type family is a function on types.
type family Element f
type instance Element [a] = a
instance MonoFunctor [a] where
type Join a b = Map (Key a) (Entity a, Collection b)
type Collection a = Map (Key a) (Entity a)
innerJoin
:: ( PersistEntity val1
, PersistEntity val2
, PersistField typ
, PersistEntityBackend val1 ~ SqlBackend
, PersistEntityBackend val2 ~ SqlBackend
@parsonsmatt
parsonsmatt / Beer.hs
Last active March 7, 2017 06:28
26 Bottles of Functional Programming
module Beer where
import Data.Char (toUpper)
main = putStrLn (song [100, 99 .. 0])
song = unlines . map verse
verse x = unlines [firstLine x, secondLine x]