summaryrefslogtreecommitdiff
path: root/doc/api/libstd/iterutil.txt
blob: a023542fc44bc48ff086fe9470de6e99e72e3569 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{
        title:  libstd
        description:  libstd: Summary
}


Iteration Utilities
----------------

	pkg std =
		type zipiter(@a, @b)
		type reverseiter(@a)
		type enumiter(@a)

		impl iterable zipiter(@a, @b) -> (@a, @b)
		impl iterable enumiter(@a) -> (size, @a)
		impl iterable reverseiter(@a) -> @a

		generic byzip	: (a : @a[:], b : @b[:]	 -> zipiter(@a, @b))
		generic byenum	: (a : @a[:] -> enumiter(@a))
		generic byreverse 	: (sl : @a[:] -> reverseiter(@a))
	;;

Summary
--------

Iteration over sequence is something that turns up regularly. The iteration
utilities provided here simplify a number of common instances of iteration
over collections. They allow for iterating over a zipped sequence,
enumerating a collection of elements, or going over a collection of elements
in reverse.

All of these iterators hold a reference to the original data, and require that
it not be freed until they are finished consuming it. They do not copy the
slices that they iterate over, and all consume O(1) storage.

Zip Iterators
-------------

	impl iterable zipiter(@a, @b) -> (@a, @b)
	generic byzip	: (a : @a[:], b : @b[:]	 -> zipiter(@a, @b))

The zipiter takes two slices, and returns an iterator that will walk over them
pair by pair. So, for example, if you were to call `std.byzip([1,2,3][:],
[4,5,6][:])`, you would iterate through the elements `(1,4), (2,5), (3, 6)`.

Zip Iterators
-------------
	impl iterable enumiter(@a) -> (size, @a)
	generic byenum	: (a : @a[:] -> enumiter(@a))

The enumiter takes a single sequence, and returns the pair of (index, element)
tuples. If you were to call `std.byenum(["hello", "there", "friend"][:])`, you
iterate through the elements `(1, "hello"), (2, "there"), (3, "friend")`.

Reverse Iterators
-----------------

	impl iterable reverseiter(@a) -> @a
	generic byreverse 	: (sl : @a[:] -> reverseiter(@a))


The reverse takes a single slice, and returns the same elements back, but in
the reverse order. Calling `std.byenum(["hello", "there", "friend"][:])`
would iterate in the sequence `"friend", "there", "hello".

Examples
--------

This is a simple zip iterator:

```{runmyr zipiter}
use std
const main = {
	for x in std.byzip([1,2,3][:], [4,5,6][:])
		std.put("{}\n", x)
	;;
}
```

This is a simple enum iterator:

```{runmyr enumiter}
use std
const main = {
	for x in std.byenum(["hello", "world"][:])
		std.put("{}\n", x)
	;;
}
```

```{runmyr reverseiter}
use std
const main = {
	for x in std.byreverse(["hello", "world"][:])
		std.put("{}\n", x)
	;;
}
```