Search code examples
daml

How do I check if an element is present in list of records in DAML?


Suppose Event is record with fields like eventName and eventParameter. I want to assert for the condition if some eventName say "EV1" is present in list of records. For list of native Data types (let say Parties) I can simply check with (elem in following) as given in docs. Can anyone help me what would be the syntax for record data type?


Solution

  • elem is an instance of a more general function any : (a -> Bool) -> [a] -> Bool which takes a predicate and a list of elements and returns true if the predicate holds for at least one element in the list. elem x xs is equivalent to any (\y -> x == y) xs.

    Using any you can express your example as follows:

    data Event = Event with
      eventName : Text
    
    test = scenario do
      assert $
        any (\r -> r.eventName == "EV1")
            [Event with eventName = "EV1"]
      assert $ not $
        any (\r -> r.eventName == "EV1")
            [Event with eventName = "EV2"]