• Save
Closures, the next "Big Thing" in Java: Russel Winder
Upcoming SlideShare
Loading in...5
×
 

Closures, the next "Big Thing" in Java: Russel Winder

on

  • 1,183 views

Java 8 will bring lambda function and closures to the Java-verse. These tools will revolutionize programming using Java, leading to shorter more declarative code. But how can we prepare ourselves for ...

Java 8 will bring lambda function and closures to the Java-verse. These tools will revolutionize programming using Java, leading to shorter more declarative code. But how can we prepare ourselves for this? A number of JVM-based languages already have support for lambda functions and closures: dynamically-typed languages like Groovy, and statically-typed languages like Scala and Kotlin. Can looking at examples of how to solve problems in these languages teach us how to do things in Java 8? Yes. Of course the observation of how programming changes may get people to start using Groovy, Scala, Kotlin, etc. instead of Java. The critical thing here is that we can realize the revolution in an evolutionary way. The JVM allows for all of these languages to work together, we can program bits in Java, bits in Groovy, bits in Scala, etc. So we can evolve extant systems by rewriting bits a little at a time. In this session we will look at these issues and a collection of examples to try and ascertain what the idioms might be in a post-Java 8 world.

Statistics

Views

Total Views
1,183
Views on SlideShare
1,175
Embed Views
8

Actions

Likes
0
Downloads
0
Comments
0

2 Embeds 8

http://lanyrd.com 6
http://dschool.co 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Closures, the next "Big Thing" in Java: Russel Winder Closures, the next "Big Thing" in Java: Russel Winder Presentation Transcript

  • Closures The Next “Big Thing” in Java Russel Winder email: russel@winder.org.uk xmpp: russel@winder.org.uk twitter: @russel_winderCopyright © 2012 Russel Winder 1
  • Closures The Next “Big Thing” in Java? Russel Winder email: russel@winder.org.uk xmpp: russel@winder.org.uk twitter: @russel_winderCopyright © 2012 Russel Winder 2
  • Aims, Goals and Objectives ● Investigate why some idioms from Java 0–7 lead to bad code in a post-Java 7 world. ● Show what some of the idioms of Java 8 and beyond are likely to be. ● Survive to the end of the session.Copyright © 2012 Russel Winder 3 View slide
  • Structure A beginning. A middle. An end.Copyright © 2012 Russel Winder 4 View slide
  • Interstitial AdvertisementCopyright © 2012 Russel Winder 5
  • A BeginningCopyright © 2012 Russel Winder 6
  • Many people are still using Java 1.4.Copyright © 2012 Russel Winder 7
  • They see no reason to move to any later version of Java: no real benefit, for too much pain.Copyright © 2012 Russel Winder 8
  • Java 5 is a barrier too far for them.Copyright © 2012 Russel Winder 9
  • The Java 7 → Java 8 change is an even more disruptive change.Copyright © 2012 Russel Winder 10
  • This possibly means Java is now a legacy language?Copyright © 2012 Russel Winder 11
  • No, its an opportunity to stop programming using 1970s techniques and start using 1930s ones.Copyright © 2012 Russel Winder 12
  • Alonzo Church Alan Turing Stephen Kleene J B RosserCopyright © 2012 Russel Winder 13
  • And an opportunity to do 1990s programming.Copyright © 2012 Russel Winder 14
  • i.e. We finally get to do real object-oriented programming using Java.Copyright © 2012 Russel Winder 15
  • Another BeginningCopyright © 2012 Russel Winder 16
  • What is a “closure”?Copyright © 2012 Russel Winder 17
  • A closure is a function with an associated environment containing values for all the free variables in the function.Copyright © 2012 Russel Winder 18
  • A Function Integer f(final Integer x) { return x * y ; } Free variable.Copyright © 2012 Russel Winder 19
  • A closure should be referentially transparent. Whenever function f is evaluated on the same value, a say, then the same value is returned. f(a) = bCopyright © 2012 Russel Winder 20
  • Java has had things (sort of) like this since (almost) the beginning…Copyright © 2012 Russel Winder 21
  • A closure can be realized as an instance of a class with a single method and single assignment fields.Copyright © 2012 Russel Winder 22
  • public class ClosureClass { private final Integer multiplier; public ClosureClass(final Integer m) { multiplier = m; } public Integer call(final Integer i) { return multiplier * i; } } No free variables in call.Copyright © 2012 Russel Winder 23
  • final ClosureClass multiplyBy5 = new ClosureClass(5); multiplyBy5.call(4)Copyright © 2012 Russel Winder 24
  • Alternatively…Copyright © 2012 Russel Winder 25
  • public interface ClosureInterface<T> { T call(T t); } final ClosureInterface<Integer> multiplyBy5 = new ClosureInterface<Integer>() { public Integer call(final Integer i) { return 5 * i; } }; multiplyBy5.call(4)Copyright © 2012 Russel Winder 26
  • If Java had operator overloading, this could be made to look much nicer.Copyright © 2012 Russel Winder 27
  • Just as it can in proper programming languages such as Groovy, Scala, C++, Python, etc.Copyright © 2012 Russel Winder 28
  • final multiplyBy5 = {i -> 5 * i} final multiplyBy5 = {5 * it} multiplyBy5(4) multiplyBy5.call(4)Copyright © 2012 Russel Winder 29
  • def multiplyBy5(i: Int): Int = 5 * i multiplyBy5(4)Copyright © 2012 Russel Winder 30
  • But Java 8 can do stuff a bit like that…Copyright © 2012 Russel Winder 31
  • final ClosureInterface<Integer> multiplyBy5 = i -> 5 * i; multiplyBy5.call(4)Copyright © 2012 Russel Winder 32
  • Nothing much revolutionary here, just a bit of syntactic sugar…Copyright © 2012 Russel Winder 33
  • …true, but that isnt all there is…Copyright © 2012 Russel Winder 34
  • A MiddleCopyright © 2012 Russel Winder 35
  • It all about where the iteration is.Copyright © 2012 Russel Winder 36
  • Explicit iteration vs. Implicit iterationCopyright © 2012 Russel Winder 37
  • Work with a (trivial) example:Copyright © 2012 Russel Winder 38
  • Calculate the sum of the squares of the numbers between 0 and 100 that are divisible by 7.Copyright © 2012 Russel Winder 39
  • final List<Integer> numbers = new ArrayList<>(); for (int i = 0; i < 100; ++i) { if (i % 7 == 0) { numbers.add(i); } } Integer sum = 0 ; for (final Integer i: numbers) { sum += i * i; }Copyright © 2012 Russel Winder 40
  • (for (i <- 0 until 100; if i % 7 == 0) yield i * i).sumCopyright © 2012 Russel Winder 41
  • sum(i * i for i in range(100) if i % 7 == 0)Copyright © 2012 Russel Winder 42
  • (0 until 100).filter(i => i %7 == 0).map(i => i * i).sumCopyright © 2012 Russel Winder 43
  • (0..100).findAll{i -> i % 7 == 0}.collect{i -> i * i}.sum()Copyright © 2012 Russel Winder 44
  • (0..100).findAll{it % 7 == 0}.collect{it * it}.sum()Copyright © 2012 Russel Winder 45
  • numbers.filter(i -> i % 7 == 0).map(i -> i * i).reduce(0, (t, x) -> t + x) Huh?Copyright © 2012 Russel Winder 46
  • final List<Integer> numbers = new ArrayList<>(); for (int i = 0; i < 100; ++i) { numbers.add(i); } Oh ffs.Copyright © 2012 Russel Winder 47
  • Higher Order FunctionsCopyright © 2012 Russel Winder 48
  • Functions that take functions as parameters and/or return functions as result.Copyright © 2012 Russel Winder 49
  • Some may be thinking: Why do I give a f###Copyright © 2012 Russel Winder 50
  • After all nothing good has happened in Java since Java 1.4.2.Copyright © 2012 Russel Winder 51
  • Copyright © 2012 Russel Winder 52
  • Because all computers are now parallel computers.Copyright © 2012 Russel Winder 53
  • Copyright © 2012 Russel Winder 54
  • We have to move to an attitude where we assume our software is not uniprocessor.Copyright © 2012 Russel Winder 55
  • We have to actually do object-oriented and functional programming.Copyright © 2012 Russel Winder 56
  • Instead of just saying we write Java and so are doing object-oriented programming.Copyright © 2012 Russel Winder 57
  • numbers.parallel().filter(i -> i % 7 == 0). map(i -> i * i).reduce(0, (t, x) -> t + x)Copyright © 2012 Russel Winder 58
  • GParsPool.withPool { value = (0..100).findAllParallel{it % 7 == 0}.collectParallel{it * it}.sum() }Copyright © 2012 Russel Winder 59
  • GParsPool.withPool { value = (0..100).parallel.filter{it % 7 == 0}.map{it * it}.sum() } Very Java 8.Copyright © 2012 Russel Winder 60
  • def value = (0..100) ParallelEnhancer.enhanceInstance(value) value = value.parallel.filter{it % 7 == 0}.map{it * it}.sum()Copyright © 2012 Russel Winder 61
  • def value = (0..100) ParallelEnhancer.enhanceInstance(value) value = value.findAllParallel{it % 7 == 0}.collectParallel{it * it}.sum()Copyright © 2012 Russel Winder 62
  • GPars and Groovy give you Java 8 style approach and parallelism today for Java with JDK6 or JDK7.Copyright © 2012 Russel Winder 63
  • Guava, TotallyLazy, and FunctionalJava can be used today to practice the functional approach using Java. Or just install the JDK8 Lambda release.Copyright © 2012 Russel Winder 64
  • Using Scala is an option for doing functional programming*.Copyright © 2012 Russel Winder *And just ignore Java altogether. 65
  • (0 until 100).par.filter(i => i %7 == 0).map(i => i * i).sumCopyright © 2012 Russel Winder 66
  • An EndCopyright © 2012 Russel Winder 67
  • Java is about to get the functional programming aspect.Copyright © 2012 Russel Winder 68
  • Java is about to become an object-oriented language.Copyright © 2012 Russel Winder 69
  • Scala, Groovy, Python, C++, etc. already have object-oriented and functional.Copyright © 2012 Russel Winder 70
  • Its all about how your data evolves. Its not about the flow of control.Copyright © 2012 Russel Winder 71
  • Its all about sending messages to your objects, requesting activity.Copyright © 2012 Russel Winder 72
  • Closure the next “big thing” in Java?Copyright © 2012 Russel Winder 73
  • Copyright © 2012 Russel Winder 74
  • Squirrels deny parallelism.Copyright © 2012 Russel Winder 75
  • Copyright © 2012 Russel Winder 76
  • Yes*. *But will everyone ignore it?Copyright © 2012 Russel Winder 77
  • No.Copyright © 2012 Russel Winder 78
  • It is not the lambda expressions in Java 8 that is the disruptive revolution. Its the change to the Java library that is. Its all about those defender methods.Copyright © 2012 Russel Winder 79
  • Surreptitious AdvertisementCopyright © 2012 Russel Winder 80
  • The EndCopyright © 2012 Russel Winder 81
  • Closures The Next “Big Thing” in Java? Russel Winder email: russel@winder.org.uk xmpp: russel@winder.org.uk twitter: @russel_winderCopyright © 2012 Russel Winder 82