Skip to content

Commit 97f60ee

Browse files
author
Joel Abrahams
committed
doc: list indexing
1 parent ec4c747 commit 97f60ee

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,34 @@ def factorial(x: Int) -> Int := match x
101101
]
102102
```
103103

104+
### 🍡 Collections
105+
106+
In mamba sets, lists and maps are first class citizens, they are baked into the language.
107+
Unlike C-style languages, collection indexes are also a function.
108+
109+
```mamba
110+
# lists
111+
def a := [0, 2, 51]
112+
def b := ["list", "of", "strings"]
113+
# lists of tuples, buidler syntax
114+
def ab := [(x, y) | x in a, x > 0, y in b, b != "of" ]
115+
116+
# sets
117+
def c := { 10, 20 }
118+
def d := { 3 }
119+
# sets, builder syntax
120+
def cd := { x ^ y | x in c, y in d }
121+
122+
# maps
123+
def e := { "do" => 1, "ree" => 2, "meee" => 3 }
124+
# maps, builder syntax
125+
def ef := { x => y - 2 | x in e, y = x.len() }
126+
127+
# indexing works for lists and maps/mappings (sets cannot be indexed because these are unordered)
128+
print(ab(2)) # prints '(2, "list")'
129+
print(ef(1)) # prints '1'
130+
```
131+
104132
### 📋 Types, Classes, and Mutability
105133

106134
Classes are similar to classes in Python, though we can for each function state whether we can write to `self` or not by stating whether it is mutable or not.

docs/spec/characters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ The following is a list of characters in the language
1010

1111
Symbol | Use
1212
---|---
13-
`(` | Denote start of tuple elements or function arguments
14-
`)` | Denote end of tuple elements or function arguments
13+
`(` | Denote start of tuple elements or function arguments, including collection indexing (list or mapping)
14+
`)` | Denote end of tuple elements or function arguments, including collection indexing (list or mapping)
1515
`{` | Denote start of set, set constructor, or map
1616
`}` | Denote end of set, set constructor, or map
1717
`[` | Denote start of list, or opening bracket of generics of a class

docs/spec/grammar.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ The grammar of the language in Extended Backus-Naur Form (EBNF).
4646
| control-flow-expr
4747
| code-block
4848
| collection
49-
| index
5049
| key-value
5150
| operation
5251
| anon-fun
@@ -66,7 +65,6 @@ The grammar of the language in Extended Backus-Naur Form (EBNF).
6665
6766
slice ::= expression ( "::" | "::=" ) expression [ "::" expression ]
6867
range ::= expression ( ".." | "..=" ) expression [ ".." expression ]
69-
index ::= expression "[" expression "]"
7068
7169
definition ::= "def" ( variable-def | fun-def | operator-def )
7270

0 commit comments

Comments
 (0)