Skip to content

Instantly share code, notes, and snippets.

@hrchu
Last active October 11, 2016 06:15
Show Gist options
  • Save hrchu/9286c8b4c4541028955421ef16db2515 to your computer and use it in GitHub Desktop.
Save hrchu/9286c8b4c4541028955421ef16db2515 to your computer and use it in GitHub Desktop.
Wrap lambda which throws checked exception
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;
}
@hrchu
Copy link
Author

hrchu commented Oct 11, 2016

Usage: paths.stream().map(wrapFunction(Files::lines))...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment