Copyright 2008 - 2009 Tony Morris, Runar Bjarnason, Tom Adams, Brad Clow, Ricky Clarkson, Nick Partridge, Jason Zaugg

This software is released under an open source BSD licence.

fj.data
Class NonEmptyList<A>

java.lang.Object
  extended by fj.data.NonEmptyList<A>
All Implemented Interfaces:
Iterable<A>

public final class NonEmptyList<A>
extends Object
implements Iterable<A>

Provides an in-memory, immutable, singly linked list with total head and tail.


Field Summary
 A head
          The first element of this linked list.
 List<A> tail
          This list without the first element.
 
Method Summary
 NonEmptyList<A> append(NonEmptyList<A> as)
          Appends the given list to this list.
<B> NonEmptyList<B>
bind(F<A,NonEmptyList<B>> f)
          Binds the given function across each element of this list with a final join.
 NonEmptyList<A> cons(A a)
          Prepend the given value to this list.
static
<A> Option<NonEmptyList<A>>
fromList(List<A> as)
          Returns a potential non-empty list from the given list.
 Iterator<A> iterator()
          Returns an iterator for this non-empty list.
<B> NonEmptyList<B>
map(F<A,B> f)
          Maps the given function across this list.
<B> NonEmptyList<B>
mapTails(F<NonEmptyList<A>,B> f)
          Maps the given function across the tails of this list (comonad pattern).
static
<A> F<A,NonEmptyList<A>>
nel()
          Returns a function that puts an element into a non-empty list.
static
<A> NonEmptyList<A>
nel(A head)
          Return a non-empty list with the given value.
static
<A> NonEmptyList<A>
nel(A head, List<A> tail)
          Return a non-empty list with the given head and tail.
 NonEmptyList<NonEmptyList<A>> sublists()
          Returns a NonEmptyList of the sublists of this list.
 NonEmptyList<NonEmptyList<A>> tails()
          Returns a NonEmptyList of the tails of this list.
 Collection<A> toCollection()
          Projects an immutable collection of this non-empty list.
static
<A> F<NonEmptyList<A>,List<A>>
toList_()
          Returns a function that takes a non-empty list to a list.
 List<A> toList()
          Returns a List projection of this list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

head

public final A head
The first element of this linked list.


tail

public final List<A> tail
This list without the first element.

Method Detail

iterator

public Iterator<A> iterator()
Returns an iterator for this non-empty list. This method exists to permit the use in a for-each loop.

Specified by:
iterator in interface Iterable<A>
Returns:
A iterator for this non-empty list.

cons

public NonEmptyList<A> cons(A a)
Prepend the given value to this list.

Parameters:
a - The value to prepend.
Returns:
A non-empty list with an extra element.

append

public NonEmptyList<A> append(NonEmptyList<A> as)
Appends the given list to this list.

Parameters:
as - The list to append.
Returns:
A new list with the given list appended.

map

public <B> NonEmptyList<B> map(F<A,B> f)
Maps the given function across this list.

Parameters:
f - The function to map across this list.
Returns:
A new list after the given function has been applied to each element.

bind

public <B> NonEmptyList<B> bind(F<A,NonEmptyList<B>> f)
Binds the given function across each element of this list with a final join.

Parameters:
f - The function to apply to each element of this list.
Returns:
A new list after performing the map, then final join.

sublists

public NonEmptyList<NonEmptyList<A>> sublists()
Returns a NonEmptyList of the sublists of this list.

Returns:
a NonEmptyList of the sublists of this list.

tails

public NonEmptyList<NonEmptyList<A>> tails()
Returns a NonEmptyList of the tails of this list. A list is considered a tail of itself for the purpose of this function (Comonad pattern).

Returns:
A NonEmptyList of the tails of this list.

mapTails

public <B> NonEmptyList<B> mapTails(F<NonEmptyList<A>,B> f)
Maps the given function across the tails of this list (comonad pattern).

Parameters:
f - The function to map across the tails of this list.
Returns:
The results of applying the given function to the tails of this list, as a NonEmptyList.

toList

public List<A> toList()
Returns a List projection of this list.

Returns:
A List projection of this list.

toCollection

public Collection<A> toCollection()
Projects an immutable collection of this non-empty list.

Returns:
An immutable collection of this non-empty list.

toList_

public static <A> F<NonEmptyList<A>,List<A>> toList_()
Returns a function that takes a non-empty list to a list.

Returns:
A function that takes a non-empty list to a list.

nel

public static <A> NonEmptyList<A> nel(A head,
                                      List<A> tail)
Return a non-empty list with the given head and tail.

Parameters:
head - The first element of the new list.
tail - The remaining elements of the new list.
Returns:
A non-empty list with the given head and tail.

nel

public static <A> NonEmptyList<A> nel(A head)
Return a non-empty list with the given value.

Parameters:
head - The value in the non-empty list.
Returns:
A non-empty list with the given value.

nel

public static <A> F<A,NonEmptyList<A>> nel()
Returns a function that puts an element into a non-empty list.

Returns:
A function that puts an element into a non-empty list.

fromList

public static <A> Option<NonEmptyList<A>> fromList(List<A> as)
Returns a potential non-empty list from the given list. A non-value is returned if the given list is empty.

Parameters:
as - The list to construct a potential non-empty list with.
Returns:
A potential non-empty list from the given list.

Copyright 2008 - 2009 Tony Morris, Runar Bjarnason, Tom Adams, Brad Clow, Ricky Clarkson, Nick Partridge, Jason Zaugg

This software is released under an open source BSD licence.