Daml 101: Using [Lists]: Who removed Pluto?!?! [2022]
Nothing is loaded from YouTube until you press play, and then without tracking cookies.
About this video
📚Check out Daml Learning Center: bit.ly/LearnDaml 📖Join our forum and get started: bit.ly/tryDaml
This is a multi-episode series intended to take you from the brief context behind blockchain and smart contract to the development of smart contracts using a next-gen smart contract language called Daml. The entire series can be found here: bit.ly/damlone
Daml is a development framework for creating systems of record that securely connect across business processes and data boundaries while retaining privacy, guaranteeing data integrity, and enforcing strict authorization. Allowing such systems to cross boundaries — legal, regulatory, organizational, or other — enables them to form seamless economic networks.
Transcript
2,688 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
in 2006 the iau voted to remove pluto from the list of planets in the solar system essentially demoting it from a planet to a dwarf planet that sent millions of teachers and parents with sharpies to go cancel out pluto from their kids science textbooks in this video we're going to look at lists and see how easy it is to remove and add items into the list and we'll see if there's anything we can do for poor pluto
- 0:32
don't go anywhere hi this is steve cell from digital asset apart from software and technology i love astronomy i've become a fan in the last year or so and i'm constantly amazed at the new things that we are learning about the universe take a look at this picture of the milky way that i took in august astonishing isn't it another astonishing thing i learned was the removal of pluto from the list of
- 1:06
planets of the solar system super strange right see what happened was the astronomers were discovering more and more celestial bodies like pluto so they decided to change the criteria and pluto didn't meet the bar so it was demoted to a dwarf planet i don't like it but i'm no astronomer i thought in this video it'd be great for us to use that as an example to see how we can handle lists in demo
- 1:36
in your project you will undoubtedly need to lump more than one data point into one variable and this will be the way to go so let's take a closer look and we're gonna do something radical we're gonna put pluto back in the list as always we're going to fire up the terminal and do a demo new let's call it justice for pluto ah that's a little dramatic but just let's call it solar system planets and we're going to use the
- 2:08
template empty skeleton this will require us to create our new demo our own demo file that should be fine let's fire up demo studio and visual studio vs code will fire up all right let's go into the folder where we need to create this file and let's go ahead and save it before we forget and we'll call it main dot demo wonderful here i'm trying to make sure that we have saved the demo file into the right folder so let's go ahead and define the module main same as the file name and we're
- 2:50
going to use demo script we're not going to use the navigator or any ui but we'll use the script and its output to help us look at our code i'm going to define it define a contract id we're going to call it universe um and there'll be just two input arguments uh a an astronomer that will be authorizing this contract and um a planet and that's the way you would define a array by using the and the brackets okay so the signatory will be just the
- 3:25
astronomer and the astronomer is going to be able to add a planet and be able to remove a planet so let's wire up the ad so here's a choice the first choice is to add it so when he access he or she exercises this choice we will insert a planet into the list and the only information we need is just the name of the planet that we are trying to add so let's uh set it as a text and under the do block we're gonna say let's create a contract out of this template
- 4:12
and we're gonna set the planets and this is the way you would append to the planets list now this is an interesting point we're going to take a pause for one second this by default will add the new item to the beginning of the list and this is actually a very good thing remember that demo is a version of haskell and in haskell lists are constructed behind the scenes as linked lists now linked lists if you remember your computer science courses is a
- 4:51
data structure that link one value to another to another in order for you to get to any item along that chain you need to start at the beginning so if you're looking for something at the end you will need to traverse your linked list at the start of the linked list which they called the head now adding an item to the beginning of the linked list is merely just changing the head so that the new item is now the head now if you want to add an item at the end of the
- 5:23
linked list you will need to traverse from the hit all the way to the tail and then add it so your your um your time complexity becomes o of n so big o of n which means for as long as your linked list is you need to traverse the whole length just to get to add it at the end so that takes a hit on performance fantastic now let's go ahead and wire up the second choice which is to remove the planet from the list so remove the planet as a choice will return the same
- 5:57
universe id and we're gonna simply ask for the name of the planet to remove that will be of type data type text i'm sorry um we're going to do the following we're going to create the same contract out of this template and the command to remove an item from a list is delete now you notice that it is giving an error just go ahead and import the da dot list and that should activate that function now let's uh
- 6:35
start wiring up our our script here we're gonna have the script return a universe id and for the test we are gonna just run a couple of simple uh lines to make sure everything is working i like neil degrasse tyson so i'm gonna call my astronomer neil though i think he's technically a theoretical astrophysicist but that's okay uh solar system uh will be the first contract and what we're gonna do is to have neil uh create the universe
- 7:10
um universe actually let's call it a planetary planetary system uh forgive the misspelling here planetary system i'm missing the r in this demo that's okay so it's not really the universe the universe is much wider but let's call it the planetary system pardon the typo uh and we're gonna create it with the astronomer as uh neil and we are going to pass in uh a list of planets that were uh in this example already discovered let's say um now you can pass in an empty list if
- 7:53
you want to but we're gonna just uh pre-load it with one two three four five six uh planets and then we'll add the rest in the uh following lines so let's try this and see if it works at all let's look at the script results fantastic we got earth all the way to saturn that's in so we are good now we do need to return the results into a contract id here so we call it the solar system so this is contract number one 0.0 as you can see in the output uranus was discovered in
- 8:26
1781 so let's go ahead and say that it was discovered and neil is gonna go ahead and uh he's not gonna create it he's gonna exercise the command to add it so he's going to add to that currently active contract he's going to exercise the choice at planet and he's going to supply a name uranus and we should be good let's temporarily comment this out to make sure that we add the planet correctly yep it went in wonderful so let's restore that and make sure that we have
- 9:05
a new active contract called discover uranus perfect so the next thing i want to do is to add a few more uh planets the next one that was discovered was neptune it was discovered in 1846 so let's go ahead and just change that now remember that we need to exercise the choice on the latest active contract which is discovered uranus and we're going to add neptune so pluto was discovered in 1930 and so
- 9:38
we're gonna just copy and paste this line to add pluto to the planets uh but let's try something let's try to add neptune a second time let me just change the contract we can add neptune a second time and you see that there are two entries of neptune in the output results so how do we take care of something like that so this is what we can do now we go back into our choice under the do block and we can do a check here that says if the element doesn't
- 10:10
exist we can proceed executing the code so basically this is a conditional check a condition a precondition check to make sure that this is not an element and this is how we would code it to make sure that it's not an element in the planet list of course we can use a set by definition a set can only contain unique elements but we're using a list here you see that a an exception has been thrown and displaying the exact message that we had so let's correct that and put it to pluto and that went in fantastic
- 10:43
awesome now for the next step let's try to remove pluto just like the iau did in in 2006 so we're going to remove pluto by exercising a choice on the current contract called remove planet so neil is going to exercise the command on the current active contract and the choice is called remove planet and we're going to supply a planet name pluto and look at the output it's already gone that's great but what if we add a typo uh obviously there's no pluto's
- 11:24
and uh how can we check for things like that similarly we can do an assert message to say hey look we cannot find this planet and the way we check it is to use a elam or element followed by the planet that we are removing or trying to remove from the list so again it throws the exception just as we expected that they cannot find this planet so the rest of the code doesn't execute fantastic let's fix that and everything
- 11:55
is back to normal pluto has been removed not good right okay so now we are going to depart from uh uh conventional thinking here we're gonna reinstate pluto and we are going to try to add it so neil is going to exercise a command to the latest active contract he's going to call the we're going to exercise the add planet choice and we're going to add pluto back in so that is great but the order of the planets are sort of all over the place the last few were added line by line and the rest were
- 12:41
part of a list we can sort them if we want to so let's try that next let's go up to our code and check out that blue lint what they call enter there we can take care of that is telling you to use a particular fix now go into the dlint.yaml and add this following line that will ignore that that infix lint that pops up okay it's uh it'll still be there if you were to close your environment and reload demo studio again
- 13:19
it should be gone outstanding all right let's uh load up our script results again we were trying to sort the elements in the planets list and to do so um it's as easy as adding the word sort and putting that line in parenthesis you will get a error under sort just go ahead and go up and remove the delete from the parenthesis and that will load in the entire library that you can use
- 13:49
now see that it's already sorted that is great now we can also do something like reverse if we need to but remember that we are dealing with a linked list so the there's a performance hit if this list gets way too large now um there are when you must must add to the end of the list uh this is the way you would do it you specify the planets first followed by a plus plus and then you add the planet you're adding to as a single element list
- 14:22
all right so but please don't forget the performance hit so so we are now would have added pluto at the end of the list as the results show and wonderful now let's go back to our test script again and uh let's see what happens uh let's first assign that to a new contract id let's call it reinstated pluto and what we're gonna do next is this we are going to try to add pluto again to reinstate it again
- 15:04
and we should see that it already exists right so that is correct but is there another way to handle that yeah we can always do a d-dupe to remove the duplicate but i find the assert line to be a more elegant solution there's no reason to continue executing the code only to do more work so we will keep the assert statement at the top so we are not going to add um pluto but let's try something else let's try adding a list to the list so a list of planets so they discovered a bunch of
- 15:44
dwarf planets and now we want to add a bunch of planets at the same time instead of doing it singularly so we need a new we need a new oops this should be two lists we need a new choice that can add planets instead of just a singular planet so we're gonna ask for now a list that's gonna be of type text so essentially we're asking for a list of the text data type and we're going to add it to a list of text data type and
- 16:18
remember that you cannot mix the data type it's homogeneous so it's it has to be the same data type inside of the list unlike what you'll find say in javascript where you can mix and match uh elements or data types inside an array so we're going to do a plus plus just like what we did for the previous uh method of adding a planet to the end of the list we have no choice now but to add two lists together using the plus class so let's go down to our test
- 16:48
and we now have a choice called add planets and we are going to specify uh the planets to add and we're gonna um just list them up so they found a bunch of these um dwarf planets iris hamiya siris makimaki so to the horror and objection of the iau we are not only going to reinstate pluto we're going to add more planets to the list uh so there's another elegant more elegant way
- 17:27
to do this and that is to define the list uh separately before just before we exercise command and that is to use the left command and we'll say these are the new kits on the block and the new kids on the block are these uh four beautiful dwarf planets and we're going to go ahead and add them into the exercise command and there you go pluto is back and more planets for everybody so i hope you have fun manipulating the planets of the solar system with me in this episode
- 18:01
we've got more content lined up see you in the next episode bye you