Skip to content

08. Iterator Types

Tom Dodd edited this page Feb 11, 2024 · 7 revisions

Similar to Rust, DSSL has stateful iterators which can be used to traverse iterable elements such as strings and the built-in container types. This corresponds to a combination of iterators and streams in Java. An instance can be created by using the .iter method or the Iter constructor:

"abcdefghijklmnopqrstuvwxyz" .iter  # Instance method
(| 0 1 2 3 4 5 6 7 8 9 |) Iter new  # Iter constructor

Iterators can be used for efficient, lazy traversals without modification of the iterable element. For example, if we had a macro isPrime which could determine whether an integer was a prime number, we could print the squares of each of the prime numbers up to 100 with the following program:

{ println } { dup * } { isPrime } ( 1 100 ) .iter .filter .map .forEach

The .filter method filters out all the elements for which isPrime is false, the .map method squares the resulting elements with dup *, and the .forEach method prints each of the squared primes with println.

Additionally, the iterator produced by the .iter method of iterable elements will be used to produce the elements of foreach loops, destructuring definitions and loops within various built-in methods which accept iterables as arguments.

All built-in methods of iterators are listed here.

Clone this wiki locally