explode

Brian Goetz brian.goetz at oracle.com
Thu Feb 7 10:54:36 PST 2013


flatMap is indeed map+flatten, but unfortunately we cannot factor it 
into two steps because of erasure.  (We can't make a method on 
Stream<Collection<T>> called flatten() that produces a Stream<T>.)

The name flatMap is used in Scala, and while I'm not suggesting that 
this constitutes any sort of proof of suitability, at least has some 
track record.

DIGRESSION

More generally, flatMap is commonly used to name the bind operator of a 
monad, where you have a type Foo<T>, and flatMap has the signature:

<U> Foo<U> flatMap(T -> Foo<U>)

IF we are to use the name flatMap, I feel it is important to at least 
have one overload that follows this naming pattern.



On 2/7/2013 1:34 PM, Tim Peierls wrote:
> On Thu, Feb 7, 2013 at 1:16 PM, Brian Goetz <brian.goetz at oracle.com
> <mailto:brian.goetz at oracle.com>> wrote:
>
>     I think the proposed solution there is:
>       - example in Javadoc, and/or static helper...
>
>
> OK, then.
>
> I was still having trouble following the semantics while names were
> being discussed, so I'm very late to the naming party. "flatMap" doesn't
> convey much to me, but I guess I could learn to use it. Here's my beef:
> A roughly analogous name in Guava is the unlovely but crystal clear
> "transformAndConcat". That's not accurate enough here, since the action
> is more general than concatenation, but something like "mapAndCollect"
> conveys the process in the right order -- first map, then collect the
> results -- which is something that "flatMap" gets backwards: "a
> flattening of mapped elements".
>
> --tim


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