Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Created August 15, 2018 15:52
Show Gist options
  • Save natefaubion/12c41994a7f5059c745fa8eacb23a471 to your computer and use it in GitHub Desktop.
Save natefaubion/12c41994a7f5059c745fa8eacb23a471 to your computer and use it in GitHub Desktop.
data TraverseProp (f :: Type -> Type) k = TraverseProp k
instance traverseProp ::
( Applicative f
, IsSymbol sym
, Row.Lacks sym rb
, Row.Cons sym b rb rc
, Mapping k a (f b)
) =>
FoldingWithIndex
(TraverseProp f k)
(SProxy sym)
(f (Builder { | ra } { | rb }))
a
(f (Builder { | ra } { | rc }))
where
foldingWithIndex (TraverseProp k) prop rin a =
(>>>) <$> rin <*> (Builder.insert prop <$> mapping k a)
traverseRecord :: forall f k rin rout.
Applicative f =>
HFoldlWithIndex (TraverseProp f k) (f (Builder {} {})) { | rin } (f (Builder {} { | rout})) =>
k ->
{ | rin } ->
f { | rout }
traverseRecord k =
map (flip Builder.build {}) <<<
hfoldlWithIndex (TraverseProp k) (pure identity)
test1 =
traverseRecord (Just :: Int -> Maybe Int)
{ a: 1
, b: 2
, c: 3
}
data SequencePropOf (f :: Type -> Type) = SequencePropOf
instance sequencePropOf_1 ::
( Applicative f
, IsSymbol sym
, Row.Lacks sym rb
, Row.Cons sym a rb rc
) =>
FoldingWithIndex
(SequencePropOf f)
(SProxy sym)
(f (Builder { | ra } { | rb }))
(f a)
(f (Builder { | ra } { | rc }))
where
foldingWithIndex _ prop rin a =
(>>>) <$> rin <*> (Builder.insert prop <$> a)
else
instance sequencePropOf_2 ::
( Applicative f
, IsSymbol sym
, Row.Lacks sym rb
, Row.Cons sym x rb rc
) =>
FoldingWithIndex
(SequencePropOf f)
(SProxy sym)
(f (Builder { | ra } { | rb }))
x
(f (Builder { | ra } { | rc }))
where
foldingWithIndex _ prop rin x =
(_ >>> Builder.insert prop x) <$> rin
sequencePropsOf :: forall f rin rout.
Applicative f =>
HFoldlWithIndex (SequencePropOf f) (f (Builder {} {})) { | rin } (f (Builder {} { | rout })) =>
{ | rin } ->
f { | rout }
sequencePropsOf =
map (flip Builder.build {}) <<< hfoldlWithIndex SequencePropOf (pure identity)
test :: Maybe _
test =
sequencePropsOf
{ a: Just "Hello"
, b: Nothing
, c: 42
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment