{"id":"J94nJQ-QDcs","title":"Lesson 7: Collection Data Structures","description":"For a better learning experience, sign up and enroll in this free course at: https://daml.talentlms.com/plus/catalog/courses/141","channel_name":"Digital Asset Training","published_at":"2025-02-19T00:00:00.000Z","thumbnail_url":"https://i.ytimg.com/vi/J94nJQ-QDcs/hqdefault.jpg","transcript_text":"daml provides several data types that are collections of data elements assembled in one data structure for example lists tupal sets and Maps let's take a look at each of these in more detail lists are fairly straightforward there are comma separated list of elements enclosed in square brackets here we declare the int list variable and bind it to the list of elements enclosed in square brackets if you want to declare an empty list then just use square brackets ETS without any value in the same way fruit list is a list of text values note that there is no need to declare the data type of elements in the list because daml can infer from the values you provide daml provides many functions to manipulate lists in a module called da. lists we can import the entire module or just the functions we want to use in our code by putting them in parentheses next to the library module name the script list test shown here uses two functions from the list module head which Returns the first element of the list and tail which returns all the elements after the head a quick note about the use of the show function here the show function returns textual representation of its argument for those data types that can be converted to text so when you use debug to trace values you don't always need to use show but in this case we've added a short description to make the output more readable and since these descriptions are strings we need to use show to convert the function outputs to Strings so we can concatenate everything together let's look at some more list functions continuing with the same example of int list and fruit list this debug statement prints the result of a function called last which Returns the last value in the list then there is a function sort which returns a list with the elements sorted in the order specified for the data type of the elements in the list for example fruit list is a list of text and text is sorted alphabetically lists with int values are sorted in ascending order the init function returns all the elements except the last one the reverse function returns a list that has the elements in reverse order the take function followed by a number returns as many elements from the list starting with the first element the LM function returns true or false depending on whether the element specified after LM exists in the list so LM Apple fruit list will'll check if Apple exists in fruit list and if it does it returns true we can apply a LM to int list as well in the same way the last two functions sum and product apply to in nit list and they return the sum of elements and products of elements respectively sometimes we need to initialize a list with elements that are in a sequence say numbers from 10 to 20 or even numbers starting from 2 to 20 instead of typing out all those numbers we can use the range note notation for example the range list shown here has all the elements from 10 to 20 we can also create a sequence by specifying the first and the second elements to indicate the pattern of the sequence for example even list has two and four to give the hint and then last number 20 daml will understand you need alternate numbers starting from two in the list the first debug statement here shows the use of the length function and the next two just print the lists the last statement shows how to prend elements to a list here mango is included in the trace statement for fruit list note that this operation doesn't change fruit list as the variable itself is immutable if you want to concatenate two lists you can use angle brackets or the double plus operators the angle bracket notation can be used to concatenate strings as well as you can see in the debug statement here the double plus symbol is used only for lists tupal are quite similar to lists in the sense that tupal also assemble many elements together into one data structure however there are a few key differences tupal can contain elements of different data types one tupal may contain one integer two strings one list and a Boolean value daml also provides a few access functions for two and three element tuples let's take a look at some code to see how that works as shown here constructing a tuple is similar to constructing a list except we're using parentheses instead of square brackets the FST and snd functions help access first and second elements in a two Tuple the FST 3 and snd3 and the THD 3 functions help access first second and third elements respectively in a three Tuple also for the three Tuple assessors we need to import the da. Tuple Library module the ntuple example shown here has five elements that can be accessed by using underscore followed by the index number starting with one the underscore assessors are available only for tupal with up to five elements while tupal of Great Lengths are available it's often advisable to Define custom records with name fields for complex structures as overuse of tupes can harm code readability we'll get into custom records in another video the next type of data structure we should look at is a set it's available from the da. set Library which needs to be imported the statement import da. set as set gives us an alias to access functions in the set Library let's look at some examples in the script we first declare a variable fruit set as set text which means this is a set with text elements it's then bound to the empty set we can add elements to a set from another data collection for example a list or insert one element at a time let's say we have a fruit list that has four text elements we can load these into the set by using the set. from list function and passing the fruit list to it notice that this list had two occurrences of Apple but sets can have only unique elements notice how the debug statement prints the size of fruit set as three the string apple is added to the set only once as you can see by printing the contents of fruit set using the show function the set module has several functions one of which is member member takes an element and the set and returns true or false depending on whether the element is a member of the set or not since mango is not in the set it prints false in the next block of code we add mango to the set by using the set. insert function notice that we need to create a new variable new fruit set because variables in daml are immutable also notice that we didn't provide the type definition for new fruit set as we did with fruit set daml can infer the type from the expression on the right side once mango is inserted you can see the new sizes 4 and set. member returns true there are other functions in the set module for example Union intersection and filter that you can learn from the daml documentation the last data structure we'll look at is Maps you may be familiar with maps from other programming languages it's a structure that has a collection of key and value pairs the key is used to look up a map and find the value associated with the key so we start with importing da. map and providing an alias map to access its functions then we Define word map as a map that contains key as text and value as text and then we bind it to the empty map once again we can add elements to a map from other collections for example a list is shown here we have a words variable that has a list of tupal these tupal will become key value pairs when loaded into the map so we say word map equals map. from list words when you print the word map you can see its key value pairs printed and the size function shows two the function to look up a value using a key is well lookup so you say map. lookup followed by the key then the map variable name word map so map. lookup a word map returns some apple and map. lookup C returns none notice the value Su Apple this is specific to a data type called optional followed by one of the primitive data types such as text the map. lookup function returns a value of type optional text in this case we'll look at the optional data type in another video you can insert a new key value pair into the map as shown by using the map. insert function and assigning the new map to new word map finally the last two debug statements print the size and the contents of the new word map let's review what we've gone over daml lists can hold many elements of one data type there are functions available to manipulate data through the da. list module we looked at head tail last sort andit reverse take element sum and product daml tupal are similar to lists with key differences meaning that they can hold elements of different data types daml provides the set data structure in a library module called da. set it holds elements of a certain data type and by definition can hold only unique values of that data type we saw some set functions such as size member insert and from list finally daml also provides map data structures in a library module called da. map map is a structure that contains key value pairs we looked at how to add elements to a map check its size perform a lookup and print its contents","transcript_synced_at":"2026-10-26T04:02:52.733Z","segments":[{"start_seconds":0,"text":"daml provides several data types that are collections of data elements assembled in one data structure for","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=0s"},{"start_seconds":6,"text":"example lists tupal sets and Maps let's take a look at each of these in more","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=6s"},{"start_seconds":13,"text":"detail lists are fairly straightforward there are comma separated list of elements enclosed in square brackets","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=13s"},{"start_seconds":20,"text":"here we declare the int list variable and bind it to the list of elements enclosed in square brackets if you want to declare an empty list then just use","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=20s"},{"start_seconds":29,"text":"square brackets ETS without any value in the same way fruit list is a list of text values note that there is no need","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=29s"},{"start_seconds":37,"text":"to declare the data type of elements in the list because daml can infer from the values you provide daml provides many","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=37s"},{"start_seconds":44,"text":"functions to manipulate lists in a module called da. lists we can import the entire module or just the functions we want to use in our code by putting","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=44s"},{"start_seconds":53,"text":"them in parentheses next to the library module name the script list test shown here uses two functions from the list","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=53s"},{"start_seconds":60,"text":"module head which Returns the first element of the list and tail which returns all the elements after the head","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=60s"},{"start_seconds":68,"text":"a quick note about the use of the show function here the show function returns textual representation of its argument for those data types that can be","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=68s"},{"start_seconds":76,"text":"converted to text so when you use debug to trace values you don't always need to use show but in this case we've added a","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=76s"},{"start_seconds":83,"text":"short description to make the output more readable and since these descriptions are strings we need to use show to convert the function outputs to","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=83s"},{"start_seconds":91,"text":"Strings so we can concatenate everything together let's look at some more list functions continuing with the same","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=91s"},{"start_seconds":98,"text":"example of int list and fruit list this debug statement prints the result of a function called last which Returns the","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=98s"},{"start_seconds":105,"text":"last value in the list then there is a function sort which returns a list with the elements sorted in the order","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=105s"},{"start_seconds":113,"text":"specified for the data type of the elements in the list for example fruit list is a list of text and text is","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=113s"},{"start_seconds":120,"text":"sorted alphabetically lists with int values are sorted in ascending order the init function returns all the elements","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=120s"},{"start_seconds":128,"text":"except the last one the reverse function returns a list that has the elements in reverse order the take function followed by a","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=128s"},{"start_seconds":136,"text":"number returns as many elements from the list starting with the first element the LM function returns true or false","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=136s"},{"start_seconds":144,"text":"depending on whether the element specified after LM exists in the list so LM Apple fruit list will'll check if","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=144s"},{"start_seconds":151,"text":"Apple exists in fruit list and if it does it returns true we can apply a LM to int list as well in the same way the","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=151s"},{"start_seconds":160,"text":"last two functions sum and product apply to in nit list and they return the sum of elements and products of elements","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=160s"},{"start_seconds":167,"text":"respectively sometimes we need to initialize a list with elements that are in a sequence say numbers from 10 to 20 or even numbers starting from 2 to 20","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=167s"},{"start_seconds":176,"text":"instead of typing out all those numbers we can use the range note notation for example the range list","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=176s"},{"start_seconds":183,"text":"shown here has all the elements from 10 to 20 we can also create a sequence by specifying the first and the second","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=183s"},{"start_seconds":191,"text":"elements to indicate the pattern of the sequence for example even list has two and four to give the hint and then last","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=191s"},{"start_seconds":199,"text":"number 20 daml will understand you need alternate numbers starting from two in the list the first debug statement here","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=199s"},{"start_seconds":207,"text":"shows the use of the length function and the next two just print the lists the last statement shows how to prend elements to a list here mango is","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=207s"},{"start_seconds":216,"text":"included in the trace statement for fruit list note that this operation doesn't change fruit list as the variable itself is immutable if you want","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=216s"},{"start_seconds":225,"text":"to concatenate two lists you can use angle brackets or the double plus operators the angle bracket notation can be used to concatenate strings as well","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=225s"},{"start_seconds":233,"text":"as you can see in the debug statement here the double plus symbol is used only for lists","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=233s"},{"start_seconds":242,"text":"tupal are quite similar to lists in the sense that tupal also assemble many elements together into one data structure however there are a few key","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=242s"},{"start_seconds":250,"text":"differences tupal can contain elements of different data types one tupal may contain one integer two strings one list","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=250s"},{"start_seconds":257,"text":"and a Boolean value daml also provides a few access functions for two and three element tuples let's take a look at some","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=257s"},{"start_seconds":265,"text":"code to see how that works as shown here constructing a tuple is similar to constructing a list except we're using","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=265s"},{"start_seconds":272,"text":"parentheses instead of square brackets the FST and snd functions help access first and second elements in a","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=272s"},{"start_seconds":281,"text":"two Tuple the FST 3 and snd3 and the THD 3 functions help access first second and","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=281s"},{"start_seconds":288,"text":"third elements respectively in a three Tuple also for the three Tuple assessors we need to import the da. Tuple Library","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=288s"},{"start_seconds":297,"text":"module the ntuple example shown here has five elements that can be accessed by using underscore followed by the index","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=297s"},{"start_seconds":304,"text":"number starting with one the underscore assessors are available only for tupal with up to five","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=304s"},{"start_seconds":311,"text":"elements while tupal of Great Lengths are available it's often advisable to Define custom records with name fields","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=311s"},{"start_seconds":319,"text":"for complex structures as overuse of tupes can harm code readability we'll get into custom records in another video","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=319s"},{"start_seconds":327,"text":"the next type of data structure we should look at is a set it's available from the da. set Library which needs to","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=327s"},{"start_seconds":333,"text":"be imported the statement import da. set as set gives us an alias to access functions in the set Library let's look","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=333s"},{"start_seconds":342,"text":"at some examples in the script we first declare a variable fruit set as set text which means this is a set with text elements","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=342s"},{"start_seconds":351,"text":"it's then bound to the empty set we can add elements to a set from another data collection for example a list or insert","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=351s"},{"start_seconds":359,"text":"one element at a time let's say we have a fruit list that has four text elements we can load these into the set by using","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=359s"},{"start_seconds":367,"text":"the set. from list function and passing the fruit list to it notice that this list had two occurrences of Apple but","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=367s"},{"start_seconds":375,"text":"sets can have only unique elements notice how the debug statement prints the size of fruit set as three the string apple is added to the set only","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=375s"},{"start_seconds":384,"text":"once as you can see by printing the contents of fruit set using the show function the set module has several","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=384s"},{"start_seconds":390,"text":"functions one of which is member member takes an element and the set and returns true or false depending on whether the","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=390s"},{"start_seconds":398,"text":"element is a member of the set or not since mango is not in the set it prints false in the next block of code","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=398s"},{"start_seconds":407,"text":"we add mango to the set by using the set. insert function notice that we need to create a new variable new fruit set because","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=407s"},{"start_seconds":415,"text":"variables in daml are immutable also notice that we didn't provide the type definition for new fruit set as we did","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=415s"},{"start_seconds":422,"text":"with fruit set daml can infer the type from the expression on the right side once mango is inserted you can see","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=422s"},{"start_seconds":430,"text":"the new sizes 4 and set. member returns true there are other functions in the set module for example Union","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=430s"},{"start_seconds":438,"text":"intersection and filter that you can learn from the daml documentation the last data structure we'll look at is Maps you may be","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=438s"},{"start_seconds":447,"text":"familiar with maps from other programming languages it's a structure that has a collection of key and value pairs the key is used to look up a map","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=447s"},{"start_seconds":454,"text":"and find the value associated with the key so we start with importing da. map and providing an alias map to access its","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=454s"},{"start_seconds":462,"text":"functions then we Define word map as a map that contains key as text and value as text and then we bind it to the empty","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=462s"},{"start_seconds":471,"text":"map once again we can add elements to a map from other collections for example a list is shown here we have a words","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=471s"},{"start_seconds":479,"text":"variable that has a list of tupal these tupal will become key value pairs when loaded into the map so we say word map","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=479s"},{"start_seconds":487,"text":"equals map. from list words when you print the word map you can see its key value pairs printed and the size","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=487s"},{"start_seconds":494,"text":"function shows two the function to look up a value using a key is well lookup so you say","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=494s"},{"start_seconds":501,"text":"map. lookup followed by the key then the map variable name word map so map.","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=501s"},{"start_seconds":508,"text":"lookup a word map returns some apple and map. lookup C returns none notice the","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=508s"},{"start_seconds":515,"text":"value Su Apple this is specific to a data type called optional followed by one of the primitive data types such as","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=515s"},{"start_seconds":523,"text":"text the map. lookup function returns a value of type optional text in this case we'll look at the optional data type in","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=523s"},{"start_seconds":531,"text":"another video you can insert a new key value pair into the map as shown by using the map. insert function and","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=531s"},{"start_seconds":538,"text":"assigning the new map to new word map finally the last two debug statements print the size and the contents of the new word","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=538s"},{"start_seconds":546,"text":"map let's review what we've gone over daml lists can hold many elements of one data type there are functions available","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=546s"},{"start_seconds":554,"text":"to manipulate data through the da. list module we looked at head tail last sort","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=554s"},{"start_seconds":561,"text":"andit reverse take element sum and product daml tupal are similar to lists with key differences meaning that they","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=561s"},{"start_seconds":569,"text":"can hold elements of different data types daml provides the set data structure in a library module called da.","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=569s"},{"start_seconds":577,"text":"set it holds elements of a certain data type and by definition can hold only unique values of that data type we saw","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=577s"},{"start_seconds":584,"text":"some set functions such as size member insert and from list finally daml also provides map data","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=584s"},{"start_seconds":592,"text":"structures in a library module called da. map map is a structure that contains key value pairs we looked at how to add","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=592s"},{"start_seconds":600,"text":"elements to a map check its size perform a lookup and print its","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=600s"},{"start_seconds":624,"text":"contents","url":"https://www.youtube.com/watch?v=J94nJQ-QDcs&t=624s"}],"youtube_url":"https://youtube.com/watch?v=J94nJQ-QDcs","url":"https://ccpedia.xyz/videos/J94nJQ-QDcs"}