Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Created May 21, 2015 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtchepak/f8aa21520f5e6252b848 to your computer and use it in GitHub Desktop.
Save dtchepak/f8aa21520f5e6252b848 to your computer and use it in GitHub Desktop.
Reader to construct a function that reads values from a single value
-- Some function that takes a number and a string
ghci> let f a b = show (a+1) ++ " " ++ b
ghci> :t f
f :: (Show a, Num a) => a -> [Char] -> [Char]
-- Some type with a number of fields we want to read
ghci> data Option = Option { number :: Int, label :: String } deriving (Show, Eq)
ghci> let o = Option 1 "hi"
-- Read value of each field from Option and pass to `f`
ghci> f (number o) (label o)
"2 hi"
-- Use applicative instance for `(->) t` to construct a function that takes a single Object value
-- and reads from that to get the value for each argument.
ghci> import Control.Applicative
ghci> :t f <$> number <*> label
f <$> number <*> label :: Option -> [Char]
ghci> f <$> number <*> label $ o
"2 hi"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment