Primitive Expressions¶
prim
expr
Parentheses, braces, and square brackets set off primitive expressions.
Parentheses override normal precedence rules:
>>> 4 + 2 * 3
10
>>> (4 + 2) * 3
18
Noun¶
NounExpr
name
A noun is a reference to a final or variable slot:
>>> Int
Int
>>> _equalizer
_equalizer
Any string literal prefixed by :: can be used as an identifier:
>>> { def ::"hello, world" := 1; ::"hello, world" }
1
Literal Expression¶
LiteralExpr
List Expression¶
ListExpr
comprehension
iter
Among Monte’s collection types, the list is a very common type. Lists are heterogenous ordered unsorted collections with sequencing and indexing, and have the performance characteristics of arrays in C, vectors in C++, or lists in Python:
>>> ['I', "love", "Monte", 42, 0.5][3]
42
A list expression evaluates to a ConstList
:
▲> { def l := ['I', "love", "Monte", 42, 0.5]; l[3] := 0 }
...
Message refused: ([I, love, Monte, 42, 0.500000], Atom(put/2), [3, 0])
Use diverge
and snapshot
to go from ConstList
to mutable
FlexList
and back:
>>> { def l := ['I', "love", "Monte", 42, 0.5].diverge(); l[3] := 0 }
0
See also
Expansion:
>>> m`[]`.expand()
m`_makeList.run()`
Map Expression¶
MapExpr
mapItem
Monte uses the “fat arrow”, =>
for map syntax:
>>> { def m := ["roses" => "red", "violets" => "blue"]; m["roses"] }
"red"
Like list expressions, a map expressions evaluates to an immutable
data structures, a ConstMap
:
▲> { def m := ["roses" => "red", "violets" => "blue"]; m["roses"] := 3 }
...
Message refused: ([roses => red, violets => blue], Atom(put/2), ["roses", 3])
Use diverge
and snapshot
similarly:
>>> { def m := ["roses" => "red", "violets" => "blue"].diverge(); m["roses"] := 3 }
3
Warning
Maps in monte are ordered:
>>> [ "a" => 1, "b" => 2] == [ "b" => 2, "a" => 1]
false
To compare without regard to order, use sortKeys
:
>>> [ "a" => 1, "b" => 2].sortKeys() == [ "b" => 2, "a" => 1].sortKeys()
true