Where has the map method on Optional moved? (original) (raw)

Remi Forax forax at univ-mlv.fr
Tue Feb 26 00:15:00 PST 2013


On 02/26/2013 08:19 AM, Remi Forax wrote:

Yes, I vote to remove it because it doesn't map :) well with the java mindset. That said, we already discussed that and other alternatives are less nice to use, at least until we use the static import trick (as with reducers)

just to be crystal clear. interface Optionalizer<T, R> { // good name needed abstract R result(boolean isPresent, T element);

public static Optionalizer<T,T> orDefaultValue(T defaultValue) { return (isPresent, element) -> isPresent? element: defaultValue; }

public static Optionalizer<T, Boolean> isPresent() { return (isPresent, element) -> isPresent; }

public static Optionalizer<T, Void> andIfPresent(Consumer<? super T> consumer) { return (isPresent, element) -> { if (isPresent) { consumer.accept(element); } }; }

public static Optionalizer<T, T> T orNull() { // doesn't use orDefaultValue(null) because the returned lambda is not constant :) // maybe better to do a null check in orDefaultValue return (isPresent, element) -> isPresent? element: null: } }

with: interface Stream { R findFirst(Optionalizer<? super T, ? extends R> optionalizer); }

examples: String s = streamOfString.findFirst(orDefaultValue("")): boolean isPresent = streamOfString.findFirst(isPresent()); streamOfString.findFirst(andIfPresent(System.out::println)); String s2 = streamOfString.findFirst(orNull()):

I think i can like this.

Rémi

Sent from my Phone ----- Reply message ----- From: "Sam Pullara" <sam at sampullara.com> To: <lambda-libs-spec-experts at openjdk.java.net> Subject: Where has the map method on Optional moved? Date: Tue, Feb 26, 2013 06:16 I've never been comfortable with this. I'm glad Jed is calling it out. Can we make Optional first class or remove it? Sam On Mon, Feb 25, 2013 at 9:12 PM, Jed Wesley-Smith <jed at wesleysmith.io> wrote: > Hi Paul, > > You don't get a choice, it is a (or forms a) monad, you just removed > the useful methods (map/flatMap aka fmap/bind). This leaves clients to > implement them (or the functionality) in an ad-hoc and possibly buggy > form themselves. > > It is a monad if there exists some pair of functions: > > A -> Option > Option -> (A -> Option) -> Option > > The first is Optional.of, the second is currently: > > Optional a = … > Optional b = … > Function<A, Optional f = … > if (a.isPresent) { > b = f.apply(a.get()); > } else { > b = Optional.empty(); > } > > rather than: > > Optional a = … > Function<A, Optional f = … > final Optional b = a.flatMap(f); > > cheers, > jed. > > On 26 February 2013 00:12, Paul Sandoz <paul.sandoz at oracle.com> wrote: >> Hi Dhananjay, >> >> It is not missing it was removed. >> >> java.util.Optional has a narrower scope that optional things in other languages. We are not trying to shoe-horn in an option monad. >> >> Paul. >> >> On Feb 23, 2013, at 12:27 AM, Dhananjay Nene <dhananjay.nene at gmail.com> wrote: >> >>> It seemed to be there on the Optional class in b61 but is missing now. Is >>> there some way to run map/flatMap operations on an Optional? >>> >>> Thanks >>> Dhananjay >>> >> >> >



More information about the lambda-libs-spec-observers mailing list