Skip to content
Discussions/App Development/flatMapA in DAML?Forum ↗

flatMapA in DAML?

App Development4 posts380 views14 likesLast activity Jun 2020
DL
dliakakosOP
Jun 2020

:wave: is there a flatMapA function in daml? (ie how can I go from Update [[ContractId Foo]] to Update [ContractId Foo] )

GA
Gary_Verhaegen
Jun 2020
f : Update [[ContractId Asset]] -> Update [ContractId Asset]
f = fmap concat

seems to compile.

SH
shaynefletcher
Jun 2020

Depending on the context, a do notation version might suit you:

f : Update [[Int]] -> Update[Int]
f ls = do
  ls <- ls
  pure (concat ls)

If you want to get cryptic, there are various ways of expressing this operation in terms of monadic binds f = (=<<) (pure . concat) being an example.

ST
Stephen
Jun 2020

Where m = Update and n = [] and a = b = ContractId Foo, which signature do you think would be more useful?

joinA : (Functor m, Action n) => m (n (n a)) -> m (n a)
joinA = fmap join

-- like blah `flatMapA` \arg -> ...
flatMapA : (Action m, Action n) => m a -> (a -> m (n (n b))) -> m (n b)
flatMapA ma f = ma >>= (joinA . f)

When defining generic utility functions, I think it’s best to replace as many things with type variables up front as possible, because it clarifies what you are trying to do by clarifying what you are not trying to do. For example, you wouldn’t want this function’s behavior to change depending on what the ContractId Foos are.

In a more decorative, festive spirit,

f : (Action m, Action n) => m (n (n a)) -> m (n a)
f = (>>= (pure . (>>= identity))) -- Two-Stage To Orbit
← Back to Discussions