Skip to content

Instantly share code, notes, and snippets.

@raindev
Last active August 29, 2015 14:19
Show Gist options
  • Save raindev/603dee4c27f5b0079151 to your computer and use it in GitHub Desktop.
Save raindev/603dee4c27f5b0079151 to your computer and use it in GitHub Desktop.
Java let form
// implementation
<T> void let(T value, Consumer<T> consumer) {
consumer.accept( value );
}
// example usage
let(userService.findUser(), user -> {
// do something to the user
if (!user.isValid()) {
userService.removeUser(user);
}
});
@raindev
Copy link
Author

raindev commented Apr 23, 2015

Here is what could be achieved with lambda:

((Function<User, Void>) (user) -> {
    // do something to the user
    if (!user.isValid()) {
        userService.removeUser(user);
    }
    return null;
}).apply(userService.findUser());

Agh 😯 Ugly!

Sadly, something like that won't work:

((user) -> {
    // do something to the user
    if (!user.isValid()) {
        userService.removeUser(user);
    }
})(userService.findUser());

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