Last active
October 11, 2016 06:15
-
-
Save hrchu/9286c8b4c4541028955421ef16db2515 to your computer and use it in GitHub Desktop.
Wrap lambda which throws checked exception
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
private <T, R> Function<T, R> wrapFunction(ThrowableFunction<T, R, Throwable> function) { | |
try { | |
return function.apply(arg); | |
} catch (Throwable e) { | |
throw new RuntimeException(e); | |
} | |
}; | |
} | |
@FunctionalInterface | |
private interface ThrowableFunction<T, R, E extends Throwable> { | |
R apply(T t) throws E; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
paths.stream().map(wrapFunction(Files::lines))...