Last active
March 9, 2016 09:40
-
-
Save fwrq41251/f6b82e201ff3b63d11ca to your computer and use it in GitHub Desktop.
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
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