Module Iterator

This module is undocumented. This is a list of its definitions.

filter :: (a -> <b> Boolean) -> Iterator.T a -> <Proc,b> ()

Removes from the underlying collection all remaining values of the iterator that do not satisfy the given predicate.

Despite the name this is not Prelude.filter. It builds no new collection and returns (): it destructively removes the rejected values from the collection behind the iterator. The predicate says which values to keep. MSet.filterInPlace is this function applied to the iterator of a set.

The underlying collection must support removal, so an SCL list cannot be filtered this way.

Example:

> import "Iterator" as Iterator
> import "JavaBuiltin" as Java
> s = MSet.fromList [1, 2, 3, 4, 5]
> Iterator.filter (\x -> mod x 2 == 0) (Iterator.iterator (Java.unsafeCoerce s :: Iterator.Iterable Integer))
> sort (MSet.toList s)
[2, 4]

The values are sorted only for printing: the iteration order of an MSet is not specified.

fold :: (a -> b -> <c> a) -> a -> Iterator.T b -> <Proc,c> a

Folds over the remaining values of the iterator starting with the given initial value.

hasNext :: Iterator.T a -> <Proc> Boolean

Returns True if the iterator has more values.

iter :: (a -> <c> b) -> Iterator.T a -> <Proc,c> ()

Calls the given function with all remaining values of the iterator.

iterB :: (a -> <b> Boolean) -> Iterator.T a -> <Proc,b> Boolean

Calls the given function with the remaining values of the iterator until it returns False. Returns False if the iteration was interrupted this way and True if the function accepted all values.

The result means "was not interrupted", not "found something": it is True exactly when the function accepted every remaining value. See MMap.iterB for a worked example of the same pattern.

iterI :: (Integer -> a -> <c> b) -> Iterator.T a -> <Proc,c> ()

Calls the given function with all remaining values of the iterator, giving also a running index as the first parameter.

iterator :: Iterable a -> <Proc> Iterator.T a

A new iterator over the values of the given iterable.

Example:

> import "Iterator" as Iterator
> import "JavaBuiltin" as Java
> it = Iterator.iterator (Java.unsafeCoerce [1, 2, 3] :: Iterator.Iterable Integer)
> Iterator.fold (+) 0 it
6

An SCL list is a Java Iterable at run time, which is what makes viewing one as an Iterable safe here.

mapFirst :: (a -> <c> Maybe b) -> Iterator.T a -> <Proc,c> Maybe b

Applies the given function to the remaining values of the iterator until it returns Just and returns that result. Returns Nothing if the function returns Nothing for all of them.

It is a search and a map in one, and it consumes the iterator only up to and including the value it accepted, so the rest of the iterator can still be read afterwards. Java/Iterator.mapFirst and Java/Collection.mapFirst are the same function.

Example:

> import "Iterator" as Iterator
> import "JavaBuiltin" as Java
> l = ArrayList.fromList [1, 2, 3, 4]
> it = Iterator.iterator (Java.unsafeCoerce l :: Iterator.Iterable Integer)
> Iterator.mapFirst (\x -> if x > 2 then Just (x*10) else Nothing) it
Just 30
> Iterator.fold (+) 0 it
4

The last command folds what is left of the iterator, which is only the value 4: mapFirst consumed 1, 2 and 3.

remove :: Iterator.T a -> <Proc> ()

Removes from the underlying collection the value that was returned by the last call of next.

The value to remove is not a parameter: remove always removes the value the iterator is standing on. Calling it before the first next fails, as does calling it twice for the same value or calling it on an iterator over a collection that does not support removal, such as an SCL list.

Example:

> import "Iterator" as Iterator
> import "JavaBuiltin" as Java
> l = ArrayList.fromList ["a", "b", "c"]
> it = Iterator.iterator (Java.unsafeCoerce l :: Iterator.Iterable String)
> Iterator.next it
"a"
> Iterator.remove it
> ArrayList.freeze l
["b", "c"]