Created
May 21, 2015 04:30
-
-
Save dtchepak/f8aa21520f5e6252b848 to your computer and use it in GitHub Desktop.
Reader to construct a function that reads values from a single value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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