util.fn
Class FnIterable<T>

java.lang.Object
  extended by util.fn.FnIterable<T>
Type Parameters:
T - type of elements of the sequence
All Implemented Interfaces:
Iterable<T>

public class FnIterable<T>
extends Object
implements Iterable<T>

Functional programming like sequences. This class provides map-like functionality.


Method Summary
static
<T> FnIterable<T>
from(Iterable<T> it)
          Creates a new functional sequence from an Iterable object
 Iterator<T> iterator()
           
<U> FnIterable<U>
map(Lambda<T,U> fn)
          Maps function to sequence
<U> U
reduce(Lambda2<U,T,U> fn, U initial)
          Reduces this sequence to a value.
 List<T> toList()
           
<U> FnIterable<Pair<T,U>>
zip(FnIterable<U> o)
          Produces a sequence of pairs from two sequences.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

from

public static <T> FnIterable<T> from(Iterable<T> it)
Creates a new functional sequence from an Iterable object

Type Parameters:
T - type of elements of the sequence
Parameters:
it - the iterable object
Returns:
a functional sequence view of the iterable object

zip

public final <U> FnIterable<Pair<T,U>> zip(FnIterable<U> o)
Produces a sequence of pairs from two sequences.
  {a1, a2, a3, ...}.zip({b1, b2, b3, ...}) ==> {(a1, b1), (a2, b2), (a3, b3), ...}
 

Type Parameters:
U - type of elements of the argument sequence
Parameters:
o - argument sequence to zip with
Returns:
sequence of pairs combining this and argument sequence

map

public final <U> FnIterable<U> map(Lambda<T,U> fn)
Maps function to sequence
  {a1, a2, a3, ...}.map(fn) ==> {fn(a1), fn(a2), fn(a3), ...}
 

Type Parameters:
U - type of elements in result sequence
Parameters:
fn - function from elements in this sequence to elements of result sequence
Returns:
a sequence of the return values of the function

reduce

public final <U> U reduce(Lambda2<U,T,U> fn,
                          U initial)
Reduces this sequence to a value.
  {}.reduce(fn, initial)  ==> initial
  {a1, a2, a3, ...}.reduce(fn, initial) ==> fn(... fn(fn(fn(initial, a1), a2), a3) ...)
 

Type Parameters:
U - type of the resulting value
Parameters:
fn - function to reduce sequence
initial - initial value to pass reducing function
Returns:
resulting value from applying function over sequence

toList

public final List<T> toList()
Returns:
a copy of this sequence as a list

iterator

public Iterator<T> iterator()
Specified by:
iterator in interface Iterable<T>