Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
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 User
s 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 ->Maybe
User nameAtLeastOneCharacter =traverseRecord
(val -> case UserName -> doguard
(length
val >= 1)pure
val _ ->pure
val )
Since: 0.0.3.0