Skip to content
CCPEDIAby Unity Nodes
VideosLesson 3: Functional Programming Part 3

Lesson 3: Functional Programming Part 3

Nothing is loaded from YouTube until you press play, and then without tracking cookies.

About this video

For a better learning experience, sign up and enroll in this free course at: daml.talentlms.com/plus/catalog/courses/150

Transcript

1,280 words

Captions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.

  1. 0:00

    this lesson will review the monatic action and some of the iterative functions to perform action on individual elements of a list let's start by refreshing your memory of the difference between pure expressions and action pure expressions are evaluated solely based on the values of their input arguments they rely on no cont text and cause no side effects pure expressions are deterministic meaning they consistently produce the same output for

  2. 0:38

    a given input for example when invoking the add function with two and three it will always evaluate to five regardless of how many times or when it is called its execution remains isolated within the function without affecting external variables or state action is a tight class which has various instances such as update script optional either Etc among the various instances this

  3. 1:10

    lesson will focus on update and script because they are the only types that lead to Ledger actions or Ledger events for instance optional is also a data type that happens to be an instance of action however optional evaluates to either some value or or none it does not evaluate to either update or script therefore optional does not lead to a ledger action action in this lesson refers to the Expressions that evaluate to either update or a

  4. 1:44

    script unlike pure Expressions action Expressions that evaluate to update or script rely on context such as Ledger State they also have the effect of updating The Ledger update directly leads to a ledger action while script can lead to a command submission the command submission then causes daml to run on The Ledger to generate an update which then leads to The Ledger actions use the do notation to compose an

  5. 2:15

    action use the left arrow notation to bind the result of the action on the right side to the variable on the left side using update and script in addition to creating a contract and submitting a transaction to a ledger you can also retrieve data from The Ledger all the functions on the left side return update and the functions on the right side return script fetch fetch by key and lookup by key are used in a choice which returns

  6. 2:46

    and update all the query functions are used in a test script since the return type of a test script is script let's try testing one of them query here's a simple bank account template which takes bank account owner and balance Bank can exercise apply interest on a bank account contract to apply 5% of interest to the account the test script has allocated four parties Bank Alice Bob and Charlie each of Alice Bob and Charlie has an

  7. 3:22

    account with a balance of 100.0 use the query function to query all the active bank account contracts that are visible to the bank party it returns a script value containing the long list of tupples if you want to have the bank party exercise apply interest on each one of the contract IDs you would have to iterate through this list you may have thought of using map but it doesn't work in this scenario let's talk about

  8. 3:53

    why in daml the map function takes a function and a list as arguments the function provided to map should return a value of type B if you map the show function over the list of 1 2 3 it evaluates to a list of text in a test script we can pass this list of text to debug as a quick reminder when you pass a value to debug it prints the value and returns an empty action which can be either an empty script or an empty

  9. 4:27

    update in Choice Bo iies debug returns an empty update while in test scripts it returns an empty script in both cases debug prints the value and returns an empty action as a result when you provide the map expression resting in a list of text to the debug function it prints the list and returns an empty script which is an action however when you map the debug function over the same list you end up

  10. 4:58

    with a list of actions not an action containing a list this is a problem because inside a do block you need each expression to either one evaluate to an action or two if it's a variable it needs to be used in an action expression to ensure that it has an effect on The Ledger in other words you meet a way to convert this list of actions into a single action containing a list of results

  11. 5:30

    to address this you can use the map a function instead of map map a also takes a function and a list as arguments the key difference is that map a performs an action on each element of the input list then it converts the list of actions to return a single action containing a list of results the type constraint applicative M indicates that the value of Type M in the signature must be an instance of the applicative tag

  12. 6:03

    class in daml the funter tag class provides a way to apply a pure function to values wrapped in a context such as list script update optional and so on the applicative type class further extends this capability by enabling the application of one or more functions within a context to values within another context functions like map a are considered effectful because they evaluate to an applicative effect of

  13. 6:34

    Performing actions Beyond pure computation such as modifying The Ledger the action type class allows composing these effectful functions while keeping them encapsulated within a context let's take a look at some examples of using map a in the first example map a Maps debug over the list 1 2 3 in the second example map a Maps debug over the list of tuples representing the active bank account

  14. 7:05

    contracts both examples using map a evaluate to an action containing a list of empty tupples and debug prints the corresponding values for this reason you must use map a to exercise apply interest choice on each of the contract IDs by printing the all accounts variable you can see that it's a list of Tes each Tuple contains contract ID as its first element and contract payload

  15. 7:35

    as its second element you can use map a by passing the Lambda function which takes each Tuple element X and have the bank party exercise apply interest choice on the first element of each Tuple X as a result the map a expression evaluates to a list of new contract IDs you can confirm on the table will view that all three of the old contracts were archived and three new contracts were created with increased balances in addition to map a there are

  16. 8:10

    other iteratives that you can use for mapping actions over a list there is for a which is map a with its arguments flipped there are also map a and for a with an underscore in case you want to perform actions while you're not concerned with the results of evaluating the actions there's also fold la which works similarly to the regular fold L except fold La is used for performing

  17. 8:42

    actions this lesson focused on the action typ class update and script which are instances of action are different from Pure Expressions due to their context dependency you can use actions to interact with a ledger when you perform actions in an iterative you can use action specific iterative functions such as map a or for a instead of regular iterative functions