Generators

This section contains a number of useful generators modeled after Standard ML, Haskell, Python, Ruby, JavaScript and other languages.

Finite Generators

fun.range([start, ]stop[, step])
Parameters
  • start (number) – an endpoint of the interval (see below)

  • stop (number) – an endpoint of the interval (see below)

  • step (number) – a step

Returns

an iterator

The iterator to create arithmetic progressions. Iteration values are generated within closed interval [start, stop] (i.e. stop is included). If the start argument is omitted, it defaults to 1 (stop > 0) or to -1 (stop < 0). If the step argument is omitted, it defaults to 1 (start <= stop) or to -1 (start > stop). If step is positive, the last element is the largest start + i * step less than or equal to stop; if step is negative, the last element is the smallest start + i * step greater than or equal to stop. step must not be zero (or else an error is raised). range(0) returns empty iterator.

Examples:

> for _it, v in range(5) do print(v) end
1
2
3
4
5
> for _it, v in range(-5) do print(v) end
-1
-2
-3
-4
-5
> for _it, v in range(1, 6) do print(v) end
1
2
3
4
5
6
> for _it, v in range(0, 20, 5) do print(v) end
0
5
10
15
20
> for _it, v in range(0, 10, 3) do print(v) end
0
3
6
9
> for _it, v in range(0, 1.5, 0.2) do print(v) end
0
0.2
0.4
0.6
0.8
1
1.2
1.4
> for _it, v in range(0) do print(v) end
> for _it, v in range(1) do print(v) end
1
> for _it, v in range(1, 0) do print(v) end
1
0
> for _it, v in range(0, 10, 0) do print(v) end
error: step must not be zero

Infinity Generators

fun.duplicate(...)
Parameters

... (non nil) – objects to duplicate

Returns

an iterator

The iterator returns values over and over again indefinitely. All values that passed to the iterator are returned as-is during the iteration.

Examples:

> each(print, take(3, duplicate('a', 'b', 'c')))
a       b       c
a       b       c
> each(print, take(3, duplicate('x')))
x
x
x
> for _it, a, b, c, d, e in take(3, duplicate(1, 2, 'a', 3, 'b')) do
    print(a, b, c, d, e)
>> end
1       2       a       3       b
1       2       a       3       b
1       2       a       3       b
fun.xrepeat(...)

An alias for duplicate().

fun.replicate(...)

An alias for duplicate().

fun.tabulate(fun)
Parameters

fun (function(n: uint) -> ...) – an unary generating function

Returns

an iterator

The iterator that returns fun(0), fun(1), fun(2), ... values indefinitely.

Examples:

> each(print, take(5, tabulate(function(x)  return 'a', 'b', 2*x end)))
a       b       0
a       b       2
a       b       4
a       b       6
a       b       8
> each(print, take(5, tabulate(function(x) return x^2 end)))
0
1
4
9
16
fun.zeros()
Returns

an iterator

The iterator returns 0 indefinitely.

Examples:

> each(print, take(5, zeros()))
0
0
0
0
0
fun.ones()
Returns

an iterator

The iterator that returns 1 indefinitely.

Example:

> each(print, take(5, ones()))
1
1
1
1
1

Random sampling

fun.rands([n[, m]])
Parameters
  • n (uint) – an endpoint of the interval (see below)

  • m (uint) – an endpoint of the interval (see below)

Returns

an iterator

The iterator returns random values using math.random(). If the n and m are set then the iterator returns pseudo-random integers in the [n, m) interval (i.e. m is not included). If the m is not set then the iterator generates pseudo-random integers in the [0, n) interval. When called without arguments returns pseudo-random real numbers with uniform distribution in the interval [0, 1).

Warning

This iterator is not pure-functional and may not work as expected with some library functions.

Examples:

> each(print, take(10, rands(10, 20)))
19
17
11
19
12
13
14
16
10
11

> each(print, take(5, rands(10)))
7
6
5
9
0

> each(print, take(5, rands()))
0.79420629243124
0.69885246563716
0.5901037417281
0.7532286166836
0.080971251199854