Package fj.control.parallel
Class Callables
- java.lang.Object
-
- fj.control.parallel.Callables
-
public final class Callables extends java.lang.Object
Monadic functions and conversion methods for java.util.concurrent.Callable.- Version:
- %build.number%
-
-
Constructor Summary
Constructors Modifier Constructor Description private
Callables()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <A,B>
java.util.concurrent.Callable<B>apply(java.util.concurrent.Callable<A> ca, java.util.concurrent.Callable<F<A,B>> cf)
Performs function application within a callable (applicative functor pattern).static <A,B>
F<F<A,B>,F<A,java.util.concurrent.Callable<B>>>arrow()
Provides a transformation from a function to a Callable-valued function that is equivalent to it.static <A,B>
java.util.concurrent.Callable<B>bind(java.util.concurrent.Callable<A> a, F<A,java.util.concurrent.Callable<B>> f)
Binds the given function to the value in a Callable with a final join.static <A,B,C>
java.util.concurrent.Callable<C>bind(java.util.concurrent.Callable<A> ca, java.util.concurrent.Callable<B> cb, F<A,F<B,C>> f)
Binds the given function to the values in the given callables with a final join.static <A> F<A,java.util.concurrent.Callable<A>>
callable()
Provides a transformation from a value to a Callable that completely preserves that value.static <A> java.util.concurrent.Callable<A>
callable(A a)
Returns a callable that completely preserves the argument.static <A,B>
F<A,java.util.concurrent.Callable<B>>callable(F<A,B> f)
Wraps a given function's return value in a Callable.static <A> java.util.concurrent.Callable<A>
callable(java.lang.Exception e)
Returns a callable that throws the given exception.static <A> F<java.util.concurrent.Callable<A>,P1<Either<java.lang.Exception,A>>>
either()
Returns a transformation from a Callable to an Either.static <A> P1<Either<java.lang.Exception,A>>
either(java.util.concurrent.Callable<A> a)
Turns the given Callable into either an exception or the value in the Callable.static <A,B>
F<java.util.concurrent.Callable<A>,java.util.concurrent.Callable<B>>fmap(F<A,B> f)
Lifts any function to a function on Callables.static <A> F<P1<Either<java.lang.Exception,A>>,java.util.concurrent.Callable<A>>
fromEither()
Returns a transformation from an Either to a Callable.static <A> java.util.concurrent.Callable<A>
fromEither(F0<Either<java.lang.Exception,A>> e)
Turns a given Either value into the equivalent Callable.static <A> F<P1<Option<A>>,java.util.concurrent.Callable<A>>
fromOption()
Returns a transformation from an optional value to a Callablestatic <A> java.util.concurrent.Callable<A>
fromOption(F0<Option<A>> o)
Turns an optional value into a Callable.static <A> java.util.concurrent.Callable<A>
join(java.util.concurrent.Callable<java.util.concurrent.Callable<A>> a)
Joins a Callable of a Callable with a bind operation.static <A,B,C>
F<java.util.concurrent.Callable<A>,F<java.util.concurrent.Callable<B>,java.util.concurrent.Callable<C>>>liftM2(F<A,F<B,C>> f)
Promotes a function of arity-2 to a function on callables.static <A> F<java.util.concurrent.Callable<A>,java.util.concurrent.Callable<A>>
normalise()
A first-class version of the normalise function.static <A> java.util.concurrent.Callable<A>
normalise(java.util.concurrent.Callable<A> a)
Normalises the given Callable by calling it and wrapping the result in a new Callable.static <A> F<java.util.concurrent.Callable<A>,P1<Option<A>>>
option()
Returns a transformation from a Callable to an optional value.static <A> P1<Option<A>>
option(java.util.concurrent.Callable<A> a)
Turns the given Callable into an optional value.static <A> java.util.concurrent.Callable<List<A>>
sequence(List<java.util.concurrent.Callable<A>> as)
Turns a List of Callables into a single Callable of a List.static <A> F<List<java.util.concurrent.Callable<A>>,java.util.concurrent.Callable<List<A>>>
sequence_()
A first-class version of the sequence method.
-
-
-
Method Detail
-
callable
public static <A> java.util.concurrent.Callable<A> callable(A a)
Returns a callable that completely preserves the argument. The unit function for Callables.- Parameters:
a
- A value to preserve in a Callable- Returns:
- A Callable that yields the argument when called.
-
callable
public static <A> java.util.concurrent.Callable<A> callable(java.lang.Exception e)
Returns a callable that throws the given exception. The unit function for Callables.- Parameters:
e
- The exception to throw when the Callable is called.- Returns:
- A callable that always throws the given exception.
-
callable
public static <A> F<A,java.util.concurrent.Callable<A>> callable()
Provides a transformation from a value to a Callable that completely preserves that value.- Returns:
- A function from a value to a Callable that completely preserves that value.
-
callable
public static <A,B> F<A,java.util.concurrent.Callable<B>> callable(F<A,B> f)
Wraps a given function's return value in a Callable. The Kleisli arrow for Callables.- Parameters:
f
- The function whose return value to wrap in a Callable.- Returns:
- The equivalent function whose return value is wrapped in a Callable.
-
arrow
public static <A,B> F<F<A,B>,F<A,java.util.concurrent.Callable<B>>> arrow()
Provides a transformation from a function to a Callable-valued function that is equivalent to it. The first-class Kleisli arrow for Callables.- Returns:
- A transformation from a function to the equivalent Callable-valued function.
-
bind
public static <A,B> java.util.concurrent.Callable<B> bind(java.util.concurrent.Callable<A> a, F<A,java.util.concurrent.Callable<B>> f)
Binds the given function to the value in a Callable with a final join.- Parameters:
a
- A value in a Callable to which to apply a function.f
- A function to apply to the value in a Callable.- Returns:
- The result of applying the function in the second argument to the value of the Callable in the first.
-
fmap
public static <A,B> F<java.util.concurrent.Callable<A>,java.util.concurrent.Callable<B>> fmap(F<A,B> f)
Lifts any function to a function on Callables.- Parameters:
f
- A function to lift to a function on Callables.- Returns:
- That function lifted to a function on Callables.
-
apply
public static <A,B> java.util.concurrent.Callable<B> apply(java.util.concurrent.Callable<A> ca, java.util.concurrent.Callable<F<A,B>> cf)
Performs function application within a callable (applicative functor pattern).- Parameters:
ca
- The callable to which to apply a function.cf
- The callable function to apply.- Returns:
- A new callable after applying the given callable function to the first argument.
-
bind
public static <A,B,C> java.util.concurrent.Callable<C> bind(java.util.concurrent.Callable<A> ca, java.util.concurrent.Callable<B> cb, F<A,F<B,C>> f)
Binds the given function to the values in the given callables with a final join.- Parameters:
ca
- A given callable to bind the given function with.cb
- A given callable to bind the given function with.f
- The function to apply to the values in the given callables.- Returns:
- A new callable after performing the map, then final join.
-
join
public static <A> java.util.concurrent.Callable<A> join(java.util.concurrent.Callable<java.util.concurrent.Callable<A>> a)
Joins a Callable of a Callable with a bind operation.- Parameters:
a
- The Callable of a Callable to join.- Returns:
- A new Callable that is the join of the given Callable.
-
liftM2
public static <A,B,C> F<java.util.concurrent.Callable<A>,F<java.util.concurrent.Callable<B>,java.util.concurrent.Callable<C>>> liftM2(F<A,F<B,C>> f)
Promotes a function of arity-2 to a function on callables.- Parameters:
f
- The function to promote.- Returns:
- A function of arity-2 promoted to map over callables.
-
sequence
public static <A> java.util.concurrent.Callable<List<A>> sequence(List<java.util.concurrent.Callable<A>> as)
Turns a List of Callables into a single Callable of a List.- Parameters:
as
- The list of callables to transform.- Returns:
- A single callable for the given List.
-
sequence_
public static <A> F<List<java.util.concurrent.Callable<A>>,java.util.concurrent.Callable<List<A>>> sequence_()
A first-class version of the sequence method.- Returns:
- A function from a List of Callables to a single Callable of a List.
-
option
public static <A> P1<Option<A>> option(java.util.concurrent.Callable<A> a)
Turns the given Callable into an optional value.- Parameters:
a
- The callable to convert to an optional value.- Returns:
- An optional value that yields the value in the Callable, or None if the Callable fails.
-
option
public static <A> F<java.util.concurrent.Callable<A>,P1<Option<A>>> option()
Returns a transformation from a Callable to an optional value.- Returns:
- a function that turns a Callable into an optional value.
-
either
public static <A> P1<Either<java.lang.Exception,A>> either(java.util.concurrent.Callable<A> a)
Turns the given Callable into either an exception or the value in the Callable.- Parameters:
a
- The callable to convert to an Either value.- Returns:
- Either the value in the given Callable, or the Exception with which the Callable fails.
-
either
public static <A> F<java.util.concurrent.Callable<A>,P1<Either<java.lang.Exception,A>>> either()
Returns a transformation from a Callable to an Either.- Returns:
- a function that turns a Callable into an Either.
-
fromEither
public static <A> java.util.concurrent.Callable<A> fromEither(F0<Either<java.lang.Exception,A>> e)
Turns a given Either value into the equivalent Callable.- Parameters:
e
- Either an exception or a value to wrap in a Callable- Returns:
- A Callable equivalent to the given Either value.
-
fromEither
public static <A> F<P1<Either<java.lang.Exception,A>>,java.util.concurrent.Callable<A>> fromEither()
Returns a transformation from an Either to a Callable.- Returns:
- a function that turns an Either into a Callable.
-
fromOption
public static <A> java.util.concurrent.Callable<A> fromOption(F0<Option<A>> o)
Turns an optional value into a Callable.- Parameters:
o
- An optional value to turn into a Callable.- Returns:
- A Callable that yields some value or throws an exception in the case of no value.
-
fromOption
public static <A> F<P1<Option<A>>,java.util.concurrent.Callable<A>> fromOption()
Returns a transformation from an optional value to a Callable- Returns:
- A function that turns an optional value into a Callable that yields some value or throws an exception in the case of no value.
-
normalise
public static <A> java.util.concurrent.Callable<A> normalise(java.util.concurrent.Callable<A> a)
Normalises the given Callable by calling it and wrapping the result in a new Callable. If the given Callable throws an Exception, the resulting Callable will throw that same Exception.- Parameters:
a
- The callable to evaluate.- Returns:
- A normalised callable that just returns the result of calling the given callable.
-
normalise
public static <A> F<java.util.concurrent.Callable<A>,java.util.concurrent.Callable<A>> normalise()
A first-class version of the normalise function.- Returns:
- A function that normalises the given Callable by calling it and wrapping the result in a new Callable.
-
-