Skip to content

Instantly share code, notes, and snippets.

@parsonsmatt
parsonsmatt / record-monoid.md
Last active May 7, 2017 19:03
Which default monoid instance would you want?

I'm writing an extensible records library for fun and profit. I have two potential 'Monoid' instances I can give my 'HashRecord' type. Which would you prefer to see?

The record type is essentially HashRecord f xs, where xs is a type level list of (key :: Symbol) =: (value :: *) pairs, and f is a type constructor that each entry in the record is contained in.

Option 1

Delegate to the Monoid instance for the values.

instance ToSchema (Book' '[]) where
declareNamedSchema _ =
pure . NamedSchema Nothing $ mempty
& type_ .~ SwaggerObject
instance (KnownSymbol k, ToSchema v, ToSchema (Book' xs)) => ToSchema (Book' ( k :=> v ': xs )) where
declareNamedSchema _ = do
valRef <- declareSchemaRef (Proxy @v)
let key = Text.pack (symbolVal (Proxy @k))
rest = toNamedSchema (Proxy @(Book' xs))
@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]
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 / 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
@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 / 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 / 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
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Lensy where
import Data.Functor.Identity
import Control.Lens
data Api f
@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)]