I feel I need some help in reading and understanding the Motoko documentation to advance more rapidly and with less trial-and-error. I’ve got a background in Python which feels way easier to grasp tbh. Also it’s older so I can just google many things. Let’s take the find
function for Arrays as an example. The documentation says the following:
func find<A>(xs : [A], f : A -> Bool) : ?A
These are my questions:
- What does
<A>
stand for? Are<>
just placeholders for variables in the documentation? - What do question marks stand for? I’ve seen them preceding variables
?A
but also seemingly indicate optional notations like<sort> <id>? =? <obj-body>
, i.e. in the language quick reference. - What does
xs
stand for and why xs?
Here’s my take at using the find
function on an Array:
var myArray : [Nat] = [2, 4, 6];
Array.find(myArray, func x {x == 2});
And this would be the corresponding notation in natural language:
Run a function on myArray that returns a Boolean value for each entry in that array.
This means that I’m expecting it to return an Array with Boolean values but this doesn’t seem to be the case. How can I tell from the documentation what a function returns?
Here’s another take:
var myArray : [Nat] = [2, 4, 6];
Array.find(myArray : [Nat], func x {x == 2});
But something tells me it might as well work like this:
var myArray : [Nat] = [2, 4, 6];
Array.find<Bool>(myArray, func x {x == 2});
Can anybody tell what my problem is?