For-comprehension with flatMap as innermost operator

#1

Has there been any discussion of implementing an alternative keyword (other than yield) in the for comprehension to cause the expander to substitute flatMap for map in the inner most loop?

Normally, the for-comprehension expands to a flatMap of flatMap of flatMap of … of map. It would be nice for some applications to expand to flatMap of flatMap of flatMap of … of flatMap.

I don’t have a good suggestion for the keyword, but Common Lisp uses the word nconc or nconcing which is quite arcane, albeit idiomatic in CL.

1 Like
#2

Maybe some logic of Better Monadic For could make it into Dotty. In particular, there is a special treatment for cases where the final map can be avoided.

3 Likes
#3

Another upvote for better-monadic-for, it’s now standard in all my projects.

I’d love to see this as the default behaviour in Dotty

2 Likes
#4

Like all the other proposals related to for comprehensions (Making `for` simpler and more regular, https://github.com/lampepfl/dotty/issues/2573), this won’t happen until someone writes an implementable SIP, and manages to get some consensus on it.

1 Like
#5

I’ll add that no one at EPFL/Scala Center/Lightbend has the time to do the spec-inc/consensus-building work around changes to for-comprehensions currently, so this has to come from the community.

#6

In that case I will try and take this on :slight_smile:

11 Likes