Skip to content

Instantly share code, notes, and snippets.

@missingfaktor
Last active May 31, 2016 23:42
Show Gist options
  • Select an option

  • Save missingfaktor/d2fbb0ff371f903ef00ca9b0515b92f4 to your computer and use it in GitHub Desktop.

Select an option

Save missingfaktor/d2fbb0ff371f903ef00ca9b0515b92f4 to your computer and use it in GitHub Desktop.
Prelude> newtype Dyn a = Dyn a deriving Show
Prelude> data Book = Book (Dyn String) Int deriving Show
Prelude> :{
Prelude| class Serialise a where
Prelude| serialise :: a -> String
Prelude| :}
Prelude> instance Serialise Book where serialise (Book (Dyn s) i) = s ++ ":" ++ show i
Prelude>
Prelude> let serialisedBook = fmap serialise (\s -> Book s 32)
Prelude> -- apply dynamic value when available
Prelude> serialisedBook (Dyn "isbn")
"isbn:32"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment