| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Prairie.Traverse
Description
Synopsis
- traverseRecord :: forall rec f. (Record rec, Applicative f) => (forall ty. ty -> Field rec ty -> f ty) -> rec -> f rec
Documentation
traverseRecord :: forall rec f. (Record rec, Applicative f) => (forall ty. ty -> Field rec ty -> f ty) -> rec -> f rec Source #
Apply an effectful function over each field of a Record, producing
a new Record in the process.
This example use increments a Users age and requests a new name.
happyBirthday :: User -> IO User
happyBirthday =
traverseRecord
(\val field ->
case field of
UserName -> do
putStrLn $ "Current name is: " <> val
putStrLn "Please input a new name: "
getLine
UserAge -> do
putStrLn $ "Current age is " <> show val
pure (val + 1)
)
If you only want to target a single field, you can use a wildcard match
and pure. This example also uses LambdaCase.
nameAtLeastOneCharacter :: User ->MaybeUser nameAtLeastOneCharacter =traverseRecord(val -> case UserName -> doguard(lengthval >= 1)pureval _ ->pureval )
Since: 0.0.3.0