Added iterators

Iterators are an unbounded sequence of elements that can only be consumed one-by-one.
This commit is contained in:
Leon Mika 2025-01-30 22:15:38 +11:00
parent badb3b88ba
commit 142abeb990
11 changed files with 614 additions and 62 deletions

View file

@ -74,14 +74,17 @@ Returns the length of COL. If COL is a list or hash, COL will be the number of
elements. If COL is a string, COL will be the string's length. All other values will
return a length of 0.
If COL is an iterator, `len` will consume the values of the iterator and return the number of items consumed.
### map
```
map COL BLOCK
```
Returns a new list of elements mapped from COL according to the result of BLOCK. COL can be any listable data
structure, however the result will always be a concrete list.
Returns a new list of elements mapped from COL according to the result of BLOCK. COL can be any list or hash
with the result being a concrete list. COL can be an iterator, in which case the result will be an iterator
which will call BLOCK for every consumed value.
```
map [1 2 3] { |x| str $x | len }
@ -107,7 +110,7 @@ reduce COL [INIT] BLOCK
Returns the result of reducing the elements of COL with the passed in block.
BLOCK will receive at least two argument, with the current value of the accumulator always being the last argument.
If COL is a list, the arguments will be _|element accumulator|_, and if COL is a hash, the arguments will be
If COL is a list or iterator, the arguments will be _|element accumulator|_, and if COL is a hash, the arguments will be
_|key value accumulator|_.
The block result will be set as the value of the accumulator for the next iteration. Once all elements are process

View file

@ -5,4 +5,5 @@ Modules of the standard library:
- [core](/mod/core): Core builtins
- [csv](/mod/csv): Functions for operating over CSV data.
- [fs](/mod/fs): File system functions
- [itrs](/mod/itrs): Iterator utilities
- [os](/mod/os): Operating system functions

20
_docs/mod/itrs.md Normal file
View file

@ -0,0 +1,20 @@
---
---
# Iterator Builtins
### from
```
itrs:from LIST
```
Returns an iterator which will step through the elements of LIST.
### to-list
```
lists:to-list ITR
```
Consume the elements of the iterator ITR and return the elements as a list.