Skip to content

Instantly share code, notes, and snippets.

@folarinmartins
Last active October 16, 2021 09:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save folarinmartins/39111c001e434a1b280ed9b0d511f058 to your computer and use it in GitHub Desktop.
Save folarinmartins/39111c001e434a1b280ed9b0d511f058 to your computer and use it in GitHub Desktop.
Snippets: Curry-style Lambda Initialization
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