71
Copyright © 2016 Russel Winder 1 The Rise and Rise of Dataflow in the JavaVerse Russel Winder @russel_winder [email protected] https://www.russel.org.uk

The Rise and Rise of Dataflow in the JavaVerse · Copyright © 2016 Russel Winder 71 The Rise and Rise of Dataflow in the JavaVerse. Author: Russel Winder Created Date: 6/14/2016

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Copyright © 2016 Russel Winder 1

The Rise and Rise ofDataflow in the JavaVerse

Russel Winder

@[email protected]

https://www.russel.org.uk

Copyright © 2016 Russel Winder 2

The Plan for the Session● Stuff.● More stuff.● Even more stuff – possibly.● Summary and conclusions – maybe.● Q & A

Notice theflow goingon here.

Copyright © 2016 Russel Winder 3

Some Definitions● Dataflow — moving data from one transformation to another.● Javaverse —

– the Java Platform: the JVM the Java compiler, and the standard library;

– other languages such as Kotlin, Ceylon, Groovy, Scala, Clojure, etc;– other libraries from Bintray, Maven Central, etc.

Copyright © 2016 Russel Winder 4

More on the Term Dataflow● Wikipedia: https://en.wikipedia.org/wiki/Dataflow

– Dataflow can also be called stream processing or reactive programming.

– Dataflow programming is about pipelines.– …data flow programming promotes high-level functional style…

Streams, pipelines imply one-dimensional.

Copyright © 2016 Russel Winder 5

Some Libraries of Interest● GPars — http://www.gpars.org/● Akka — http://akka.io/● Quasar — http://docs.paralleluniverse.co/quasar/● RxJava — https://github.com/ReactiveX/RxJava● Apache Spark — http://spark.apache.org/

Copyright © 2016 Russel Winder 6

What is a Program?

n m

Copyright © 2016 Russel Winder 7

Example

Copyright © 2016 Russel Winder 8

Let’s be a bit abstractfor the moment.

Copyright © 2016 Russel Winder 9

Let Us Assume Java and…

public static Integer f1(final Integer i) { return i * 2; }

public static Integer f2(final Integer i) { return i * 3; }

public static Integer f3(final Integer i) { return i * 4; }

All this in a class.

Copyright © 2016 Russel Winder 10

Compute a Value Given an Input

public static Integer statementSequence(final Integer i) { final Integer i1 = f1(i); final Integer i2 = f2(i1); final Integer i3 = f3(i2); return i3;}

Relatively declarative style.Quite dataflow oriented.

Copyright © 2016 Russel Winder 11

Diagrammatically

Copyright © 2016 Russel Winder 12

Compute a Value Given an Input

public static Integer statementSequence(final Integer i) { final Integer i1 = f1(i); final Integer i2 = f2(i1); final Integer i3 = f3(i2); return i3;}

Relatively declarative style.Quite dataflow oriented.

Copyright © 2016 Russel Winder 13

Compute a Value Given an Input

public static Integer statementSequence(final Integer i) { Integer x = f1(i); x = f2(x); x = f3(x); return x;}

Very traditionalImperative style.

Copyright © 2016 Russel Winder 14

State changevs.

Data flow

Copyright © 2016 Russel Winder 15

Compute a Value Given an Input

A more functional anddeclarative approach.

public static Integer functionApplication(final Integer i) { return f3(f2(f1(i)));}

Copyright © 2016 Russel Winder 16

Diagrammatically

Copyright © 2016 Russel Winder 17

Compilers deal in “dataflow analysis”,let us not forget this view of a program.

Copyright © 2016 Russel Winder 18

That is all very Java,how about doing it with

Frege?

Copyright © 2016 Russel Winder 19

The Frege Transforms

f1 = (* 2)

f2 = (* 3)

f3 = (* 4)

Pointfree definitionof functions usingpartial evaluation.Frege has top-level functions.

Copyright © 2016 Russel Winder 20

Compute a Value Given an Input

bindingSequenceScalar i = f3 i3 where i2 = f1 i i3 = f2 i2

An Imperative stylefunctional approach.

Copyright © 2016 Russel Winder 21

Diagrammatically

Copyright © 2016 Russel Winder 22

Compute a Value Given an Input

functionApplicationScalarExplicit i = f3 (f2 (f1 i))

A more functional anddeclarative approach.

Copyright © 2016 Russel Winder 23

Compute a Value Given an Input

A more functional anddeclarative approach.

functionApplicationScalar i = f3 $ f2 $ f1 $ i

Copyright © 2016 Russel Winder 24

Compute a Value Given an Input

A more functional anddeclarative approach.

functionCompositionScalar i = f3 . f2 . f1 $ i

Copyright © 2016 Russel Winder 25

Diagrammatically

Copyright © 2016 Russel Winder 26

Let’s do the maths…

Copyright © 2016 Russel Winder 27

Function Composition

f 3(f 2(f 1(x))) = (f 3∘ f 2∘ f 1)(x)

It’s only a bit of maths,do not be afraid.

Copyright © 2016 Russel Winder 28

Compute a Value Given an Input

A more functional anddeclarative approach.

functionSavedCompositionScalar i = f i where f = f3 . f2 . f1

Copyright © 2016 Russel Winder 29

This example isseriously unrealistic.

Copyright © 2016 Russel Winder 30

Make it a wee bit more realisticby switching to a

potentially infinite dataset…

Copyright © 2016 Russel Winder 31

…let’s stage this by doinga finite sequence first…

Copyright © 2016 Russel Winder 32

…let’s go(statically compiled) Groovy…

Do not have to have classes,top-level functions are allowed.

Copyright © 2016 Russel Winder 33

The fs but Groovy

Integer f1(final Integer i) { i * 2 }

Integer f2(final Integer i) { i * 3 }

Integer f3(final Integer i) { i * 4 }

Copyright © 2016 Russel Winder 34

Compute a Value Given an InputList<Integer> statementSequence(final List<Integer> l) { final result = new ArrayList<Integer>() for (final Integer i: l) { final i1 = f1(i) final i2 = f2(i1) final i3 = f3(i2) result.add(i3) } result}

Copyright © 2016 Russel Winder 35

Compute a Value Given an InputList<Integer> statementSequence(final List<Integer> l) { final result = new ArrayList<Integer>() for (final Integer i: l) { def x = f1(i) x = f2(x) x = f3(x) result.add(x) } result}

Copyright © 2016 Russel Winder 36

Compute a Value Given an Input

List<Integer> functionApplication(final List<Integer> l) { final result = new ArrayList<Integer>() for (final Integer i : l) { result.add(f3(f2(f1(i)))) } result}

Copyright © 2016 Russel Winder 37

But this is all about state,no real dataflow. So…

Copyright © 2016 Russel Winder 38

Compute a Value Given an Input

List<Integer> usingStream(final List<Integer> l) { l.stream() .map(this.&f1) .map(this.&f2) .map(this.&f3) .collect(Collectors.toList())}

This is using Streams from the Java Platform library.

Copyright © 2016 Russel Winder 39

Diagrammatically

Copyright © 2016 Russel Winder 40

Diagrammatically

Copyright © 2016 Russel Winder 41

Compute a Value Given an Input

List<Integer> usingStream(final List<Integer> l) { l.stream() .map(this.&f1) .map(this.&f2) .map(this.&f3) .collect(Collectors.toList())}

This is using Streams from the Java Platform library.

Intermediate

Terminal

Copyright © 2016 Russel Winder 42

Do this with explicit composition?

Copyright © 2016 Russel Winder 43

Frege

functionCompositionSequence l = map (f3 . f2 . f1) l

Copyright © 2016 Russel Winder 44

Let’s introduce anew language:

Kotlin

Do not have to have classes,top-level functions are allowed.

Copyright © 2016 Russel Winder 45

The Three Functions

fun f1(i:Int):Int = i * 2

fun f2(i:Int):Int = i * 3

fun f3(i:Int):Int = i * 4

We can already tell thatKotlin will be lots of fun.

Copyright © 2016 Russel Winder 46

Using Streams

fun usingStream(l:List<Int>):List<Int> = l.stream() .map(::f1) .map(::f2) .map(::f3) .collect(Collectors.toList<Int>())

Copyright © 2016 Russel Winder 47

Kotlin version

fun usingMap(l:List<Int>):List<Int> = l.map(::f1).map(::f2).map(::f3)

Copyright © 2016 Russel Winder 48

Using composition

fun usingComposedMap(l:List<Int>):List<Int > = l.map(::f1 compose ::f2 compose ::f3)

Copyright © 2016 Russel Winder 49

Kotlin Compose

infix fun<V, T, R> Function1<T, R>.compose(before: (V) -> T): (V) -> R { return { v: V -> this(before(v)) }}

t ∘b (i) = t (b(i))

Copyright © 2016 Russel Winder 50

But this is still finite, whatabout potentially infinite?

Copyright © 2016 Russel Winder 51

Cannot do collect.

Map is another name forcollect in most circumstances.

Copyright © 2016 Russel Winder 52

Infinite Data● With an infinite data sequence you can:

– Do some form of reduction Or windowing.– Perform a side-effect, e.g. output.

Copyright © 2016 Russel Winder 53

New (more realistic) problem…

Copyright © 2016 Russel Winder 54

Cumulative mean andStandard deviation.

Copyright © 2016 Russel Winder 55

Equation warning:please do not be afraid.

Copyright © 2016 Russel Winder 56

x̄=1n∑i=0

nxi

s=√ 1n−1∑i=0

n(x i− x̄)2

Copyright © 2016 Russel Winder 57

x̄=1n∑i=0

nxi

s=√ 1n−1

((∑i=0

nx i

2)−n x̄2)

Copyright © 2016 Russel Winder 58

Copyright © 2016 Russel Winder 59

I only do equations after being fed…

Copyright © 2016 Russel Winder 60

Code?

Copyright © 2016 Russel Winder 61

A little architecture first.

Copyright © 2016 Russel Winder 62

Copyright © 2016 Russel Winder 63

Code.

Copyright © 2016 Russel Winder 64

What’s the Message?● Small, single threaded, communicating processes are easy to

program. (Communicating Sequential Processes, CSP)● Threadpools and processpools make parallelism easy to

realize, without manual locks.● Most calculations and dataset are now very big, hence “Big

Data”.

Copyright © 2016 Russel Winder 65

Parallelism is mandatoryFor “Big Data”.

Copyright © 2016 Russel Winder 66

Dataflow not stateis required for parallelism.

Copyright © 2016 Russel Winder 67

The Rise and Rise ofDataflow in the JavaVerse

Russel Winder

@[email protected]

https://www.russel.org.uk

Copyright © 2016 Russel Winder 68

But before I go…

Copyright © 2016 Russel Winder 69

Copyright © 2016 Russel Winder 70

Q & A

Copyright © 2016 Russel Winder 71

The Rise and Rise ofDataflow in the JavaVerse

Russel Winder

@[email protected]

https://www.russel.org.uk