Skip to content
CCPEDIAby Unity Nodes
App Development

Applying Filter to Contract Elements from a List of Contracts

3 posts380 viewsLast activity Oct 2022

  1. barmentOP

    Hi All,

    I am working on a simple application as I learn DAML and ran into a roadblock. I have a list of contracts I’ve created and I want to be able to filter on elements embedded within the contracts.

    I think the code below is failing due to the filter function looking at a list of TestData while the anon function is attempting to match on the 4 elements within TestData which I am unsure how to access. Is there a solution to have my filter access each TestData and pull the 4 fields out to allow me to filter based on the budget? In other words, I have a list of contracts each containing a budget element that I want to filter out if the budget is over a given value. I am unsure how to access the underlying elements as the filter traverses the list of contracts.

    Appreciate any help on this!

    template TestData
    with
    me: Party
    rev: Int
    exp: Int
    budget: Int
    where
    signatory me

    template TestTemp
    with
    me: Party
    testList:[TestData]
    where
    signatory me
    choice FilterList : ContractId TestTemp
    controller me
    do
    let newList = filter ((,,_,a) → (a > 100)) testList <— fails here
    create this with testList = newList

    Error message:
    c:/Users/barment/Documents/Blockchain/DAML/daml-test/daml/Test.daml:51:64: error:
    • Couldn’t match type ‘TestData’ with ‘(a0, b0, c0, Int)’
    Expected type: [(a0, b0, c0, Int)]
    Actual type: [TestData]
    • In the second argument of ‘filter’, namely ‘testList’
    In the expression: filter (\ (_, _, , a) → (a > 100)) testList
    In an equation for ‘newList’:
    newList = filter (\ (
    , _, _, a) → (a > 100)) testList
    • Relevant bindings include
    newList : [(a0, b0, c0, Int)]
    (bound at c:/Users/barment/Documents/Blockchain/DAML/daml-test/daml/Test.daml:51:21)

  2. Matteo_Limberto

    Hi @barment and welcome to the forum.

    You can access the elements of a record type with the . operator.

    You can filter for budget as follows:

    template TestTemp
      with
        me: Party
        testList:[TestData]
      where
        signatory me
    
        choice FilterList : ContractId TestTemp
          controller me
          do
            let newList = filter (\e -> e.budget > 100) testList
            create this with testList = newList
    

    Matteo

  3. barment

    Thank you Matteo! I had tried the dot operator on the left side of the arrow with no luck but this works as intended. Appreciate the quick response.