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

1,057 views
984 views

Published on

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.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,057
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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

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

×