A simple base project to allow exploration of Akka
$ cd akka
$ mvn package exec:java
Yay for tabbed browsers!
We will be exploring reactive concepts by playing with Observable operator composition.
- Open
src/main/java/com/example/AkkaApplication.java
in your IDE. - Edit the
run()
method to complete the exercises below.
Complete as many of the steps within each Exercise as you can (either within the allotted time, or without looking for help outside of the above references).
- Steps within an exercise are related (later steps often require earlier steps, e.g.)
- At the end of each step, use
dumpSourceToStdOut
to print the result. This will need some type munging; this is intentional.
- Use
.alsoTo(getDebugSink())
between operations if you get confused. - A cheap way to convert something to a string is with
.map(x -> "" + x)
-
Use
map
to make all words lowercase and remove all punctuation ([^a-zA-Z]
).For example, in
src/main/java/com/example/AkkaApplication.java
, edit therun()
and add the following:// Exercise 1: System.out.println("=== exercise 1.1"); Source<String, NotUsed> src_1_1 = words(); dumpSourceToStdOut(src_1_1);
Use
mvn package exec:java
from the command line to rebuild and run (run inside your IDE of choice if you prefer). The poem Jabberwocky should now be followed by a list of punctuation-free, lower case words. -
Apply
filter
to create a stream of words beginning withb
-
Create a second stream of words beginning with
g
-
merge
the two streams -
Optional: distinct, scan
- replace all words in the two current streams with their lengths.
- use
scan
to total the number of chars in words startingb
- use
scan
to total the number of chars indistinct
words startingg
-
Really Really Optional: count
count
how many words started with 'b'count
how manydistinct
words started with 'g'
Note: Akka doesn't have
distinct
but you can use other operations or tricks to create your own version.
Start with the stream of lines (available from the lines()
method):
- split each line in the stream into an array of words, e.g.
Source<String[],NotUsed>
- split each line in the stream into a stream of words, e.g.
Source<Source<String,NotUsed>,NotUsed>
Add operators to the stream to allow dumpSourceToStdOut
to print the individual words again.
- Use
flatMapConcat
orflatMapMerge
to convert the stream from step 1 above into a stream of words (Source<String,NotUsed>
) - Use
flatMapConcat
orflatMapMerge
to convert the stream from step 2 above into a stream of words (Source<String,NotUsed>
)
Start with a stream of (optionally lowercase, punctuation-free) words()
:
- Use
groupBy
to group words by first letter - Use
mergeSubstreams
to re-merge substreams: observe order of words - Optional: distinct
- Include only
distinct
words
- Include only
- Really Really Optional
count
how many words are in each group
Start with stream of (optionally lowercase, punctuation-free) words()
:
- Use
map
to create a stream of string lengths - Use
zipWith
to combine the stream of lengths and stream of words intoSource<Pair,NotUsed>
- Use
flatMap
to show group contents as singleSource<String,NotUsed>
, e.g. "length: string" - Optional
- Use
groupBy
to groupSource<Pair,NotUsed>
by length - Recombine the groups with
mergeSubstreams
- Use
- Really Really Optional
- What happens if you use
concatSubstreams
instead ofmergeSubstreams
? Why?
- What happens if you use