Skip to content

Instantly share code, notes, and snippets.

@fwrq41251
Last active March 9, 2016 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwrq41251/f6b82e201ff3b63d11ca to your computer and use it in GitHub Desktop.
Save fwrq41251/f6b82e201ff3b63d11ca to your computer and use it in GitHub Desktop.
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