Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.function.Consumer;
import java.util.function.Function;
public class Currying {
public static void main(String[] args) {
MyFileHelper.using(new MyFile("my-file")).accept(myFile -> myFile.print());
}
static class MyFile {
private final String fileName;
MyFile(String fileName) {
super();
this.fileName = fileName;
}
void close() {
System.out.println("closing file:" + fileName);
}
void print() {
System.out.println("fileName:" + fileName);
}
}
static class MyFileHelper {
static Consumer<Consumer<MyFile>> using(MyFile file) {
Function<MyFile, Consumer<Consumer<MyFile>>> currier = myFile -> consumer -> {
try {
consumer.accept(myFile);
} finally {
myFile.close();
}
};
Consumer<Consumer<MyFile>> curried = currier.apply(file);
return curried;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.