Module Java/Collection

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

all :: (a -> <b> Boolean) -> Collection a -> <b> Boolean

Returns True if all values of the collection satisfy the given predicate. Stops at the first rejected value.

any :: (a -> <b> Boolean) -> Collection a -> <b> Boolean

Returns True if at least one value of the collection satisfies the given predicate. Stops at the first accepted value.

contains :: Collection a -> a -> Boolean

Returns True if the collection contains the given value.

find :: (a -> <b> Boolean) -> Collection a -> <b> Maybe a

The first value of the collection that satisfies the given predicate, or Nothing if there is none.

foldl :: (a -> b -> <c> a) -> a -> Collection b -> <c> a

Folds over the values of the collection starting with the given initial value.

foldl1 :: (a -> a -> <b> a) -> Collection a -> <b> a

Like foldl, but uses the first value of the collection as the initial value.

There is no initial value to fall back on, so foldl1 fails on an empty collection instead of returning a neutral element. Use foldl with an explicit initial value unless the collection is known to be non-empty.

Which value is taken as the initial one depends on the iteration order of the collection, which for a set or a map is not specified. The folded function should therefore be associative and commutative, as min is below.

Example:

> import "JavaBuiltin" as Java
> c = Java.unsafeCoerce [3, 1, 2] :: JC.Collection Integer
> JC.foldl1 min c
1
isEmpty :: Collection a -> Boolean

Returns True if the collection contains no values.

iter :: (a -> <c> b) -> Collection a -> <c> ()

Calls the given function with all values of the collection.

mapFirst :: (a -> <c> Maybe b) -> Collection a -> <c> Maybe b

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

It is a search and a map in one: use it instead of find when the value you are looking for is computed by the same function that decides whether a value matches. See Iterator.mapFirst for a worked example. The iterator it uses is private to the call, so the partial consumption visible there cannot be observed from here.

size :: Collection a -> Integer

The number of values in the collection.

uniqueElement :: Collection a -> a

The only value of the collection.

This is not a way to take the first value out of a collection. It fails with the message Collection is not a singleton. both on an empty collection and on a collection of two or more values, so it is an assertion that the collection has exactly one value. Checking size first is the caller's job.

Example:

> import "JavaBuiltin" as Java
> c = Java.unsafeCoerce ["only"] :: JC.Collection String
> JC.uniqueElement c
"only"