Basic containers

Lists

Lists are built with [ ; ]:
1
effect {
2
var l = [ 1; 2; 3 ];
3
}
Copied!
The length operator returns the number of elements in the list.
1
effect {
2
var c = length([1; 2; 3]);
3
if c = 3 then transfer 1tz to coder;
4
}
Copied!
The contains operator returns true if and only if an element is in the list.
1
effect {
2
if contains([1; 2; 3], 2) then transfer 1tz to coder;
3
}
Copied!
The nth operator retrieves the element at index n starting from 0:
1
effect {
2
var v = nth(["1"; "2"; "3"], 2);
3
if v = "3" then transfer 1tz to coder;
4
}
Copied!
It is possible to iterate over a list with the for do done loop syntax:
1
effect {
2
var l = [3; 4; 5];
3
for i in l do
4
transfer (i * 1tz) to coder;
5
done; // coder receives 12tz ...
6
}
Copied!
It is possible to create a list by prepending an element (as the first element) to an existing list with the prepend.
1
effect {
2
var l = prepend(["second"], "first"); // l is ["first"; "second"]
3
if nth(l, 0) = "first" then transfer 1tz to coder;
4
}
Copied!

Sets

Sets are containers of unique objects. It is possible to add and remove an object, test if an object is in a set, and know the number of elements in a set.
Sets may currently contain only builtin types: int nat string bytes tez bool key_hash date duration and address.
Sets are built with [ ; ]:
1
variable s : set<nat> = [ 1; 3; 5; 12 ]
Copied!
The contains operator returns true if an element is an element of the set, false otherwise.
1
effect {
2
var s : set<nat> = [0; 1 ; 2; 3];
3
var t : bool = contains(s, 2); /* true */
4
}
Copied!
The add operator returns a set with an extra element if the element is not already in the argument set. It returns the same set if the element is already in the set.
1
effect {
2
var s : set<nat> = [0; 1 ; 2; 3];
3
var s1 = add(s, 5); /* [0; 1 ; 2; 3; 5] */
4
}
Copied!
The remove operator returns a set with an element removed if the element was present in the argument set . It returns the same set if the element is not in the set.
1
effect {
2
var s : set<nat> = [0; 1 ; 2; 3];
3
var s1 = remove(s, 2); /* [0; 1 ; 3] */
4
}
Copied!
The length operator returns the number of elements in the set.
1
effect {
2
var s : set<nat> = [0; 1 ; 2; 3];
3
var l = length(s); /* 4 */
4
}
Copied!

Maps

Maps are collections of pairs (key, value). It is possible to retrieve efficiently the value associated to a key.
Key values may currently be only builtin types : int nat string bytes tez bool key_hash date duration and address. Values may be any type, except assets.
Maps are built with [ ( , ) ; ]:
1
variable m : map<nat, string> = [ (0, "a string"); (1, "another string") ]
Copied!
The put operator returns a map with a new pair (key,value) if the key is not associated in the argument map. It returns the same map otherwise.
1
effect {
2
var m : map<nat, string> = [ (0, "a string"); (1, "another string") ];
3
var m2 = put(m, 2, "and another ...");
4
}
Copied!
The remove operator returns a map that does not pair the argument key.
1
effect {
2
var m : map<nat, string> = [ (0, "a string"); (1, "another string") ];
3
var m2 = remove(m, 0); /* [ (1,"another string") ] */
4
}
Copied!
The [ ]operator returns an option of the value associated to the argument key.
1
effect {
2
var m : map<nat, string> = [ (0, "a string"); (1, "another string") ];
3
var v = m[1]; /* some("another string") */
4
var v2 = m[42]; /* none */
5
}
Copied!
The contains operator returns true if the map associates a value to the argument key, false otherwise.
1
effect {
2
var m : map<nat, string> = [ (0, "a string"); (1, "another string") ];
3
var t = contains(m, 0); /* true */
4
var f = contains(m, 42); /* false */
5
}
Copied!
The length operator returns the number of pairs in the map.
1
effect {
2
var m : map<nat, string> = [ (0, "a string"); (1, "another string") ];
3
var l = length(m); /* 2 */
4
}
Copied!
Last modified 8mo ago
Export as PDF
Copy link
Edit on GitHub