flatMap() equivalent for Optional ?

1,850 views
Skip to first unread message

Ronan Michaux

unread,
Nov 19, 2013, 2:07:12 PM11/19/13
to guava-...@googlegroups.com
Hi,

I can't understand why the monadic type Optional hasn't a flatMap(Function<T, Optional<U>> f) as all Optional/Maybe equivalent ?

With the current version, the usage is very limited.

best regards,
Ronan.

Louis Wasserman

unread,
Nov 19, 2013, 2:23:16 PM11/19/13
to Ronan Michaux, guava-...@googlegroups.com
This has been discussed in several centithreads, see e.g. https://groups.google.com/d/topic/guava-discuss/JVgYWq6VU4c/discussion .

Essentially, I'd say that we approve of adding a flatMap method in the presence of lambdas in the language.   Java 8's Optional was quite heavily based on ours, and we supported adding flatMap there.

But in the absence of lambdas, we generally consider the anonymous classes required to write the Function type here as more awkward than the other alternatives:

optional.isPresent() ? function(optional.get()) : Optional.absent();

See also the Caveats section of the Functional Idioms page of the wiki.


--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/7a7e1ef3-02c7-4013-8f81-4eaeeb80f156%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Louis Wasserman

Colin Decker

unread,
Nov 19, 2013, 2:28:11 PM11/19/13
to Louis Wasserman, Ronan Michaux, guava-...@googlegroups.com
There is currently an open issue for this (though the issue uses a different name): https://code.google.com/p/guava-libraries/issues/detail?id=1450



For more options, visit https://groups.google.com/groups/opt_out.



--
Colin Decker | Software Engineer | cgde...@google.com | Java Core Libraries Team

Michał Minicki

unread,
Nov 19, 2013, 7:18:30 PM11/19/13
to guava-...@googlegroups.com
I don't get it. This explanation could as well apply to the transform [1] method:

    optional.isPresent() ? transformFunction(optional.get()) : Optional.absent();

What's the difference?


Mike

Ronan Michaux

unread,
Nov 20, 2013, 3:05:59 AM11/20/13
to guava-...@googlegroups.com, Ronan Michaux
I already have functions :
  • Function<A, Optional<B>> getB = ...
  • Function<B, Optional<C>> getC = ...

But I can't chain my transformations :

Optional<C> cOpt = FluentIterable.from(listOfA).first().flatTransform(getB).flatTransform(getC);


I have to "flatten" myself each step separately with a custom flatOpt()  :


public static <T> Optional<T> flatOpt(final Optional<Optional<T>> input) {
    return (input.isPresent()) ? input.get() : Optional.<T> absent();
}

Optional<B> bOpt = flatOpt(FluentIterable.from(listOfA).first().transform(getB))

Optional<C> cOpt = flatOpt(bOpt.transform(getC));


it's not very clean, not in the "spirit" of Optional.


Regards,

Ronan.



Louis Wasserman

unread,
Nov 20, 2013, 1:09:22 PM11/20/13
to Ronan Michaux, guava-discuss

I'm not sure why you wrote the functions that way, then, instead of as a normal Java method taking an Optional argument.

--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.

Ronan Michaux

unread,
Nov 20, 2013, 2:33:20 PM11/20/13
to guava-...@googlegroups.com, Ronan Michaux
I use Funcito, so for many functions I have got easily the java function and the corresponding Guava Function.

In other language I use the Optional monad to "linearize" several nested (null safe) if (...).


Optional<C> cOpt = FluentIterable.from(listOfA).first().flatTransform(getB).flatTransform(getC);


Is equivalent of :

if (!listOfA.isEmpty()) {
  A first = listOfA.get(0);

  Optional<B> bOpt = first.getB()

  if (bOpt.isPresent()) {
    B b = bOpt.get();

    Optional<C> cOpt = b.getC();

    if (cOpt.isPresent()) {
      C c = cOpt.get();

      // Use of c.........
    }
}

Regards,
Ronan.

Jed Wesley-Smith

unread,
Nov 23, 2013, 11:53:32 PM11/23/13
to Ronan Michaux, guava-discuss
Note that the fugue library provides a richer Option class, as well as Either, that support the various useful combinators such as flatMap, filter and fold.




these are designed to work with the Guava Function/Predicate interfaces.

cheers,
jed.

* some background on why Java8 has flatMap:


--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages