Skip to content

Commit

Permalink
docs: add map info
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhgray committed Oct 27, 2024
1 parent 7367000 commit 7b39fab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ print percentage(20, 28);
- [Contracts (Preconditions, Postconditions and Assertions)](#contracts-preconditions-postconditions-and-assertions)
- [Unit Tests](#unit-tests)
- [Arrays](#arrays)
- [Maps](#maps)
- [Conditionals](#conditionals)
- [Loops](#loops)
- [Built-in Functions](#built-in-functions)
Expand All @@ -60,7 +61,7 @@ Tahini currently implements:
- [x] **Conditionals**: If-else statements for decision-making.
- [x] **Functions**: First class citizens of Tahini. Define and call reusable blocks of code, with support for contracts (`precondition`, `postcondition`, and `assertion`).
- [ ] **Classes**: Object-oriented features to group variables and methods (halted in favour of a lean towards a functional paradigm).
- [ ] **Advanced Data Structures**: Support for lists, maps, and other data structures (in progress).
- [x] **Advanced Data Structures**: Basic support for lists, maps, and other data structures.
- [ ] **Error Handling**: Support for user-defined exceptions and error handling (in progress).
- [x] **Stack Traces**: Detailed error messages with line numbers and function names.
- [x] **Unit Tests**: Write test blocks directly in the source file to validate code correctness.
Expand Down Expand Up @@ -317,6 +318,15 @@ fun remove(arr, index) {
}
```

### Maps

Maps are implemented as a HashMap. You can create a map via `{...}` syntax, and access elements using the `[]` operator. Maps can contain any object keys or values.

```
var map = {"key": "value", 1: 2, "fib": fib};
print map["key"]; // value
```

### Conditionals

```
Expand Down
15 changes: 10 additions & 5 deletions test.tah
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
var dict = { "key1": 1, "key2": 2, "key3": 3, true: false, nil:5 };
print dict["key1"];
print dict[true];
print dict[nil];

fun isEven(n) {
while (n >= 2) {
n = n - 2;
}
return n == 0;
}

var dictwithfunc = { "key1": 1, "key2": 2, "key3": 3, true: false, nil:5, "fib": isEven, isEven: isEven };
print dictwithfunc["key1"];
print dictwithfunc[true];
print dictwithfunc[nil];
print dictwithfunc["fib"](4);
print dictwithfunc["fib"];
print dictwithfunc[isEven];
print dictwithfunc[isEven](5);
print dictwithfunc["isEven"](4);

print len("Hello");
var str = "hello";
print len(str);
Expand Down

0 comments on commit 7b39fab

Please sign in to comment.