Skip to content

Instantly share code, notes, and snippets.

@acple acple/RowTraversable.purs
Last active Aug 31, 2018

Embed
What would you like to do?
module Main where
import Prelude
import Control.Parallel (class Parallel, parallel, sequential)
import Data.Maybe (Maybe(..))
import Data.Symbol (class IsSymbol, SProxy(..))
import Effect (Effect)
import Effect.Class.Console (logShow)
import Prim.Row as Row
import Prim.RowList as RL
import Record as Record
import Type.Data.RowList (RLProxy(..))
----------------------------------------------------------------
main :: Effect Unit
main = do
logShow $ sequenceRow { x: Just 123, y: Just "abc" }
logShow $ sequenceRow { x: Just 123, y: Nothing :: Maybe String }
logShow $ sequenceRow { x: [1, 2, 3], y: ["a", "b"] }
----------------------------------------------------------------
class RowTraversable list xs ys m f | list -> ys m where
traverseRow :: (RLProxy list) -> (m ~> f) -> { | xs } -> f { | ys }
instance rowTraversableNil :: Applicative f => RowTraversable RL.Nil xs () m f where
traverseRow _ _ _ = pure {}
instance rowTraversableCons ::
( IsSymbol key
, Row.Cons key (m a) xs' xs
, Row.Cons key a ys' ys
, Row.Lacks key ys'
, RowTraversable tail xs ys' m f
, Applicative f
) => RowTraversable (RL.Cons key (m a) tail) xs ys m f where
traverseRow _ f obj = Record.insert key <$> f (Record.get key obj) <*> traverseRow tail f obj
where
key = SProxy :: SProxy key
tail = RLProxy :: RLProxy tail
sequenceRow
:: forall rowList mr r m
. RL.RowToList mr rowList
=> RowTraversable rowList mr r m m
=> { | mr } -> m { | r }
sequenceRow = traverseRow (RLProxy :: RLProxy rowList) identity
props
:: forall rowList mr r m f
. RL.RowToList mr rowList
=> RowTraversable rowList mr r m f
=> Parallel f m
=> { | mr } -> m { | r }
props = sequential <<< traverseRow (RLProxy :: RLProxy rowList) parallel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.