Algebraic Data Types + Pattern Matching = Elegant and readable Java code
summary
Builds a working cryptarithmetic solver for SEND + MORE = MONEY using Java’s Project Amber features, implementing expressions as a sealed Expression hierarchy and evaluation via a pattern-matching switch. Uses records and sealed interfaces for product/sum types, a DFS generator to reduce ~90 million raw combinations to ~181,000 constrained combos, and checks constraints (unique-values, equality) with expression evaluation. The unit test runs and finds a valid assignment; source code is on GitHub. record deconstruction and guarded patterns
AI-written summary · about
chapters (21)
- 0:00Introduction
- 0:45Outline
- 1:19Questions
- 1:38Puzzle
- 2:31Rules
- 4:22Problem Analysis
- 5:20Constraint Programming
- 6:00Solution Design
- 7:16Expression Evaluation
- 9:03Algebraic Data Types
- 9:27Product Types
- 10:12Sum Types
- 11:02Pattern Matching
- 11:27Instance of
- 12:41Switch
- 13:40Primitive Types in Patterns
- 14:19Solving the Problem
- 15:34Implementation
- 17:09Seal Interface
- 23:16Generator
- 23:50Puzzle constraint
related talks
54mData Focused programming with Pattern Matching in Java
27mPattern Matching for Java
31mAlgebraic Data Type in Java 17
55mThe Art of Java Language Pattern Matching
33mImprove your Java Applications with Records and Pattern Matching
01h 02mThe Three Amigos: Intermixing Pattern Matching, Records, and Sealed Classes
01h 01mPattern Matching in Java
35mAlgebraic Data Types
