flatMapA in DAML?

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

2 Likes
f : Update [[ContractId Asset]] -> Update [ContractId Asset]
f = fmap concat

seems to compile.

5 Likes

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.

3 Likes

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
4 Likes