Skip to content

Commit

Permalink
additional list methods, easier nested literals
Browse files Browse the repository at this point in the history
  • Loading branch information
christianschmitz committed Jun 16, 2023
1 parent e231467 commit e38b20a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/lang/builtins/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,20 @@ bytearray_list.join(separator: ByteArray = #) -> ByteArray

### `map`

Transforms each item of a list. The resulting list item type is a type parameter of this method: `NewItemType`.
Transforms each item of a list.

```helios
list.map[NewItemType](mapper: (ItemType) -> NewItemType) -> []NewItemType
```

### `map_option`

Transforms and filters a list in one call.

```helios
list.map_option[NewItemType](mapper: (ItemType) -> Option[NewItemType]) -> []NewItemType
```

### `prepend`

Creates a new list by prepending an item to the old list.
Expand All @@ -276,6 +284,14 @@ list.prepend(item: ItemType) -> []ItemType
list.serialize() -> ByteArray
```

### `set`

Creates a new list, replacing an item at an `index`. Throws an error if the `index` is out of range.

```helios
list.set(index: Int, item: ItemType) -> []ItemType
```

### `sort`

Sorts the list using insertion sort.
Expand All @@ -284,6 +300,14 @@ Sorts the list using insertion sort.
list.sort((a: ItemType, b: ItemType) -> Bool) -> []ItemType
```

### `split_at`

Splits the list at an `index`. Throws an error if the `index` is out of range.

```helios
list.split_at(index: Int) -> ([]ItemType, []ItemType)
```

### `sum`

Only defined for `[]Int` and `[]Real`.
Expand Down
19 changes: 17 additions & 2 deletions src/lang/container-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ x: Int = some_ints.get(2); ... // x == 3
More information about lists can be found [here](./builtins/list.md).


## `Map`

A `Map` in Helios is internally represented as a list of key-value pairs. Both key and value can have any type except a function type. Uniqueness of keys isn't guaranteed.
Expand Down Expand Up @@ -66,4 +65,20 @@ If you expect `Some`, you can assign, and even destructure, using the correct ty
Option[Int]::Some{my_int} = option; ...
```

More information about `Option` can be found [here](./builtins/option.md).
More information about `Option` can be found [here](./builtins/option.md).

## Nested literal constructors

If a literal List, `Map`, or `Option` contains other literal constructors, the types of those literal constructors can be omitted.

```helios
struct Pair {
a: Int
b: Int
}
...
list = []Pair{{0, 0}, {1, 1}, {2, 2}};
map = Map[Pair]Pair{{0, 0}: {0, 0}, {1, 1}: {1, 1}, {2, 2}: {2, 2}};
nested_list = [][]Pair{{{0, 0}, {1, 1}, {2, 2}}, {{0, 0}, {1, 1}, {2, 2}}};
option = Option[Pair]::Some{{0, 0}}
```

0 comments on commit e38b20a

Please sign in to comment.