Write all the code examples mentioned below.
Introduce trigram exercise. For our purposes, we'll just examine the trigram-generating part of the algorithm.
Introduce TransformAndConcat as a part of solution. Use single-threaded List-based solution
Recap previous solution Notice that we
Might have to implement a custom collection based on BlockingArrayQueue or somesuch. @see:
Fix/improve this part of the solution (still single-threaded)
But wait! Now that we have a producer-consumer model, we can make a multithreaded IterableHelper that accepts as a parameter the size of the thread pool it should create. We'll cover that next time.
Notice that except for switching to a ProducerConsumerIterable for the initial Lines object, all of our client-side logic (in the function objects) has remained identical.
Make IterableHelper multithreaded.
Run on “War and Peace”; demonstrate speedup / scaling
http://www.gutenberg.org/ebooks/2600
Notice:
Why can we do this? List / BlockingQueue obey the following invariants:
Explain in English:
Say it again in math
What have we done?
This design pattern has a name. Unfortunately, the name is rather obscure and has nothing obvious to do with what it buys us, but you've undoubtably heard that name before:
Monad. Sort of. To be explained in the next blog.
Notice that Possible/Option only encapsulates a value. What if we also want to encapsulate some status information suitable for logging?
Introduce the LoggedResult monad.
LoggedResult<T>(T result, List<IStatus> log)
Notice, we can't implement Iterable over this without losing information: either the result or the log.
But we can implement the following method:
private final A contents; //... public <B, IterableB extends Iterable<B>> LoggedResult<B> transformAndFlatten(F<A,IterableB> transformer) { IterableB result = transformer.apply(contents); for??? // How does one flatten IterableB into a new contents variable? }
What if Observable<T> were Iterable-over-time?
Introduce Observable<T>
Introduce an implementation of Observable<T> that uses continuations (JYield) to become Iterable using the Consumer/Producer pattern described previously.
Is there an Observable mouse event? Down/Move/Up?
If so, rewrite the droppable widget snippet to use foreach rather than separate events…?
Now we can foreach over Observables; notice how algorithms become clearer?