Orbital library

orbital.logic.functor
Class Functionals

java.lang.Object
  extended by orbital.logic.functor.Functionals
Direct Known Subclasses:
Functionals

public class Functionals
extends java.lang.Object

Provides important compositional functionals for functors. For that, this module class contains several static methods that work like Functionals, i.e. high-order functions that have a Function-object in their argument or return type signature.

The compose methods in this class return objects that are instances of Functor.Composite. By using this interface, notation manipulation and component extraction can be performed. Since some section types like a binary function composed with two void functions are not exported as an interface, decomposition may only be accessible with a dynamic type cast to Functor.Composite.

Note: some static methods in this class may get a true functional counterpart, that is functions that take functions as arguments and transform them into new functions. However, while templates are not yet widely available in Java today, this will only increase the number of type casts required.

Author:
André Platzer
See Also:
Functor, Function, BinaryFunction, Predicate, BinaryPredicate, Collection, Iterator, Functionals, Facade Pattern, Substitutions.lambda, Setops

Nested Class Summary
static class Functionals.Anamorphism
          Anamorphism recursion functional scheme (lense).
static class Functionals.Catamorphism
          Catamorphism recursion functional scheme (banana).
static class Functionals.Hylomorphism
          Hylomorphism recursion functional scheme (envelope).
static class Functionals.Paramorphism
          Paramorphism recursion functional scheme (barbed wire).
 
Field Summary
static BinaryFunction apply
          apply ·(·): Map(A,B)×A→B; (f,x) ↦ f(x).
static BinaryFunction compose
          compose ∘: Map(B,C)×Map(A,B)→Map(A,C); (f,g) ↦ f ∘ g := (x↦f(g(x))).
static Functionals functionals
          Class alias object.
 
Constructor Summary
protected Functionals()
          prevent instantiation - module class
 
Method Summary
static BinaryFunction asFunction(BinaryPredicate p)
          converts a predicate to a function.
static Function asFunction(Predicate p)
          converts a predicate to a function.
static VoidFunction asFunction(VoidPredicate p)
          converts a predicate to a function.
static java.lang.Object banana(java.lang.Object c, BinaryFunction f, java.util.Iterator a)
          banana (|c,f|) a.
static int barbedwire(int b, BinaryFunction f, int a)
           
static java.lang.Object barbedwire(java.lang.Object b, BinaryFunction f, java.util.Iterator a)
          barbedwire {|b,f|} a.
static Function bind(BinaryFunction f)
          Binds both arguments of a BinaryFunction together.
static Predicate bind(BinaryPredicate P)
          Binds both arguments of a BinaryPredicate together.
static VoidFunction bind(Function f, java.lang.Object a)
          Binds the argument of a Function to a fixed value.
static VoidPredicate bind(Predicate P, java.lang.Object a)
          Binds the argument of a Predicate to a fixed value.
static Function bindFirst(BinaryFunction f, java.lang.Object x)
          Binds the first argument of a BinaryFunction to a fixed value.
static Predicate bindFirst(BinaryPredicate P, java.lang.Object x)
          Binds the first argument of a BinaryPredicate to a fixed value.
static Function bindSecond(BinaryFunction f, java.lang.Object y)
          Binds the second argument of a BinaryFunction to a fixed value.
static Predicate bindSecond(BinaryPredicate P, java.lang.Object y)
          Binds the second argument of a BinaryPredicate to a fixed value.
static BinaryFunction.Composite compose(BinaryFunction f, BinaryFunction g, BinaryFunction h)
          compose: Map(B1×B2,C)×(Map(A1×A2,B1)×Map(A1×A2,B2))→Map(A1×A2,C); (f,g,h) ↦ f ∘ (g × h) := f(g,h) .
static Function.Composite compose(BinaryFunction f, Function g, Function h)
          compose: Map(B1×B2,C)×(Map(A,B1)×Map(A,B2))→Map(A,C); (f,g,h) ↦ f ∘ (g × h) := f(g,h) .
static VoidFunction.Composite compose(BinaryFunction f, VoidFunction g, VoidFunction h)
          compose: Map(B1×B2,C)×(Map({()},B1)×Map({()},B2))→Map({()},C); (f,g,h) ↦ f ∘ (g × h) := f(g,h) .
static BinaryPredicate.Composite compose(BinaryPredicate P, BinaryFunction g, BinaryFunction h)
          compose: ℘(B1×B2)×(Map(A1×A2,B1)×Map(A1×A2,B2))→℘(A1×A2); (P,g,h) ↦ P ∘ (g × h) := P(g,h) .
static Predicate.Composite compose(BinaryPredicate P, Function g, Function h)
          compose: ℘(B1×B2)×(Map(A,B1)×Map(A,B2))→℘(A); (P,g,h) ↦ P ∘ (g × h) := P(g,h) .
static VoidPredicate.Composite compose(BinaryPredicate P, VoidFunction g, VoidFunction h)
          compose: ℘(B1×B2)×(Map({()},B1)×Map({()},B2))→℘({()}); (P,g,h) ↦ P ∘ (g × h) := P(g,h) .
static Function.Composite compose(Function f, Function g)
          compose: Map(B,C)×Map(A,B)→Map(A,C); (f,g) ↦ f ∘ g := f(g).
static VoidFunction.Composite compose(Function f, VoidFunction g)
          compose: Map(B,C)×Map({()},B)→Map({()},C); (f,g) ↦ f ∘ g := f(g).
static Predicate.Composite compose(Predicate P, Function g)
          compose: ℘(B)×Map(A,B)→℘(A); (P,g) ↦ P ∘ g := P(g).
static VoidPredicate.Composite compose(Predicate P, VoidFunction g)
          compose: ℘(B)×Map({()},B)→℘({()}); (P,g) ↦ P ∘ g := P(g).
static Function cross(Function f, Function g)
          cross: Map(A,B1)×Map(A,B2)→Map(A,B1×B2); (f,g) ↦ f × g := (f,g).
static Function curry(BinaryFunction f)
          Currys a binary function to a function mapping to a function.
static java.lang.Object envelope(java.lang.Object c, BinaryFunction f, Function g, Predicate p, java.lang.Object a)
          envelope [[(c,f),(g,p)]] a.
static java.lang.Object fixedPoint(Function f, java.lang.Object x)
           
static java.lang.Object fixedPoint(Function f, java.lang.Object x, int maxIteration)
          fixedPoint starts with an object, then applies f repeatedly until the result no longer changes.
static java.lang.Object foldLeft(BinaryFunction f, java.lang.Object c, java.util.Collection a)
           
static java.lang.Object foldLeft(BinaryFunction f, java.lang.Object c, java.util.Iterator a)
           
static java.lang.Object foldLeft(BinaryFunction f, java.lang.Object c, java.lang.Object[] a)
          Folds a list with a BinaryFunction.
static java.lang.Object foldRight(BinaryFunction f, java.lang.Object c, java.util.Collection a)
           
static java.lang.Object foldRight(BinaryFunction f, java.lang.Object c, java.util.Iterator a)
           
static java.lang.Object foldRight(BinaryFunction f, java.lang.Object c, java.util.List a)
          efficient foldRight for lists.
static java.lang.Object foldRight(BinaryFunction f, java.lang.Object c, java.lang.Object[] a)
          Folds a list with a BinaryFunction.
static Functor.Composite genericCompose(BinaryFunction f, java.lang.Object g, java.lang.Object h)
          generic compose calls the compose function appropriate for the type of g and h.
static Functor.Composite genericCompose(BinaryPredicate P, java.lang.Object g, java.lang.Object h)
          generic compose calls the compose function appropriate for the type of g and h.
static Functor.Composite genericCompose(Function f, java.lang.Object g)
          Deprecated. Use compose instead.
static java.util.List lense(Function g, Predicate p, java.lang.Object b)
          lense |(g,p)| b.
static BinaryFunction listable(BinaryFunction f)
          Get a listable function automatically mapping itself over lists (recursively).
static Function listable(Function f)
          Get a listable function automatically mapping itself over lists (recursively).
static java.util.Collection map(BinaryFunction f, java.util.Collection x, java.util.Collection y)
          Maps two lists of arguments with a BinaryFunction.
static java.util.Iterator map(BinaryFunction f, java.util.Iterator x, java.util.Iterator y)
           
static java.util.ListIterator map(BinaryFunction f, java.util.ListIterator x, java.util.ListIterator y)
           
static java.util.List map(BinaryFunction f, java.util.List x, java.util.List y)
           
static java.lang.Object[] map(BinaryFunction f, java.lang.Object[] x, java.lang.Object[] y)
          Maps two lists of arguments with a BinaryFunction.
static java.util.Set map(BinaryFunction f, java.util.Set x, java.util.Set y)
           
static java.util.SortedSet map(BinaryFunction f, java.util.SortedSet x, java.util.SortedSet y)
           
static java.util.Collection map(Function f, java.util.Collection a)
          Maps a list of arguments with a function.
static java.util.Iterator map(Function f, java.util.Iterator a)
           
static java.util.List map(Function f, java.util.List a)
           
static java.util.ListIterator map(Function f, java.util.ListIterator a)
           
protected static java.lang.Object map(Function f, java.lang.Object a)
          Maps a list-like generalized iteratable list of arguments with a function.
static java.lang.Object[] map(Function f, java.lang.Object[] a)
          Maps a list of arguments with a function.
static java.util.Set map(Function f, java.util.Set a)
           
static java.util.SortedSet map(Function f, java.util.SortedSet a)
           
protected static void mapInto(BinaryFunction f, java.util.Iterator x, java.util.Iterator y, java.util.ListIterator t)
          map implementation writing values into the target iterator t.
protected static void mapInto(Function f, java.util.Iterator a, java.util.ListIterator r)
          map implementation writing values into the target iterator t.
static Function nest(Function f, int n)
          Nests a function n times within itself.
static BinaryFunction onFirst(Function f)
          Applies a function on the first argument, ignoring the second.
static BinaryPredicate onFirst(Predicate p)
          Applies a predicate on the first argument, ignoring the second.
static BinaryFunction onSecond(Function f)
          Applies a function on the second argument, ignoring the first.
static BinaryPredicate onSecond(Predicate p)
          Applies a predicate on the second argument, ignoring the first.
static Function onVoid(VoidFunction f)
          Applies a function on the void argument, ignoring all arguments.
static Predicate onVoid(VoidPredicate p)
          Applies a predicate on the void argument, ignoring all arguments.
protected static Function paramorphism(int b, BinaryFunction f)
          Paramorphism recursion functional operator (barbed wire).
static BinaryFunction swap(BinaryFunction f)
          Swaps the two arguments of a BinaryFunction.
static BinaryPredicate swap(BinaryPredicate P)
          Swaps the two arguments of a BinaryPredicate (inverse).
static BinaryFunction uncurry(Function f)
          Uncurrys a curried function to a binary function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

functionals

public static final Functionals functionals
Class alias object.

To alias the methods in this class, use an idiom like

 // alias object
 Functionals F = Functionals.functionals;
 // use alias
 Function f = (Function) F.compose(f1,f2);
 // instead of the long form
 Function f = (Function) Functionals.compose(f1,f2);
 


compose

public static final BinaryFunction compose
compose ∘: Map(B,C)×Map(A,B)→Map(A,C); (f,g) ↦ f ∘ g := (x↦f(g(x))).

compose functional ∘ calls the compose function appropriate for the type of g. Valid types for g are Function, VoidFunction and non-functor Object. In the latter case, composition is done using a constant function.

For example, the following call creates a macro command function

 List fs = Arrays.asList(new Function[] {f1, f2, ..., fn});
 // f = (|id,∘|) fs
 Function f = Functionals.banana(Functions.id, Functionals.compose, fs.iterator());
 

See Also:
genericCompose(Function,Object)

apply

public static final BinaryFunction apply
apply ·(·): Map(A,B)×A→B; (f,x) ↦ f(x).

apply is the inverse operator of λ.

See Also:
Facade Pattern
Constructor Detail

Functionals

protected Functionals()
prevent instantiation - module class

Method Detail

cross

public static Function cross(Function f,
                             Function g)
cross: Map(A,B1)×Map(A,B2)→Map(A,B1×B2); (f,g) ↦ f × g := (f,g).

Another notation for the function cross-product is f × g = f ⊗ g = f || g.

Returns:
x↦f × g (x) = (f(x), g(x)) as a Pair.

compose

public static Function.Composite compose(Function f,
                                         Function g)
compose: Map(B,C)×Map(A,B)→Map(A,C); (f,g) ↦ f ∘ g := f(g).

Returns:
x ↦ (f∘g)(x) = f(g(x)).
See Also:
Facade Pattern, compose

compose

public static BinaryFunction.Composite compose(BinaryFunction f,
                                               BinaryFunction g,
                                               BinaryFunction h)
compose: Map(B1×B2,C)×(Map(A1×A2,B1)×Map(A1×A2,B2))→Map(A1×A2,C); (f,g,h) ↦ f ∘ (g × h) := f(g,h) .

Returns:
(x,y) ↦ f(g(x,y),h(x,y)).
See Also:
Facade Pattern

compose

public static Function.Composite compose(BinaryFunction f,
                                         Function g,
                                         Function h)
compose: Map(B1×B2,C)×(Map(A,B1)×Map(A,B2))→Map(A,C); (f,g,h) ↦ f ∘ (g × h) := f(g,h) .

Returns:
x ↦ f(g(x),h(x)).
See Also:
Facade Pattern

compose

public static VoidFunction.Composite compose(Function f,
                                             VoidFunction g)
compose: Map(B,C)×Map({()},B)→Map({()},C); (f,g) ↦ f ∘ g := f(g).

Returns:
() ↦ (f ∘ g) () = f(g()).
See Also:
Facade Pattern

compose

public static VoidFunction.Composite compose(BinaryFunction f,
                                             VoidFunction g,
                                             VoidFunction h)
compose: Map(B1×B2,C)×(Map({()},B1)×Map({()},B2))→Map({()},C); (f,g,h) ↦ f ∘ (g × h) := f(g,h) .

Returns:
() ↦ f(g(),h()).
See Also:
Facade Pattern

compose

public static Predicate.Composite compose(Predicate P,
                                          Function g)
compose: ℘(B)×Map(A,B)→℘(A); (P,g) ↦ P ∘ g := P(g).

Returns:
P ∘ g = λx. P(g(x)) = {x∈A ¦ P(g(x))}.
See Also:
Facade Pattern

compose

public static BinaryPredicate.Composite compose(BinaryPredicate P,
                                                BinaryFunction g,
                                                BinaryFunction h)
compose: ℘(B1×B2)×(Map(A1×A2,B1)×Map(A1×A2,B2))→℘(A1×A2); (P,g,h) ↦ P ∘ (g × h) := P(g,h) .

Returns:
P ∘ (g × h) = λx,y. P(g(x,y),h(x,y)) = {(x,y)∈A1×A2 ¦ P(g(x,y),h(x,y))}.
See Also:
Facade Pattern

compose

public static Predicate.Composite compose(BinaryPredicate P,
                                          Function g,
                                          Function h)
compose: ℘(B1×B2)×(Map(A,B1)×Map(A,B2))→℘(A); (P,g,h) ↦ P ∘ (g × h) := P(g,h) .

Returns:
P ∘ (g × h) = λx. P(g(x),h(x)) = {x∈A ¦ P(g(x),h(x))}.
See Also:
Facade Pattern, Functionals.BinaryCompositePredicate

compose

public static VoidPredicate.Composite compose(Predicate P,
                                              VoidFunction g)
compose: ℘(B)×Map({()},B)→℘({()}); (P,g) ↦ P ∘ g := P(g).

Returns:
λ(). P(g()).
See Also:
Facade Pattern

compose

public static VoidPredicate.Composite compose(BinaryPredicate P,
                                              VoidFunction g,
                                              VoidFunction h)
compose: ℘(B1×B2)×(Map({()},B1)×Map({()},B2))→℘({()}); (P,g,h) ↦ P ∘ (g × h) := P(g,h) .

Returns:
λ(). P(g(),h()).
See Also:
Facade Pattern, Functionals.BinaryCompositeVoidPredicate

genericCompose

public static Functor.Composite genericCompose(Function f,
                                               java.lang.Object g)
Deprecated. Use compose instead.

generic compose calls the compose function appropriate for the type of g.

See Also:
Facade Pattern, compose
Preconditions:
g is "composable"

genericCompose

public static Functor.Composite genericCompose(BinaryFunction f,
                                               java.lang.Object g,
                                               java.lang.Object h)
generic compose calls the compose function appropriate for the type of g and h. Valid types for g and h are VoidFunction, Function, BinaryFunction and non-functor Object. In the latter case, composition is done using a constant function.

See Also:
Facade Pattern
Preconditions:
g and h are "compatible"

genericCompose

public static Functor.Composite genericCompose(BinaryPredicate P,
                                               java.lang.Object g,
                                               java.lang.Object h)
generic compose calls the compose function appropriate for the type of g and h.

See Also:
Facade Pattern
Preconditions:
g and h are "compatible"

curry

public static Function curry(BinaryFunction f)
Currys a binary function to a function mapping to a function.

curry: Map(A1×A2,B)→Map(A1,Map(A2,B)); f ↦ (x↦f(x,·)) = (x↦(y↦f(x,y))).

Returns:
x↦f(x,·)
See Also:
uncurry(Function), bindFirst(BinaryFunction,Object)

uncurry

public static BinaryFunction uncurry(Function f)
Uncurrys a curried function to a binary function.

uncurry: Map(A1,Map(A2,B))→Map(A1×A2,B); f ↦ ((x,y)↦f(x)(y)).

Returns:
(x,y)↦f(x)(y)
See Also:
curry(BinaryFunction)

bindFirst

public static Function bindFirst(BinaryFunction f,
                                 java.lang.Object x)
Binds the first argument of a BinaryFunction to a fixed value.

bindFirst: Map(A1×A2,B)→Map(A2,B); f↦f(x,·). The unary left-adjoint got from f by currying and single application.

Returns:
f(x,·):A2→B; y ↦ f(y) := f(x, y)
See Also:
curry(BinaryFunction)

bindFirst

public static Predicate bindFirst(BinaryPredicate P,
                                  java.lang.Object x)
Binds the first argument of a BinaryPredicate to a fixed value.

bindFirst: ℘(A1×A2)→℘(A2); P↦P(x,·). The unary left-adjoint got from P by "currying".

Returns:
P(x,·) := λy. P(x,y) = {y∈A2 ¦ P(x, y)}

bindSecond

public static Function bindSecond(BinaryFunction f,
                                  java.lang.Object y)
Binds the second argument of a BinaryFunction to a fixed value.

bindSecond: Map(A1×A2,B)→Map(A1,B); f↦f(·,y). The unary right-adjoint got from f by "currying".

Returns:
f(·,y):A1→B; x ↦ f(x) := f(x, y)

bindSecond

public static Predicate bindSecond(BinaryPredicate P,
                                   java.lang.Object y)
Binds the second argument of a BinaryPredicate to a fixed value.

bindSecond: ℘(A1×A2)→℘(A1); P↦P(·,y). The unary right-adjoint got from P by "currying".

Returns:
P(·,y) := λx.P(x,y) = {x∈A1 ¦ P(x, y)}

bind

public static VoidFunction bind(Function f,
                                java.lang.Object a)
Binds the argument of a Function to a fixed value.

bind: Map(A,B)→Map({()},B); f↦f(a). The void adjoint got from f by "currying" Every evaluation of a function can be seen as a sequence of binds ending with a void bind.

Returns:
f(a):{()}→B; () ↦ f(a)

bind

public static VoidPredicate bind(Predicate P,
                                 java.lang.Object a)
Binds the argument of a Predicate to a fixed value.

bind: ℘(A)→℘({()}); P↦P(a). The void adjoint got from P by "currying".

Returns:
P(a):∅→Boole; () ↦ P := P(a)

bind

public static Function bind(BinaryFunction f)
Binds both arguments of a BinaryFunction together.

bind: Map(A×A,B)→Map(A,B); f↦g. The unitary adjoint function (not by "currying").

Returns:
g:A→B; x ↦ g(x) := f(x, x)

bind

public static Predicate bind(BinaryPredicate P)
Binds both arguments of a BinaryPredicate together.

bind: ℘(A×A)→℘(A); P↦Q. The unitary adjoint predicate (not by "currying").

Returns:
Q := λx.P(x,x) = {x∈A ¦ P(x, x)}

onFirst

public static BinaryFunction onFirst(Function f)
Applies a function on the first argument, ignoring the second.

onFirst: f↦g.

Evolves: might be renamed or removed.

Returns:
(x,y) ↦ g(x,y) := f(x)
See Also:
Adapter Pattern

onFirst

public static BinaryPredicate onFirst(Predicate p)
Applies a predicate on the first argument, ignoring the second.

onFirst: f↦g;.

Evolves: might be renamed or removed.

Returns:
(x,y) ↦ g(x,y) := f(x)
See Also:
Adapter Pattern

onSecond

public static BinaryFunction onSecond(Function f)
Applies a function on the second argument, ignoring the first.

onSecond: f↦g;.

Evolves: might be renamed or removed.

The resulting function is right-projective, i.e., it is curry constant: currying the resulting function yields to a constant function.

Returns:
(x,y) ↦ g(x,y) := f(y)
See Also:
Adapter Pattern

onSecond

public static BinaryPredicate onSecond(Predicate p)
Applies a predicate on the second argument, ignoring the first.

onSecond: f↦g;.

Evolves: might be renamed or removed.

Returns:
(x,y) ↦ g(x,y) := f(y)
See Also:
Adapter Pattern

onVoid

public static Function onVoid(VoidFunction f)
Applies a function on the void argument, ignoring all arguments.

onVoid: f↦g;.

Evolves: might be renamed or removed.

Returns:
x ↦ g(x) := f()
See Also:
Adapter Pattern

onVoid

public static Predicate onVoid(VoidPredicate p)
Applies a predicate on the void argument, ignoring all arguments.

onVoid: f↦g;.

Evolves: might be renamed or removed.

Returns:
x ↦ g(x) := f()
See Also:
Adapter Pattern

swap

public static BinaryFunction swap(BinaryFunction f)
Swaps the two arguments of a BinaryFunction.

swap: Map(A1×A2,B)→Map(A2×A1,B); f↦f.

Returns:
f:A2×A1→B; (x,y) ↦ f(x,y) := f(y,x)
Attributes:
involutive ...

swap

public static BinaryPredicate swap(BinaryPredicate P)
Swaps the two arguments of a BinaryPredicate (inverse).

swap: ℘(A1×A2)→℘(A2×A1); P↦P.

Returns the converse predicate P-1 of P.

Returns:
P-1=P := {(x,y)∈A2×A1 ¦ P(y,x)}
Attributes:
morph ∪, morph ∩, involutive ...

asFunction

public static VoidFunction asFunction(VoidPredicate p)
converts a predicate to a function.

This method acts as a bridge between predicates and functions in case a predicate representation is not acceptable.

See Also:
Adapter Pattern

asFunction

public static Function asFunction(Predicate p)
converts a predicate to a function.

This method acts as a bridge between predicates and functions in case a predicate representation is not acceptable.

Returns:
the characteristic function χp with χρ(x)=1 iff x∈ρ, and χρ(x)=0 iff x∉ρ.
See Also:
Adapter Pattern

asFunction

public static BinaryFunction asFunction(BinaryPredicate p)
converts a predicate to a function.

This method acts as a bridge between predicates and functions in case a predicate representation is not acceptable.

See Also:
Adapter Pattern

listable

public static Function listable(Function f)
Get a listable function automatically mapping itself over lists (recursively).

listable: Map(A,B)→Map(A∪A*,C∪C*); f ↦ f*.

Applies the function to each element of the list a. Also known as collect.

The listable function takes values ∈A* represented as a generalized iteratable, like f.ex. Iterator.

Returns:
x↦f*(x) where f*({x0,...,xm}) = {f(x0),...,f(xm)} for Lists, Collections and Object[], and f*(x) = f(x), otherwise.
listable: a ↦ f(a) := {f(x) ¦ x∈a}
See Also:
map(Function, Collection)

mapInto

protected static final void mapInto(Function f,
                                    java.util.Iterator a,
                                    java.util.ListIterator r)
map implementation writing values into the target iterator t.

Overwrites any existing elements in t and appends if necessary. As long as the target iterator has a next element, that will be overwritten via ListIterator.set(Object). But as soon as the target iterator reaches its end, new elements will be added via ListIterator.add(Object).

Preconditions:
ALWAYS(a.hasNext() ⇒ ((t.hasNext() ⇒ SUCCEEDES(t.set(Object))) ∧ (¬t.hasNext() ⇒ SUCCEEDES(t.add(Object)))))

map

public static java.util.Collection map(Function f,
                                       java.util.Collection a)
Maps a list of arguments with a function.

map: a ↦ f(a) := {f(x) ¦ x∈a}.
Applies the Function to each element of the list a.

Also known as collect.

The function map can be specified by

map f ∅ = f ∅
map f ([first|rest]) = [f first | map f rest]
In terms of catamorphisms or anamorphisms this equals
map = (|∅, ((a,bs) ↦ [f a | bs]) |)
map = |(g, (as↦as=∅) )|
where g([a|as]) = (f(a), as)

See Also:
listable(Function)
Postconditions:
RES.getClass()=a.getClass() or at least compatible

map

public static java.util.List map(Function f,
                                 java.util.List a)

map

public static java.util.Set map(Function f,
                                java.util.Set a)

map

public static java.util.SortedSet map(Function f,
                                      java.util.SortedSet a)

map

public static java.util.Iterator map(Function f,
                                     java.util.Iterator a)

map

public static java.util.ListIterator map(Function f,
                                         java.util.ListIterator a)

map

public static java.lang.Object[] map(Function f,
                                     java.lang.Object[] a)
Maps a list of arguments with a function.

map: a ↦ f(a) := {f(x) ¦ x∈a}.
Applies the Function to each element of the list a.

See Also:
map(Function,Collection)

map

protected static final java.lang.Object map(Function f,
                                            java.lang.Object a)
Maps a list-like generalized iteratable list of arguments with a function.

Takes values ∈A* represented as a generalized iteratable, like f.ex. Iterator.

See Also:
map(Function,Collection)

listable

public static BinaryFunction listable(BinaryFunction f)
Get a listable function automatically mapping itself over lists (recursively).

listable: Map(A,B)→Map(A∪A*,C∪C*); f ↦ f*.

listable: a ↦ f(a) := {f(x) ¦ x∈a}.
Applies the function to each element of the list a.

The listable function takes values ∈A* represented as a generalized iteratable, like f.ex. Iterator.

Returns:
x↦f*(x) where f*({x0,...,xm}) = {f(x0),...,f(xm)} for Lists, Collections and Object[], and f*(x) = f(x), otherwise.
See Also:
map(BinaryFunction, Collection, Collection)

mapInto

protected static void mapInto(BinaryFunction f,
                              java.util.Iterator x,
                              java.util.Iterator y,
                              java.util.ListIterator t)
map implementation writing values into the target iterator t.

Overwrites any existing elements in t and appends if necessary. As long as the target iterator has a next element, that will be overwritten via ListIterator.set(Object). But as soon as the target iterator reaches its end, new elements will be added via ListIterator.add(Object).

Preconditions:
ALWAYS(x.hasNext()⇔y.hasNext()) ∧ ALWAYS(x.hasNext() ⇒ ((t.hasNext() ⇒ SUCCEEDES(t.set(Object))) ∧ (¬t.hasNext() ⇒ SUCCEEDES(t.add(Object)))))

map

public static java.util.Collection map(BinaryFunction f,
                                       java.util.Collection x,
                                       java.util.Collection y)
Maps two lists of arguments with a BinaryFunction.

map: ((xi)i∈I,(yi)i∈I) ↦ f((xi)i∈I,(yi)i∈I) := (f(xi,yi))i∈I.

See Also:
listable(BinaryFunction)
Preconditions:
x.size() == y.size()
Postconditions:
RES.getClass()=x.getClass() or at least compatible

map

public static java.util.List map(BinaryFunction f,
                                 java.util.List x,
                                 java.util.List y)

map

public static java.util.Set map(BinaryFunction f,
                                java.util.Set x,
                                java.util.Set y)

map

public static java.util.SortedSet map(BinaryFunction f,
                                      java.util.SortedSet x,
                                      java.util.SortedSet y)

map

public static java.util.Iterator map(BinaryFunction f,
                                     java.util.Iterator x,
                                     java.util.Iterator y)

map

public static java.util.ListIterator map(BinaryFunction f,
                                         java.util.ListIterator x,
                                         java.util.ListIterator y)

map

public static java.lang.Object[] map(BinaryFunction f,
                                     java.lang.Object[] x,
                                     java.lang.Object[] y)
Maps two lists of arguments with a BinaryFunction.

map: ((xi)i∈I,(yi)i∈I) ↦ f((xi)i∈I,(yi)i∈I) := (f(xi,yi))i∈I.

Preconditions:
x.length == y.length

foldLeft

public static java.lang.Object foldLeft(BinaryFunction f,
                                        java.lang.Object c,
                                        java.lang.Object[] a)
Folds a list with a BinaryFunction.

foldLeft: a ↦ f(...f(f(f(c,a[0]),a[1]),a[2])...)

Corresponds to a left-recursive function.

h ∅ = c
h ([first|rest]) = f(h(rest), first)

For commutative functions the result will equal that of foldRight.

Also known as inject, accumulate. Implemented as an iterative unrolling of a linear left tail-recurrence.

Parameters:
f - the function used to fold the list a with.
c - the left (first) argument to start with. The result of the application of f will progressively build the next left (first) argument.
a - the list of arguments to be iteratively used as the right (second) arguments.

foldLeft

public static java.lang.Object foldLeft(BinaryFunction f,
                                        java.lang.Object c,
                                        java.util.Iterator a)

foldLeft

public static java.lang.Object foldLeft(BinaryFunction f,
                                        java.lang.Object c,
                                        java.util.Collection a)

foldRight

public static java.lang.Object foldRight(BinaryFunction f,
                                         java.lang.Object c,
                                         java.lang.Object[] a)
Folds a list with a BinaryFunction.

foldRight: a ↦ f(a[0], f(a[1], f(... f(a[n],c)...)))

foldRight is the same as the catamorphism or banana of a.

For commutative functions f the result will equal that of foldLeft.

Also known as fold, reduce. Implemented as an iterative unrolling of a linear right tail-recurrence.

Parameters:
f - the function used to fold the list a with.
c - the right (second) argument to start with. The result of the application of f will progressively build the next right (second) argument.
a - the list of arguments to be iteratively used as the left (first) arguments.
Returns:
(|c,f|) a.
See Also:
Functionals.Catamorphism.Functionals.Catamorphism(Object, BinaryFunction), "G. Hutton. A Tutorial on the universality and expressiveness of fold. Journal of Functional Programming 1(1), Jan. 1993."

foldRight

public static java.lang.Object foldRight(BinaryFunction f,
                                         java.lang.Object c,
                                         java.util.List a)
efficient foldRight for lists.


foldRight

public static java.lang.Object foldRight(BinaryFunction f,
                                         java.lang.Object c,
                                         java.util.Collection a)

foldRight

public static java.lang.Object foldRight(BinaryFunction f,
                                         java.lang.Object c,
                                         java.util.Iterator a)

banana

public static java.lang.Object banana(java.lang.Object c,
                                      BinaryFunction f,
                                      java.util.Iterator a)
banana (|c,f|) a.

See Also:
Functionals.Catamorphism.Functionals.Catamorphism(Object, BinaryFunction), foldRight(BinaryFunction, Object, Iterator)

lense

public static java.util.List lense(Function g,
                                   Predicate p,
                                   java.lang.Object b)
lense |(g,p)| b.

See Also:
Functionals.Anamorphism.Functionals.Anamorphism(Function, Predicate)

envelope

public static java.lang.Object envelope(java.lang.Object c,
                                        BinaryFunction f,
                                        Function g,
                                        Predicate p,
                                        java.lang.Object a)
envelope [[(c,f),(g,p)]] a.

See Also:
Functionals.Hylomorphism.Functionals.Hylomorphism(Object, BinaryFunction, Function, Predicate)

barbedwire

public static java.lang.Object barbedwire(java.lang.Object b,
                                          BinaryFunction f,
                                          java.util.Iterator a)
barbedwire {|b,f|} a.

See Also:
Functionals.Paramorphism.Functionals.Paramorphism(Object, BinaryFunction)

paramorphism

protected static Function paramorphism(int b,
                                       BinaryFunction f)
Paramorphism recursion functional operator (barbed wire).

A paramorphism is denoted by barbed wires. For numbers a paramorphism is

para 0 = b
para (n+1) = f(n, para n)
If b and f are functionals (or λ-expressions), {|b,f|} directly corresponds to the primitive recursion.

Parameters:
b - is the basevalue to use.
f - is a binary function.
Returns:
{|b,f|} still depending on the is a starting value a.
See Also:
"Lambert Meertens. Paramorphisms. In: Formal Aspects of Computing, 1990."

barbedwire

public static int barbedwire(int b,
                             BinaryFunction f,
                             int a)

nest

public static Function nest(Function f,
                            int n)
Nests a function n times within itself.

nest: (f,n) ↦ fn = ∘i=1,...,n f = f ∘ f ∘ ... ∘ f (n times).
nest(f,n).apply(A) gives an expression with f applied n times to A.

Parameters:
f - the function to be nested.
n - the number of times the f should be composed.
Returns:
fn
Preconditions:
n>=0

fixedPoint

public static java.lang.Object fixedPoint(Function f,
                                          java.lang.Object x,
                                          int maxIteration)
                                   throws IterationLimitException
fixedPoint starts with an object, then applies f repeatedly until the result no longer changes.

Beginning with x, it will find an x* such that f(x*) = x*, if f is continuous. Then x* is a fixed point of f.

The fixed point iteration will converge if f is a contraction, i.e.

∃ q∈[0,1) with ||f(x)-f(y)|| ≤ q*||x-y|| ∀x,y
If q = sup ||f'(z)|| < 1, then f is a contraction with q.

Parameters:
f - the (continuous) function to apply repeatedly for searching a fixed point.
x - where to start searching a fixed point.
maxIteration - the maximum number of iterations waiting for a convergence to a fixed point. Will then throw an IterationLimitException
Throws:
IterationLimitException - when the maximum number of iterations maxIteration is overrun.

fixedPoint

public static java.lang.Object fixedPoint(Function f,
                                          java.lang.Object x)
                                   throws IterationLimitException
Throws:
IterationLimitException

Orbital library
1.3.0: 11 Apr 2009

Copyright © 1996-2009 André Platzer
All Rights Reserved.