# WhenSome, need help exp

**URL:** <https://forum.canton.network/t/whensome-need-help-exp/4264>\
**Category:** App Development\
**Tags:** syntax\
**Created:** [April 6, 2022, 9:06pm UTC](https://forum.canton.network/t/whensome-need-help-exp/4264 "2022-04-06T21:06:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/max/32/2128_2.png) [@Max](https://forum.canton.network/u/Max)\
**Post date:** [April 6, 2022, 9:06pm UTC](https://forum.canton.network/t/whensome-need-help-exp/4264/1 "2022-04-06T21:06:56Z")

</div>

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](https://docs.daml.com/daml/stdlib/DA-Optional.html#function-da-optional-whensome-82167)

: [Applicative](https://docs.daml.com/daml/stdlib/Prelude.html#class-da-internal-prelude-applicative-9257) m =\> [Optional](https://docs.daml.com/daml/stdlib/Prelude.html#type-da-internal-prelude-optional-37153) 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](https://docs.daml.com/triggers/index.html)

```haskell
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?

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [April 6, 2022, 9:36pm UTC](https://forum.canton.network/t/whensome-need-help-exp/4264/2 "2022-04-06T21:36:54Z")

</div>

> [@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.

```haskell
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.

```haskell
-- 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.
