Last active
October 16, 2021 09:07
-
-
Save folarinmartins/39111c001e434a1b280ed9b0d511f058 to your computer and use it in GitHub Desktop.
Snippets: Curry-style Lambda Initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
String name; | |
int age; | |
private Person(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
static PersonWaitingForName create() { | |
return name -> age -> new Person(name, age); | |
} | |
static interface PersonWaitingForName { | |
PersonWaitingForAge name(String name); | |
} | |
static interface PersonWaitingForAge { | |
Person age(int age); | |
} | |
public static void main(String[] args) { | |
Person charlotte = Person.create() | |
.name("Charlotte") | |
.age(25); | |
} | |
} | |
/** | |
Reference | |
1: https://stackoverflow.com/a/31591305 | |
2: https://en.wikipedia.org/wiki/Currying | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment