- Commands
- Data
- Diagram
- Document
- Dropins
- Experimental
- Expressions
- Extras
- HTTP
- Java
- Junit
- SCL
- SWT
- Server
- Simantics
- Spreadsheet
- StandardLibrary
- Structural
- Tutorial
- UI
- Visualization
|
Module IterN
This module is undocumented. This is a list of its definitions.
allN :: (Integer -> <a> Boolean) -> Integer -> <a> Boolean
allN f n returns True if f i is True for all i in 0, ..., n-1. Stops at the first rejected index. allN f 0 is True. See anyN for an example.
anyN :: (Integer -> <a> Boolean) -> Integer -> <a> Boolean
anyN f n returns True if f i is True for some i in 0, ..., n-1.
Stops at the first accepted index, so f is not called for the rest.
anyN f 0 is False.
Example:
Extra.anyN (\i -> i*i > 5) 4 = True
concatMapN :: (Integer -> <b> [a]) -> Integer -> <b> [a]
concatMapN f n concatenates the lists f 0, ..., f (n-1) into a single
list.
It is implemented as sum (mapN f n). That works because lists are Additive:
+ on two lists is concatenation and zero is the empty list, so summing a
list of lists concatenates them.
Examples:
> Extra.concatMapN (\i -> [i, -i]) 3
[0, 0, 1, -1, 2, -2]
> sum [[1, 2], [3]]
[1, 2, 3]
filterN :: (Integer -> <a> Boolean) -> Integer -> <a> [Integer]
filterN f n returns those integers of 0, ..., n-1 that the predicate f
accepts.
The result is a list of indices, not of elements of anything: the values
being filtered are the integers of the range itself. This is the usual way to
find the positions of a sequence at which something holds, and it is not the
list filter, which returns the elements.
Example:
> l = [10, 5, 20, 3]
> Extra.filterN (\i -> l!i > 8) (length l)
[0, 2]
> filter (\x -> x > 8) l
[10, 20]
foldlN :: (a -> Integer -> <b> a) -> a -> Integer -> <b> a
foldlN f initial n folds f over the integers 0, ..., n-1 starting with
initial.
The folder is applied as f accumulator index: the accumulator comes first and
the index second, and it is the index that varies, not an element of any
collection. This is the workhorse behind most of the Vector module, where a
fold over a vector is a foldlN over its indices.
foldlN f initial 0 returns initial unchanged.
Examples:
> Extra.foldlN (\sum i -> sum + i*i) 0 4
14
> v = vector [1, 2, 3, 4]
> Extra.foldlN (\sum i -> sum + v!i) 0 (length v)
10
iterN :: (Integer -> <b> a) -> Integer -> <b> ()
iterN f n calls f with all integers 0, ..., n-1, in that order, and
returns ().
Like every function in this module the function comes first and the bound
second, and the bound is exclusive: the range is 0, ..., n-1, never n.
iterN f 0 does nothing. A negative n does not terminate: the loop ends
when the counter equals n and the counter only ever counts upwards. The same
is true of every other function here.
Example:
> Extra.iterN print 0
> Extra.iterN print 3
0
1
2
mapFirstN :: (Integer -> <b> Maybe a) -> Integer -> <b> Maybe a
mapFirstN f n applies f to the integers 0, ..., n-1 until it returns
Just and returns that result. Returns Nothing if f returns Nothing for
all of them.
The indices are tried in increasing order and the search stops at the first
Just, so f is not called for the remaining indices.
Example:
> l = [10, 5, 20, 3]
> Extra.mapFirstN (\i -> if l!i > 8 then Just (l!i) else Nothing) (length l)
Just 10
> Extra.mapFirstN (\i -> if i > 10 then Just i else Nothing) 3
Nothing
mapMaybeN :: (Integer -> <b> Maybe a) -> Integer -> <b> [a]
mapMaybeN f n applies f to the integers 0, ..., n-1 and collects the
contents of the Just results into a list.
This is a filter and a map in one step. An index for which f returns
Nothing is dropped from the result, not preserved as a Nothing, so the
result is usually shorter than n and its positions no longer correspond to
the indices that produced them.
Example:
> Extra.mapMaybeN (\i -> if mod i 3 == 0 then Just (i*i) else Nothing) 7
[0, 9, 36]
mapN :: (Integer -> <b> a) -> Integer -> <b> [a]
mapN f n returns the list [f 0, f 1, ..., f (n-1)]. The result has exactly
n elements; mapN f 0 is the empty list.
Example:
Extra.mapN (\i -> i*i) 5 = [0, 1, 4, 9, 16]
|