Difference between revisions of "Fork Loop Assignment"

From CSE231 Wiki
Jump to navigation Jump to search
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=Motivation=
 
=Motivation=
 
There are many incantations of the fork_loop.  We gain experience with selecting the right one for two scenarios.
 
There are many incantations of the fork_loop.  We gain experience with selecting the right one for two scenarios.
 +
 +
=Background=
 +
<youtube>LZ8Gorqj9ZU</youtube>
  
 
=Code To Use=
 
=Code To Use=
==FJ<E>==
+
==FJ==
class [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/fall22/apidocs/fj/FJ.html FJ<E>]
+
class [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html FJ<E>]
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/fall22/apidocs/fj/FJ.html#join_fork_loop(java.lang.Class,T%5B%5D,fj.api.TaskFunction) join_fork_loop(returnValueComponentType, array, function)]
+
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(java.lang.Iterable,fj.api.TaskFunction) join_fork_loop(iterable, task)]
 
+
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(java.lang.Class,T%5B%5D,fj.api.TaskFunction) join_fork_loop(returnValueComponentType, array, task)]
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/fall22/apidocs/fj/FJ.html#join_fork_loop(java.lang.Iterable,fj.api.TaskFunction) join_fork_loop(iterable, function)]
 
  
 +
=Code To Implement=
 
==Echoes==
 
==Echoes==
 
{{CodeToImplement|Echoes|toEchoes|forkloop.group}}
 
{{CodeToImplement|Echoes|toEchoes|forkloop.group}}
Line 16: Line 19:
 
for each text in parallel, produce <code>text + " " + text</code>.
 
for each text in parallel, produce <code>text + " " + text</code>.
  
<code>toEchoes</code> accepts a [https://docs.oracle.com/javase/8/docs/api/java/util/List.html List] parameter and returns a [https://docs.oracle.com/javase/8/docs/api/java/util/List.html List].
+
{{RightToolForTheJob|The toEchoes method accepts a {{ListDoc|<String>}} parameter and returns a {{ListDoc|<String>}}.
 +
 
 +
{{ListDoc}} is an {{IterableDoc}}, so instances of {{ListDoc}} can be passed to [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(java.lang.Iterable,fj.api.TaskFunction) join_fork_loop(iterable, task)].}}
  
 
===client===
 
===client===
 
{{CodeToInvestigate|EchoClient|main|forkloop.client|main}}
 
{{CodeToInvestigate|EchoClient|main|forkloop.client|main}}
  
=Code To Implement=
 
 
==SquareRoots==
 
==SquareRoots==
 
{{CodeToImplement|SquareRoots|toSquareRoots|forkloop.group}}
 
{{CodeToImplement|SquareRoots|toSquareRoots|forkloop.group}}
 
===toSquareRoots===
 
===toSquareRoots===
 
{{Parallel|public static Double[] toSquareRoots(Double[] values)}}
 
{{Parallel|public static Double[] toSquareRoots(Double[] values)}}
 +
 +
{{RightToolForTheJob|The method toSquareRoots accepts an array parameter and returns an array.
 +
 +
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(java.util.function.IntFunction,int,int,fj.api.TaskIntFunction) join_fork_loop(returnValueArrayCreator, array, task)] will perform exactly what we want, but we need to also pass an array creator.
 +
 +
How do you tersely specify an returnValueArrayCreator for <code>Double[]</code>? {{Spoiler|<code>Double[]::new</code>}}}}
 +
 
===client===
 
===client===
 
{{CodeToInvestigate|SquareRootClient|main|forkloop.client|main}}
 
{{CodeToInvestigate|SquareRootClient|main|forkloop.client|main}}

Latest revision as of 02:32, 11 September 2023

Motivation

There are many incantations of the fork_loop. We gain experience with selecting the right one for two scenarios.

Background

Code To Use

FJ

class FJ<E>

join_fork_loop(iterable, task)
join_fork_loop(returnValueComponentType, array, task)

Code To Implement

Echoes

class: Echoes.java Java.png
methods: toEchoes
package: forkloop.group
source folder: student/src/main/java

toEchoes

method: public static List<String> toEchoes(List<String> texts) Parallel.svg (parallel implementation required)

for each text in parallel, produce text + " " + text.

Wrench-screwdriver-icon Right Tool For The Job Requirement
The toEchoes method accepts a List<String> parameter and returns a List<String>. List<E> is an Iterable<E>, so instances of List<E> can be passed to join_fork_loop(iterable, task).

client

class: EchoClient.java DEMO: Java.png
methods: main
package: forkloop.client
source folder: src/main/java

SquareRoots

class: SquareRoots.java Java.png
methods: toSquareRoots
package: forkloop.group
source folder: student/src/main/java

toSquareRoots

method: public static Double[] toSquareRoots(Double[] values) Parallel.svg (parallel implementation required)

Wrench-screwdriver-icon Right Tool For The Job Requirement
The method toSquareRoots accepts an array parameter and returns an array.

join_fork_loop(returnValueArrayCreator, array, task) will perform exactly what we want, but we need to also pass an array creator.

How do you tersely specify an returnValueArrayCreator for Double[]?

client

class: SquareRootClient.java DEMO: Java.png
methods: main
package: forkloop.client
source folder: src/main/java

Testing

class: _ForkLoopTestSuite.java Junit.png
package: forkloop.group
source folder: testing/src/test/java