├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── balarawool
│ └── cryptarithmetic
│ ├── Expression.java
│ ├── Generator.java
│ ├── Puzzle.java
│ ├── PuzzleConstraint.java
│ └── Variable.java
└── test
└── java
└── com
└── balarawool
└── cryptarithmetic
└── PuzzleTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 | !**/src/main/**/target/
4 | !**/src/test/**/target/
5 |
6 | ### IntelliJ IDEA ###
7 | .idea/
8 | .idea/modules.xml
9 | .idea/jarRepositories.xml
10 | .idea/compiler.xml
11 | .idea/libraries/
12 | *.iws
13 | *.iml
14 | *.ipr
15 |
16 | ### Eclipse ###
17 | .apt_generated
18 | .classpath
19 | .factorypath
20 | .project
21 | .settings
22 | .springBeans
23 | .sts4-cache
24 |
25 | ### NetBeans ###
26 | /nbproject/private/
27 | /nbbuild/
28 | /dist/
29 | /nbdist/
30 | /.nb-gradle/
31 | build/
32 | !**/src/main/**/build/
33 | !**/src/test/**/build/
34 |
35 | ### VS Code ###
36 | .vscode/
37 |
38 | ### Mac OS ###
39 | .DS_Store
40 | **/.DS_Store
41 |
42 | /target/
43 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Example for Data Oriented Programming in Java
2 |
3 | This repo contain source code for my talk "Algebraic Data Types + Pattern Matching = Elegant and readable Java code"
4 |
5 | This program solves a [cryptarithmetic puzzle](https://en.wikipedia.org/wiki/Verbal_arithmetic): SEND + MORE = MONEY
6 |
7 | The talk shows how you can write readable, understandable code with Data Oriented Programming in Java using records, sealed types and pattern matching features.
8 | If you have questions, suggestions to improve or any other feedback, please reach me out at [@BalaRawool](https://twitter.com/BalaRawool)
9 |
10 | ## Links to articles on Data Oriented Programming
11 |
12 | [InfoQ Article from Brian Goetz](https://www.infoq.com/articles/data-oriented-programming-java/)
13 |
14 | [Inside Java Article from Nicolai Parlog](https://inside.java/2024/05/23/dop-v1-1-introduction/)
15 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.example
8 | cryptarithmetic
9 | 1.0-SNAPSHOT
10 |
11 |
12 | 23
13 | 23
14 | UTF-8
15 |
16 |
17 |
18 |
19 | org.apache.maven.plugins
20 | maven-compiler-plugin
21 |
22 | 23
23 | 23
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | junit
34 | junit
35 | 4.13.2
36 | test
37 |
38 |
39 | org.junit.jupiter
40 | junit-jupiter-api
41 | 5.8.2
42 | test
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/src/main/java/com/balarawool/cryptarithmetic/Expression.java:
--------------------------------------------------------------------------------
1 | package com.balarawool.cryptarithmetic;
2 |
3 | import java.util.Map;
4 |
5 | sealed interface Expression {
6 |
7 | record ConstExpr(int value) implements Expression { }
8 | record VarExpr(String name) implements Expression { }
9 | record AdditionExpr(Expression left, Expression right) implements Expression { }
10 | record MultiplicationExpr(Expression left, Expression right) implements Expression { }
11 |
12 | static int evaluate(Expression expr, Map values) {
13 | return switch (expr) {
14 | case ConstExpr(var value) -> value;
15 | case VarExpr(var name) -> values.get(name);
16 | case AdditionExpr(var left, var right) -> evaluate(left, values) + evaluate(right, values);
17 | case MultiplicationExpr(var left, var right) -> evaluate(left, values) * evaluate(right, values);
18 | };
19 | }
20 |
21 | static Expression constant(int value) {
22 | return new ConstExpr(value);
23 | }
24 | static Expression varExpr(String name) {
25 | return new VarExpr(name);
26 | }
27 | static Expression add(Expression left, Expression right) {
28 | return new AdditionExpr(left, right);
29 | }
30 | static Expression multiply(Expression left, Expression right) {
31 | return new MultiplicationExpr(left, right);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/balarawool/cryptarithmetic/Generator.java:
--------------------------------------------------------------------------------
1 | package com.balarawool.cryptarithmetic;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.HashSet;
6 | import java.util.List;
7 | import java.util.Map;
8 | import java.util.Set;
9 | import java.util.stream.Collectors;
10 | import java.util.stream.IntStream;
11 |
12 | record Generator(List variables, boolean uniqueValues) {
13 |
14 | List