For-comprehension with flatMap as innermost operator

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.

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.


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


Like all the other proposals related to for comprehensions (Making `for` simpler and more regular,, this won’t happen until someone writes an implementable SIP, and manages to get some consensus on it.

1 Like

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.

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