Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

Where does the "flatmap that s***" idiomatic expression in Scala come from?

What is so powerful about flatmap that it deserves such a place in the Scala folklore?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The story I heard was that two preeminent Scala programmers were pairing when one of them started writing some code like this:

option match {
    case Some ...

At which point the other said "What is this? Amateur hour? Flat map that shit!"

As to what's so powerful about flatMap, well... First, it's the fundamental monadic operator. That means it is a common operation shared by, for example, containers (such as Option, collections, etc), continuations, state, etc. Second, while you can de-construct an Option, that, as opposed to flatMap, is not a monadic operation, so it cannot be as widely applied. Also, it requires too much knowledge about the data you are manipulating.

Note: previously I said matching was slower than flatMap -- the opposite is true as a matter of fact, up to the most recent version of Scala at the time of this writing, 2.10.1.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...