Copyright Workingmouse Pty. Ltd. 2007, 2008

This software is released under an open source BSD licence.

fj.control.parallel
Class Strategy<A>

java.lang.Object
  extended by fj.control.parallel.Strategy<A>

public final class Strategy<A>
extends Object

Functional-style parallel processing for Java. A Strategy is a method of turning a closure into a Future, from which the result of its evaluation can be retrieved at a later time.

Version:
2.8

Method Summary
static
<A> Strategy<A>
completionStrategy(CompletionService<A> s)
          Provides a parallelization strategy that uses a CompletionService to control the method and degree of parallelism, and where each parallel task's completion is registered with the service.
<B> F<B,P1<A>>
concurry(F<B,A> f)
          Promotes a function to a concurrent function.
<B,C> F<B,F<C,P1<A>>>
concurry(F2<B,C,A> f)
          Promotes a function of arity-2 to a concurrent function.
static
<A> Effect<Future<A>>
discard()
          Returns an Effect that waits for a given Future to obtain a value, discarding the value.
static
<A> Strategy<A>
executorStrategy(ExecutorService s)
          Provides a parallelization strategy that uses an ExecutorService to control the method and degree of parallelism.
 F<P1<A>,Future<A>> f()
          Returns the functional representation of this Strategy.
static
<A> List<P1<A>>
mergeAll(List<Future<A>> xs)
          Waits for every Future in a list to obtain a value, and collects those values in a list.
static
<A> F<Future<A>,P1<A>>
obtain()
          Returns a function which returns a closure which waits for the given Future to obtain a value.
 F<P1<A>,P1<A>> par()
          Returns a function that evaluates a closure concurrently.
 P1<A> par(P1<A> a)
          Apply the strategy to the given closure, evaluating it concurrently.
static
<A,B> P1<List<B>>
parFlatMap(Strategy<List<B>> s, F<A,List<B>> f, List<A> as)
          Binds the given function in parallel across the given list, using the given strategy, with a final join.
 P1<List<A>> parList(List<P1<A>> ps)
          Evaluates a list of closures in parallel.
static
<A> P1<List<A>>
parListChunk(Strategy<List<A>> s, int chunkLength, List<P1<A>> as)
          Sequentially evaluates chunks (sub-sequences) of a list in parallel.
<B> F<F<B,A>,F<List<B>,P1<List<A>>>>
parMap_()
          First-class version of parMap on lists
<B> F<List<B>,P1<List<A>>>
parMap(F<B,A> f)
          Lifts a function to a parallel function on lists using this strategy.
<B> P1<List<A>>
parMap(F<B,A> f, List<B> bs)
          Maps the given function over the given list in parallel using this strategy.
<B> F<F<B,A>,F<List<B>,List<A>>>
parMap1_()
          First-class version of parMap1 on lists
<B> F<List<B>,List<A>>
parMap1(F<B,A> f)
          Lifts a function to a blocking parallel function on lists, using this strategy.
<B> F<Stream<B>,P1<Stream<A>>>
parMapStream(F<B,A> f)
          Lifts a function to a parallel function on streams using this strategy.
<B> F<F<B,A>,F<Stream<B>,Stream<A>>>
parMapStream1_()
          First-class version of parMap1 on streams
<B> F<Stream<B>,Stream<A>>
parMapStream1(F<B,A> f)
          Lifts a function to a blocking parallel function on streams, using this strategy.
<B,C> F2<List<B>,List<C>,P1<List<A>>>
parZipWith(F2<B,C,A> f)
          Lifts a given function of arity-2 so that it zips together two lists in parallel, using this strategy, calling the function once for each corresponding pair in the lists, position-wise.
<B,C> P1<List<A>>
parZipWith(F2<B,C,A> f, List<B> bs, List<C> cs)
          Zips together two lists in parallel using a given function, with this strategy.
static
<A> Strategy<A>
simpleThreadStrategy()
          Provides a simple parallelization strategy that creates, and discards, a new thread for every evaluation.
static
<A> Strategy<A>
strategy(F<P1<A>,Future<A>> f)
          Constructs a strategy from the given Future-valued function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

f

public F<P1<A>,Future<A>> f()
Returns the functional representation of this Strategy.

Returns:
The function representing this strategy.

strategy

public static <A> Strategy<A> strategy(F<P1<A>,Future<A>> f)
Constructs a strategy from the given Future-valued function.

Parameters:
f - The execution function for the strategy
Returns:
A strategy that uses the given function for generating Futures.

par

public P1<A> par(P1<A> a)
Apply the strategy to the given closure, evaluating it concurrently.

Parameters:
a - A P1 to evaluate concurrently.
Returns:
A P1 that yields the value from calling the given closure concurrently.

par

public F<P1<A>,P1<A>> par()
Returns a function that evaluates a closure concurrently.

Returns:
A function that evaluates its argument concurrently.

concurry

public <B> F<B,P1<A>> concurry(F<B,A> f)
Promotes a function to a concurrent function.

Parameters:
f - A function to promote to a concurrent function.
Returns:
A function that executes concurrently when called, yielding a Future value.

concurry

public <B,C> F<B,F<C,P1<A>>> concurry(F2<B,C,A> f)
Promotes a function of arity-2 to a concurrent function.

Parameters:
f - The function to promote to a concurrent function.
Returns:
A function that executes concurrently when called, yielding a closure that returns the value.

mergeAll

public static <A> List<P1<A>> mergeAll(List<Future<A>> xs)
Waits for every Future in a list to obtain a value, and collects those values in a list.

Parameters:
xs - The list of Futures from which to get values.
Returns:
A list of values extracted from the Futures in the argument list.

parList

public P1<List<A>> parList(List<P1<A>> ps)
Evaluates a list of closures in parallel.

Parameters:
ps - A list to evaluate in parallel.
Returns:
A list of the values of the closures in the argument.

parMap

public <B> F<List<B>,P1<List<A>>> parMap(F<B,A> f)
Lifts a function to a parallel function on lists using this strategy.

Parameters:
f - A function to transform into a parallel function on lists.
Returns:
The function transformed into a parallel function on lists.

parMap

public <B> P1<List<A>> parMap(F<B,A> f,
                              List<B> bs)
Maps the given function over the given list in parallel using this strategy.

Parameters:
f - A function to map over the given list in parallel.
bs - A list over which to map the given function in parallel.
Returns:
A closure that returns the list with all of its elements transformed by the given function.

parMap1

public <B> F<List<B>,List<A>> parMap1(F<B,A> f)
Lifts a function to a blocking parallel function on lists, using this strategy. The function blocks in the sense that the current thread will wait for all the results to become available.

Parameters:
f - A function to transform into a blocking parallel function on lists.
Returns:
The function transformed into a blocking parallel function on lists.

parMap_

public <B> F<F<B,A>,F<List<B>,P1<List<A>>>> parMap_()
First-class version of parMap on lists

Returns:
A function that promotes another function to a parallel function on lists.

parMap1_

public <B> F<F<B,A>,F<List<B>,List<A>>> parMap1_()
First-class version of parMap1 on lists

Returns:
A function that promotes another function to a blocking parallel function on lists.

parMapStream

public <B> F<Stream<B>,P1<Stream<A>>> parMapStream(F<B,A> f)
Lifts a function to a parallel function on streams using this strategy.

Parameters:
f - A function to transform into a parallel function on streams.
Returns:
The function transformed into a parallel function on streams.

parMapStream1

public <B> F<Stream<B>,Stream<A>> parMapStream1(F<B,A> f)
Lifts a function to a blocking parallel function on streams, using this strategy. The function blocks in the sense that the current thread will wait for all the results to become available.

Parameters:
f - A function to transform into a blocking parallel function on streams.
Returns:
The function transformed into a blocking parallel function on streams.

parMapStream1_

public <B> F<F<B,A>,F<Stream<B>,Stream<A>>> parMapStream1_()
First-class version of parMap1 on streams

Returns:
A function that promotes another function to a blocking parallel function on streams.

parFlatMap

public static <A,B> P1<List<B>> parFlatMap(Strategy<List<B>> s,
                                           F<A,List<B>> f,
                                           List<A> as)
Binds the given function in parallel across the given list, using the given strategy, with a final join.

Parameters:
s - The strategy to use for parallelization.
f - The function to bind across the given list.
as - The list across which to bind the given function.
Returns:
A P1 containing the result of the parallel map operation after the final join.

parListChunk

public static <A> P1<List<A>> parListChunk(Strategy<List<A>> s,
                                           int chunkLength,
                                           List<P1<A>> as)
Sequentially evaluates chunks (sub-sequences) of a list in parallel. Splits the list into chunks, evaluating the chunks simultaneously, but each chunk as a sequence.

Parameters:
s - The strategy to use for parallelization.
chunkLength - The length of each sequence.
as - The list to evaluate in parallel chunks.
Returns:
A closure containing the list of results extracted from the given list of closures.

parZipWith

public <B,C> P1<List<A>> parZipWith(F2<B,C,A> f,
                                    List<B> bs,
                                    List<C> cs)
Zips together two lists in parallel using a given function, with this strategy. Calls the given function once for each corresponding pair in the lists, position-wise, passing elements from the first list to the first argument of the function, and elements from the second list to the second argument of the function, yielding a list of the results. If the lists are not of the same length, the remaining elements of the longer list are ignored.

Parameters:
f - The function of arity-2 with which to zip.
bs - A list to zip with the given function.
cs - A list to zip with the given function.
Returns:
The list of the results of calling the given function on corresponding elements of the given lists.

parZipWith

public <B,C> F2<List<B>,List<C>,P1<List<A>>> parZipWith(F2<B,C,A> f)
Lifts a given function of arity-2 so that it zips together two lists in parallel, using this strategy, calling the function once for each corresponding pair in the lists, position-wise.

Parameters:
f - The function of arity-2 with which to zip.
Returns:
A transformation that zips two lists using the argument function, in parallel.

obtain

public static <A> F<Future<A>,P1<A>> obtain()
Returns a function which returns a closure which waits for the given Future to obtain a value.

Returns:
A function which, given a Future, yields a closure that waits for it.

discard

public static <A> Effect<Future<A>> discard()
Returns an Effect that waits for a given Future to obtain a value, discarding the value.

Returns:
An effect, which, given a Future, waits for it to obtain a value, discarding the value.

simpleThreadStrategy

public static <A> Strategy<A> simpleThreadStrategy()
Provides a simple parallelization strategy that creates, and discards, a new thread for every evaluation.

Returns:
a simple parallelization strategy that creates, and discards, a new thread for every evaluation.

executorStrategy

public static <A> Strategy<A> executorStrategy(ExecutorService s)
Provides a parallelization strategy that uses an ExecutorService to control the method and degree of parallelism.

Parameters:
s - The ExecutorService to use for scheduling evaluations.
Returns:
A Strategy that uses the provided ExecutorService to control the method and degree of parallelism.

completionStrategy

public static <A> Strategy<A> completionStrategy(CompletionService<A> s)
Provides a parallelization strategy that uses a CompletionService to control the method and degree of parallelism, and where each parallel task's completion is registered with the service.

Parameters:
s - The CompletionService to use for scheduling evaluations and detect their completion.
Returns:
A Strategy that uses the provided CompletionService to control the method and degree of parallelism, and notifies the service of task completion.

Copyright Workingmouse Pty. Ltd. 2007, 2008

This software is released under an open source BSD licence.