Skip to content
CCPEDIAby Unity Nodes
App Development

WhenSome, need help exp

2 posts290 viewsLast activity Apr 2022

  1. 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?

    whenSome

    : 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
      }
    
    
    1. get all the message contracts
    2. instantiate the variable messages which is a list of the message templates (and not the cid)
    3. instantiate lastMessage, which is taking a single element from the list above with the highest time
    4. 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?

  2. 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 map and reduce, 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 whenSome if 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.