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 the Some
.
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.html
module 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
messages
which 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?