WhenSome, need help exp
- MaxOP
For the below, I’m having a difficult time understanding the signature here. I’m coming from a Javascript background.
Is this a Daml specific thing, or Haskell? and also is there a way to interpret this from a Javascript point of view?
: Applicative m => Optional a → (a → m ()) → m ()
Perform some operation on
Some, given the field inside theSome.For example, WhenSome is a function, I know that, but what is the argument it’s supposed to take?
Is not like map, or reduce is it?is it similar to …
if not null, run the function? I’m trying to understand the trigger example here for the autoreply trigger.
https://docs.daml.com/triggers/index.htmlmodule ChatBot where import qualified Daml.Trigger as T import qualified User import qualified DA.List.Total as List import DA.Action (when) import DA.Optional (whenSome) autoReply : T.Trigger () autoReply = T.Trigger { initialize = pure () , updateState = \_ -> pure () , rule = \p -> do message_contracts <- T.query @User.Message let messages = map snd message_contracts debug $ "Messages so far: " <> show (length messages) let lastMessage = List.maximumOn (.receivedAt) messages debug $ "Last message: " <> show lastMessage whenSome lastMessage $ \m -> when (m.receiver == p) $ do users <- T.query @User.User debug users let isSender = (\user -> user.username == m.sender) let replyTo = List.head $ filter (\(_, user) -> isSender user) users whenSome replyTo $ \(sender, _) -> T.dedupExercise sender (User.SendMessage p "Please, tell me more about that.") , registeredTemplates = T.AllInDar , heartbeat = None }- get all the message contracts
- instantiate the variable
messageswhich is a list of the message templates (and not the cid) - instantiate
lastMessage, which is taking a single element from the list above with the highest time - WHENSOME, what is that doing? if lastMessage is not null, then calle the function
\m->..and only when m.receiver is the party that is logged in?
Is that the correct way to interpret Whensome?
- Stephen
Max:For example, WhenSome is a function, I know that, but what is the argument it’s supposed to take?
Is not like map, or reduce is it?It is very much like
mapandreduce, as it turns out.fmap : Functor f => (a -> b) -> f a -> f b -- substitute f = Optional, and you get fmap : (a -> b) -> Optional a -> Optional b -- flip its arguments and you get (<&>) : Optional a -> (a -> b) -> Optional b -- mess with that b a bit, and you get a variant of map, -- a function we often recommend for doing actions in a list forA : Applicative f => [a] -> (a -> f b) -> f [b] -- generalizes so that we can replace [] with Optional forA : Applicative f => Optional a -> (a -> f b) -> f (Optional b) -- but you often want to discard the results instead of reconstructing the -- traversable you're walking across (i.e. the [] or Optional) -- so that replaces the b and Optional b with (), yielding the DA.Foldable function forA_ : Applicative f => Optional a -> (a -> f ()) -> f ()which is
whenSomeif you give it the more specific name.-- Every function in Foldable can be written with `foldr` or `foldl`, -- and `whenSome` qualifies for that too, hence the similarity to `reduce`. forA_ ta f = foldr (\a k -> f a *> k) (pure ()) ta
Max:Is that the correct way to interpret Whensome?
Yes.