Advanced Functional Programming in Scala

77
Functional Programming in Scala Patrick Nicolas Oct 2013 Rev. July 2015 patricknicolas.blogspot.com www.slideshare.net/pnicolas

Transcript of Advanced Functional Programming in Scala

PowerPoint Presentation

Advanced Functional Programming in ScalaPatrick NicolasOct 2013Rev. July 2015

patricknicolas.blogspot.comwww.slideshare.net/pnicolas

Context of the presentation:The transition from Java and Python to Scala is not that easy: It goes beyond selecting Scala for its obvious benefits.- support functional concepts- leverage open source libraries and framework if needed- fast, distributed enough to handle large data setsScala was the most logical choice.

Scientific programming may very well involved different roles in a project:Mathematicians for formulasData scientists for data processing and modelingSoftware engineering for implementationDev. Ops and performance engineers for deployment in production

In order to ease the pain, we tend to to learn/adopt Scala incrementally within a development team.. The problem is that you end up with an inconsistent code base with different levels of quality and the team developed a somewhat negative attitude toward the language.

The solution is to select a list of problems or roadblocks (in our case Machine learning) and compare the solution in Scala with Java, Python ... (You sell the outcome not the process).

PresentationA set of diverse Scala features or constructs the entire team agreed that Scala is a far better solution than Python or Java.1

This is an overview of some interesting advanced features of Scala. It is not meant to be a tutorial and assume that you are familiar with the key constructs of the language.

Some of the examples are extracted from Scala for Machine Learning Packt Publishing

Disclaimer

2

Scala has a lot of features ..ImplicitsCake patternMonadsTail recursionHotswapActorsDSLParallel collectionsView boundsComposed futuresF-boundStreamsDelimited continuationReactiveLazynessMixinsMacrosPartial functionMemoizationWeak referencesFuturesBack-pressureSupervisionDependency injectionGuardsType classesADTCurryingBifunctorContra-variant functorsMagnet patternETA expansionHigher Kind projectionAdvanced functional programming? ... among them

Being an object oriented and functional language, Scala has a lot of features and powerful constructs to choose from.Here is a list of some of the features of Scala that are particularly valuable for writing scientific workflows, machine learning algorithms and complex analytics solutions.

3

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

4

Functors and monads are defined as single type higher kinds: M[_]. The problem is to define monadic composition for objects belongs to categories that have two or more types M[_, _] ( i.e. Function1[U, V] ). Higher kind projectionScala support functorial and monadic operations for multi-type categories using higher kind type projection

5

Higher kind projectionLet us consider a covariant functor F that applies a morphism f within a category C defined as

The definition of a functor in Scala relies on a single type higher kind M (*) Functors are important concepts in algebraic topology used in defining algebra for tensors for example.

6

Higher kind projectionHow can we define a functor for classes that have multiple parameterized type?Lets consider the definition of a tensor using Scala Function1

The covariant CoVector (resp. contravariant Vector) vectors are created through a projection onto the covariant (resp. contravariant) parameterized type T of Function1.

7

Higher kind projectionThe implementation of the functor for the Vector type uses the projection of the higher kind Function1 to its covariant component by accessing # the inner type Vector of Tensor

The map applies covariant composition, compose of Function1

8

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

9

Contravariant functorsSome categories of objects such as covariant tensors or function parameterized on the input or contravariant type (i.e. T => Function1[T, U] for a given type U), require the order of morphisms be reversed.Morphisms on contravariant argument type are transported through contravariant functors.

10

Contravariant functorsLet us consider a contravariant functor F that applies a morphism f within a category C defined as

The definition of a contravariant functor in Scala relies on a single type higher kind M

11

Contravariant functorsThe implementation of the contravariant functor for the CoVector type uses the projection of the higher kind Function1 to its covariant component by accessing # the inner type CoVector of Tensor The map applies covariant composition, andThen of Function1

Geometric entities on a differential (Riemann) manifold are defined as tensors. Tensor can beCovariant Contra variantA bilinear form such as Tensor productInner productn-differential forms12

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

13

It is quite common to compose, iteratively or recursively functions, methods or data transformations.TensorsVector V on field FCo-vector/ map V => FBi-linear forms V x V => VInner product V x V => FTensor product Monadic composition Monads extends the concept of functor to support composition (or chaining) of computation into a chain

14

Monads are abstract structures in algebraic topology related to the category theory. A category C is a structure which hasobject {a, b,c...} morphism or maps on objects f: a->bcomposition of morphisms f: a->b, g: b->c => f o g: a->c

Monads enable the monadic composition or chaining of functions or computation on single type argument.

Monadic composition

Some useful references, on the theory of categories and monads ..1 One Div Zero Monads are Elephants Part 2 J. Iry Blog 2007 http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.html

2. Monad Design for the Web 7 A Review of Collections as Monads L.G. Meredith Artima 2012

15

Lets consider the definition of a kernel function Kf as the composition of 2 functions g o h.

Monadic composition We create a monad to generate any kind of kernel functions Kf, by composing their component g: g1 o g2 o o gn o h

For better understanding of kernel functions in Machine learningIntroduction to Machine Learning Nonparametric Regression: Smoothing Models. E. Alpaydin MIT Press 2007

A Short Introduction to Learning with Kernels B. Scholkopt, Max Planck Institut f ur Biologische Kybernetik A. Smola Australian National University 2005 http://alex.smola.org/papers/2003/SchSmo03c.pdf

The purpose here is to generate and experiment with any kind of explicit kernels by defining and composing two g and h functionsA function h operates on each feature or component of the vector A function g is the transformation of the dot product of the two vectors. The dot product is computed by applying the function to all the elements and compute the sum.

The dot product K is computed by traversing the two observations (vector of features), computing the sum and finally applying the g transform. The variable type is the type of the function g (F1 = Double => Double)

16

A monad extends a functor with binding method (flatMap)The monadic definition of the kernel function component h

Monadic composition

Once the functor is defined, the monad is created by adding the flatMap method. The monad, KFMonad which take a kernel function as argument is defined as an implicit class so kernel functions inherits the monadic methods.

The map and flatMap transformation applies to the g function or transformation on the inner product.The flatMap method is implemented by creating a new Kernel and applying the transformation to only one of the component of the Kernel function: function h (in red). This partial monadic operation is good enough for building Kernel functions on the fly.17

Example of Kernel functionsPolynomial kernelMonadic composition

Radius basis function kernel

Kernel functions that project the inner product to the manifold for non-linear models belong to the family of exponential functions Polynomial functions and Radius basis functions are two of the most commonly used kernel functions. Note: The source code is shown here to illustrate the fact that the implementation in any other language would be a lot more messy and wont be able to fit in any of those slides.18

The monadic composition consists of chaining the flatMap invocation on the functor, map, that preserves morphisms on kernel functions.

Monadic composition The for comprehension closure is a syntactic sugar on the iterative monadic composition.

Finally we can chain flatMap to map to compose two kernel function and compute the dot/inner product of the resulting kernel function.Note that composed kernel function used the h function of the last invocation in the for instruction.

The method does not expose the functor or KF classes that wraps the components of the kernel function.19

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

20

Streams Streams reduce memory consumption by allocating and releasing chunk of data (or slice or time series) while allowing reuse of intermediate results.

Some problems lend themselves to process very large data sets of unknown size for which the execution may have to be aborted or re-applied

Real-time streaming is becoming popular (i.e. Apache Spark streaming library, Akka reactive streams.). Short of using one of these frameworks, you can create a simple streaming mechanism for large data sets that require.

Streams vs. Iterator: Iterator does not allow to dynamically select the chunk of memory or preserve it if necessary for future computation.

It is not uncommon to have to train a model as labeled data becomes available (online training). In this case, the size of the data set can be very large and unknown. The processing of the data would result in high memory consumption and heavy load on the Garbage collector.Finally, the traversing the entire data sets (~ allocating large memory chunk) may not even needed as some computation may abort if some condition is met.

Scalas streams can help!21

The large data set is converted into a stream then broken down into manageable slices. The slices are instantiated, processed (i.e. loss function) and released back to the garbage collector, one at the time X0 X1 .... Xn . Xm Data stream Garbage collector

XiAllocate slice .takeRelease slice .drop

Heap Traversal loss function

Streams

In order to minimize the memory footprint, two actions have to take placeAllocate slice of the data set (memory chunk) from the heap using take method.Release the memory chunk back to the Garbage collection through a drop method.

This example is taking from Scala for Machine Learning Packt Publishing.

Most of machine learning algorithms consists of minimizing a loss function or maximizing a likelihood. For each iteration (or recursion) the cumulative loss is computed and passed to the optimizer (i.e. Gradient descent or variant) to update the model parameters. Each slice of n observations is allocated, processed by the loss function then released back to the Garbage collector.22

Slices of NOBS observations are allocated one at the time, (take) processed, then released (drop) at the time.Views and streams

The Loss class has a single method, exec, that traverses the stream. Once again, the loss is computed using a tail recursion.

An observation is defined as y = f(x) where x the feature set containing for instance, age, ethnicity of patient, body temperature and y is the label value such as the diagnosed disease.. The tail recursion allocates the next slice of STEP observations through the take method, computes the lost, nextLoss, then drop the slice. The reference is recursively redefined as the reference to the remaining stream.

The problem is that the garbage collector cannot reclaim the memory because the first reference to the stream is created outside the recursion. The solution is to declare the reference to the stream as weak so it chunks of memory associated to the slices/batches of observations already processed can be reclaimed.23

The reference streamRef has to be weak, in order to have the slices garbage collected. Otherwise the memory consumption increases with each new batch of data.

(*) Alternatives: define strmRef as a def or use StreamIteratorViews and streams

The reference of the stream is created as a Weak java reference to an instance created by the stream constructor, Stream.iterate.In this case, the weak reference has been used to show Java concepts are still relevant.24

Comparing list, stream and stream with weak references.

Views and streams

Operating zone

Lets compare the memory consumption of three strategies to compute the loss function on a very large dataset.A listA stream with standard referenceA stream with weak reference

In the first scenario, and as expected, the memory for the entire data set is allocated before processing. The memory requirement for the stream with a strong reference increases each time a new slice is instantiated, because the memory block is held by the reference to the original stream. Only the stream with a weak reference guarantees that only the memory for a slice of STEP observations is needed through the entire execution.

25

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

26

Views Scientific computations require chaining complex data transformations on large data set. There is not always a need to process all elements of the dataset.

Scala allows the creation of a view on collections that are the result of a data transformation. The elements are instantiated only once needed.

Views

Accessing an element of the list requires allocating the entire list in memory.Accessing an element of the view requires allocating only this element in memory.

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

29

Type classes Scala libraries classes cannot always be sub-classed. Wrapping library component in a helper class clutters the design.

Type classes extends classes functionality without cluttering name spaces (alternative to type classes)The purpose of reusability goes beyond refactoring code. It includes leveraging existing well understood concepts and semantic.

30

Lets consider the definition of a tensor as being either a vector or a covector.Type classes

Lets extend the concept of tensor with. A metric is computed as the inner product or composition of a Covector and a vector. The computationis implemented by the method Metric.apply

31

Type classes The inner object Metric define the implicit conversion

32

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

33

Stacked mixins modelsScala stacked traits and abstract values preserve the core formalism of mathematical expressions. Traditional programming languages compare unfavorably to scientific related language such as R because their inability to follow a strict mathematical formalism:Variable declarationModel definitionInstantiation

The first thing to come to mind in creating complex system from existing objects (or classes) is a factory design pattern.Design patterns have been introduced by the Gang of four in the eponymous Design Patterns: Elements of Reusable Object-Oriented software some 20 years ago The list of factory design patterns includes Builder, Prototype, Factory method, Composite, Bridge and obviously Singleton.

Those patterns are not very convenient for weaving data transformation (these transformation being defined as class or interface). This is where dependency injection popularized by the Spring framework comes into play.

Beyond composition and inheritance, Scala enables us to implement and chain data transformations/reductions by stacking the traits that declare these transformations or reductions.34

DeclarationModelInstantiation

Stacked mixins models

35

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

36

Stacked mixins modelsBuilding machine learning apps requires configurable, dynamic workflows

Leverage mixins, inheritance and abstract values to create models and weave data transformation. Factory design patterns have been used to model dynamic systems (GoF). Dependency injection has gain popularity for creating configurable systems (i.e. Spring framework).

The first thing to come to mind in creating complex system from existing objects (or classes) is a factory design pattern.Design patterns have been introduced by the Gang of four in the eponymous Design Patterns: Elements of Reusable Object-Oriented software some 20 years ago The list of factory design patterns includes Builder, Prototype, Factory method, Composite, Bridge and obviously Singleton.

Those patterns are not very convenient for weaving data transformation (these transformation being defined as class or interface). This is where dependency injection popularized by the Spring framework comes into play.

Beyond composition and inheritance, Scala enables us to implement and chain data transformations/reductions by stacking the traits that declare these transformations or reductions.37

Stacked mixins modelsMultiple models and algorithms are typically evaluated by weaving computation tasks.

A learning platform is a framework thatDefine computational tasksWires the tasks (data flow)Deploys the tasks (*)

Overcome limitation of monadic composition (3 level of dynamic binding)

(*) Actor-based deployment

We briefly mentioned that the for comprehension can be used to chain/stack data transformation. Dependency injection provides a very flexible approach to create workflows dynamically, sometimes referred as the Cake pattern.

Note: As far the 3rd point, deployment of tasks, it usually involves a actor-based (non blocking) distributed architecture such as Akka and Spark. We will mention it briefly later in this presentation is introducing mailbox back-pressure mechanism.38

Even the simplest workflow, defined as a pipeline of data transformations requires a flexible design Stacked mixins models

Lets look at the Training module/task as an example. The task of training a model is executed by a Supervisor instance that can be either a support vector machine or a multi-layer perceptron, in this simplistic case. Each of these two supervisors can have several implementations (single host, distributed through a low-latency network,)

Once defined, the modules are to be weaved/chained by making sure that output of a module/tasks matches the input of the subsequent task.

Notes: The training module can be broken down further into generative and discriminative models.Real-world applications are significantly more complex and would include REST service, DAO to access relational database, caches.The terms module, tasks or computational tasks are used interchangeably in this section. 39

Stacked mixins modelsSummary of the 3 configurability layers of Cake pattern Given the objective of the computation, select the best sequence of module/tasks (i.e. Modeling: Preprocessing + Training + Validating)Given the profile of data input, select the best data transformation for each module (i.e. Data preprocessing: Kalman, DFT, Moving average.)Given the computing platform, select the best implementation for each data transformation (i.e. Kalman: KalmanOnAkka, Spark)

40

Implementation of Preprocessing module

Stacked mixins models

Lets consider the Preprocessing module (or task) implemented as trait

Preprocessing of a data set is performed by a processor of type Preprocessor that is defined at run time. Therefore the preprocessor has to be declared as an abstract value.

The three preprocessors defined in the preprocessing modules are Kalman filter, Moving Average (MovAv) and Discrete Fourier filter (DFTF). Those 2 inner classes act as adapter or stub to the actual implementation of those algorithm. 41

Implementation of Preprocessing module using discrete Fourier

and discrete Kalman filter

Stacked mixins models

Here is an implementation of the Kalman and Discrete Fourier transform band-pass filter. The2 inner classes, Kalman and DFTF act as adapter or stub to the actual implementation of those algorithm. It allows the implementation may consist of multiple version. For instance filtering.Kalman is a trait with several implementation of the algorithm (single host, distributed, using Spark)

Such design allows to Select the type of preprocessing algorithm within the Preprocessing module or namespaceSelect the implementation of this particular type of algorithm/preprocessor in the filtering package

42

dd

Preprocessing

Loading

Reducing

Training

ValidatingPreprocessorDFTFilterKalmanEMPCASVMMLPReducerSupervisor

ClusteringClustering workflow = preprocessing task -> Reducing task

Modeling workflow = preprocessing task -> model training task -> model validation

ModelingStacked mixins models

43

A simple clustering workflow requires a preprocessor & reducer. The computation sequence exec transform a time series of element of type U and return a time series of type W as option

Stacked mixins models

From the data management perspective, Clustering implements two consecutive data transformations: preprocessing and dimension reduction.44

A model is created by processing the original time series of type TS[T] through a preprocessor, a training supervisor and a validator

Stacked mixins models

Modeling workflow is created by chaining an implementation of the filter, training and validator, all selected at run-time. Modeling is therefore implemented as a stack of 3 traits, each representing a transformation or reduction on data sets.

45

Putting all together for a conditional path execution Stacked mixins models

Computational tasks related to machine learning can be complex and lengthy. The process should be able to select the appropriate date flow (or sequence of data transformation or reduction) at run time, according to the state of the computation.

In this simple case, a clustering task is triggered if anomalyDetection is needed, training a model is launched otherwise. These conditional path execution are important for complex analysis or lengthy computation that require unattended execution (i.e. overnight or over the week-end).

Note: The overriding of the abstract value for the Modeling workflow are omitted here for the sake of clarity

Summary: This factory pattern operates on 3 level of componentization: Dynamic selection of 1- Workflow or sequence of tasks according to the objective of the computation (i.e. Clustering => Preprocessing)2- Task processing algorithm according to the data (i.e. Preprocessing => Kalman filter)3- Implementation of task processing according to the environment (i.e. Kalman filter => Implementation on Apache Spark)

46

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

47

Magnet patternMethod overloading in Scala has limitations:Type erasure in the JVM causes collision of type of arguments in overloaded methodsOverloaded methods cannot be lifted into a functionCode may be unecessary duplicatedThe magnet pattern overcomes these limitations by encapsulating the return and redefining the overloaded methods as implicit functions.

Magnet patternLets consider the following three incarnations of the method test

These methods have different return types. The first and last methods conflict because of type erasure on T => List[Double]

Magnet pattern

Step 1: Define generic return type and constructorStep 2: Implement the test methods as implicits

Magnet patternStep 3: Implement the lifted function test as follows

The first call invokes the implicit fromTN and the second triggers the implicit fromT.The return type is inferred from the type of argument

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

52

View boundContext bound cannot be used to bind the parameterized type of a generic class to a primitive type.

Scala view bounds allows to create developers to create class with parameterized types associated to a Scala or Java primitive type.

View boundLets consider a class which parameterized type can be manipulate as a Float.Context bound is not permissible

Constraining the type with a upper bound Float does not work as Float is a final class.

View boundThe solution is to bind the class type to a Float using an implicit conversion (or view)

The supervision strategyCascading bottleneck => Mailbox back-pressure strategy

WorkersRouter, Dispatcher,

We mentioned earlier that a learning platform requires implementing, wiring and deploying tasks. Akka or framework derived from Akka, are commonly used to deploy workflow for large datasets because of the immutability, non-blocking and supervision characteristic for actors.

Scala/Akka actors are resilient because that are defined with hierarchical context for which an actor because a supervisor to other actors. In this slide, a router is a supervising actor to the workers and, depending on the selected strategy, is responsible of restarting a worker in case of failure.

But, what about the case for which the load (number of messages in the 63

Messages passing scheme to process various data streams with transformations.

DatasetWorkersControllerWatcherLoad->Compute->

Bounded mailboxes Completed-> Data flow back pressure

In this example, an actor, Controller loads chunk of data, partitions and distributed across multiple Worker actors, along with a data transformation.Upon receiving the message Compute the workers process data given a transformation function. The workers returns the processed data through a Completed message.

The purpose of the watch dog actor, Watcher is to monitor the utilization of mailbox and report it to the ControllerThis is a simple feedback control:1- Watcher monitors the utilization of the mailbox (average length)2- The controller adjust the size of each batch in the load message handler (throttling)3- The workers process the next batch

64

Worker actors processes data chunk msg.xt sent by the controller with the transformation msg.fctMessage sent by collector to trigger computationData flow back pressure

Lets start with the Worker actor.The load on a worker depends on three variables1- The amount of data to process2- The complexity of the data transformation3- The underlying system (cores, memory..)

The controller provides the slice of data to be processed by the workers msg.xt as well as the data transformation msg.fct.

65

Watcher actor monitors messages queues report to collector with Status message.GetStatus message sent by the collector has no payload

Data flow back pressure

Lets look at our watch dog actor, Watcher: It computes the load as the average mailbox utilization and send it back to the controller through a Status message.

66

Controller creates the workers, bounded mailbox for each worker actor (msgQueues) and the watcher actor.

Data flow back pressure

As its name implies, the controller configure and manage the dynamic workflow and control the back pressure from the worker actors.As far as the configuration is concerned, the Controller generates a list of workers, the bounded mailboxes, msgQueues, for the workers and ultimately the watcher actor.The worker and watcher are created within the Controller context using the Akka actorOf constructor.

67

The Controller loads the data sets per chunk upon receiving the message Load from the main program. It processes the results of the computation from the worker (Completed) and throttle the input to workers for each Status message.

Data flow back pressure

As far as the management of data flow and feedback control loop, the Controller loads partition and distribute batch of data points to the worker actors (message: Load)processes the results of the computation in workers (message: Completed)- Throttle up or down the flow upon receiving the status on utilization of mail boxes from the watcher (message: Status)

68

The Load message is implemented as a loop that create data chunk which size is adjusted according to the load computed by the watcher and forwarded to the controller, Status

Data flow back pressure

The composition of the messages processed by the controller are self-explanatory. It Adjusts the size of the next batch if required (throttle method)Extracts the next batch of data from the input streamPartition and distribute the batch across the worker actorsSend the partition along with the data transformation to the workers.

69

Simple throttle increases/decreases size of the batch of observations given the current load and specified watermark.Data flow back pressure

Selecting faster/slower and less/more accurate version of algorithm can also be used in the regulation strategy

The implementation of the throttle method is rather simple. It takes the load computed by the watcher actor and the current batch size (number of data points to be processed) as input. It update the batch size using a simple ratio relative to the watermark. For instance if load is below the watermark, the batch size is increased.

70

Feedback control loop adjusts the size of the batches given the load in mail boxes and complexity of the computation

Data flow back pressure

The bottom graph describes the throttle action and the complexity of data transformation.The complexity of the data transformation and has an impact on the load on workers. It varies from 0 (simple map operation) to 2 (complex data processing involving recursion or iterations).The throttle intensity ranges between -6 (rapid decrease of size of batches) and +6 (rapid increase of size of batches of data)

The top graph displays the actual utilization of the mail boxes with capacity of 512 messages as regulated by the feedback control loop (executed by the controller).

71

Feedback control loop should be smoothed (moving average, Kalman)

A larger variety of data flow control actions such as adding more workers, increasing queue capacity,

The watch dog should handle dead letters, in case of a failure of the feedback control or the workers.

Reactive streams introduced in Akka 2.2+ has a sophisticated TCP-based propagation and back pressure control flows

NotesData flow back pressure

The deployment of a reactive data flow in production would require significant improvement on our Nave model.The feedback control loop could be smoothed with a moving average technique or Kalman filter to avoid erratic behaviorWe would need to provide a larger range of options for control actions beside adjusting the size of data batches: increase of number of workers, mail box capacity, caching strategy, .. A fine grained set of actions reduces also the risk of instable systems.The watch dog should be able to handle dead letters in case of failure (mailbox overflowing)Reactive streams control the flow back pressure at the TCP connection level. It is far more accurate and responsive that mailbox utilization.

72

Higher kind projectionContravariant functorsMonadic compositionStreamsViews Type classesStacked mixins models

Cake patternMagnet patternView boundsF-bound polymorphismDataflow back pressureContinuation passing style

73

Delimited continuation Continuation Passing Style (CPS) is a technique that abstracts computation unit as a data structure in order to control the state of a computer program, workflow or sequence of data transformations

Continuations are used to jump to a method that produces a call to the current method. They can be regarded as functional GOTO

74

Delimited continuation A data transformation (or computation unit) can be extended (continued) with another transformation known as continuation. The continuation is provided as argument of the orginal transformation.Lets consider the following workflow

The first workflow is not a continuation, the second is

75

Delimited continuation A delimited continuation is a section of the workflow that is reified into a function returning a value. This technique relies on control delimiters (shift/reset) to make the continuation composable and reusable.

76

More Scala nuggets Domain specific languageReactive streamsBack-pressure strategy using connection state

Wait a minute, there is more..

77