In this JEP Café, we will use a lot of the new features added to the JDK in the context of the Data Oriented Programming support: Records, Sealed Types, Pattern Matching, Unnamed Patterns, Exhaustive Switches, as well as the most recent additions to the Stream API: mapMulti(), and Gatherers (a preview feature of the JDK 22). As a bonus, you will see an anonymous class used as a non-denotable type in action, to create a small wrapper on a mutable content. All this is used to solve a funny problem: the Countdown game, where you need to reach a target with a set of randomly drawn numbers, and the four arithmetic operations. Let us check if all this can take us to an efficient, and readable solution.
You can watch all the episodes on the JEP Café series here: https://www.youtube.com/playlist?list=PLX8CzqL3ArzV4BpOzLanxd4bZr46x5e87
You can find the code of this episode here: https://gist.github.com/JosePaumard/ee935c53f942a25c3cd38027ce4e31c3
⎯⎯⎯⎯⎯⎯ Chapters ⎯⎯⎯⎯⎯⎯
0:00 Do you feel like a coffee break?
0:11 Introducing the Countdown game and its rules
03:01 Introducing the Haskell algorithm translated to Java
04:44 Computing all the permutations of all the subsets
07:20 Splitting the sets to apply the four operations
08:53 Filtering the invalid operations
09:55 Computing the final result and the string of characters
10:21 Using Data Oriented Programming to model the data
12:05 Wrapping up this algorithm
14:49 Writing a bottom up algorithm leveraging Data Oriented Programming
17:32 Using Optional to discard invalid mapping operations
19:30 Getting rid of Optional with Stream.mapMulti()
21:37 Implementing resolve() with an exhaustive switch and pattern matching
23:00 Using a Gatherer to inserting an element at a specific place in a sorted stream
27:18 Using anonymous classes and non-denotable types
28:23 Using the Gatherer in the final process
29:00 Making the algorithm efficient
30:59 Writing the final algorithm using Records
31:50 Comparing the two algorithms
32:33 That's it for today, talk to you soon!
⎯⎯⎯⎯⎯⎯ Resources ⎯⎯⎯⎯⎯⎯
Some amazing Countdown episodes!
◦ 25 50 75 100 3 6 ➱ 952
https://youtu.be/pfa3MHLLSWI
◦ 25 50 75 100 1 10 ➱ 813
https://youtu.be/_JQYYz92-Uk
◦ 100 9 5 10 4 2 ➱ 566
https://youtu.be/Lbhxv5QPErM
◦ 50 75 100 25 2 1 ➱ 940
https://youtu.be/0q6PT4ad6BY
◦ 75 25 50 100 8 2 ➱ 431
https://youtu.be/mRLW_iZVmHU
◦ 25 100 75 50 6 4 ➱ 821
https://youtu.be/sKdM82SELsU
◦ 3 1 7 8 1 4 ➱ 246
https://youtu.be/hJf3BYnz5NQ
More resources on Data Oriented Programming and other subjects covered in this JEP Café
◦ Solving the Countdown problem with Java 21's Language Features by Sundar Athijegannathan ➱ https://inside.java/2023/11/03/countdown-haskell-java/
◦ Prof. Graham Hutton ➱ https://www.cs.nott.ac.uk/~pszgmh/
◦ Haskell ➱ https://haskell.org/
◦ Haskell Demo by Prof. Graham Hutton ➱
https://youtu.be/Qmi6T2AKA3k
◦ The JEP 461 about Stream Gatherers ➱ https://openjdk.org/jeps/461
◦ Nicolai on Gatherers ➱
https://youtu.be/epgJm2dZTSg and
https://youtu.be/pNQ5OXMXDbY
◦ Dev.java ➱ https://dev.java
◦ Inside.java ➱ https://inside.java
◦ JDK 22 ➱ https://openjdk.org/projects/jdk/22
◦ JDK 21 ➱ https://openjdk.org/projects/jdk/21
◦ JDK 17 ➱ https://openjdk.org/projects/jdk/17
◦ OpenJDK ➱ https://openjdk.org
◦ Oracle Java ➱ https://www.oracle.com/java/
Tags: #Java #Java21 #OpenJDK #JDK #JDK21 #JEPCafe #insidejava