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
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 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 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 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 17
Compilers deal in “dataflow analysis”,let us not forget this view of a program.
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 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 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 30
Make it a wee bit more realisticby switching to a
potentially infinite dataset…
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 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 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 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 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 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 67
The Rise and Rise ofDataflow in the JavaVerse
Russel Winder
https://www.russel.org.uk
Copyright © 2016 Russel Winder 71
The Rise and Rise ofDataflow in the JavaVerse
Russel Winder
https://www.russel.org.uk