Futures - Difference between `andThen` and `map`? (2.10)

5,592 views
Skip to first unread message

Srirangan S.

unread,
Jan 10, 2013, 1:33:42 AM1/10/13
to scala...@googlegroups.com
Not sure I understand the difference between these two. They look exactly the same to me.

IRC conversation:

<Sri19> (scala futures) ... aren't `andThen` and `map` very similar ? 
<Sri19> how are they different?
<dibblego> yes they are the same
<dibblego> in general anyway, I haven't looked at scala futures
<dibblego> ugh I just saw scala.actor.Future#map and it is broken
<gseitz> dibblego: look at scala.concurrent.Future
<dibblego> k
<dibblego> it's nice right? today has been a bad day, I cannot take anymore
<Sri19> well .. the signatures for andThen and map sure look different .. i must start paying more attention
<dibblego> fuck
<dibblego> yes so map is broken and andThen is something else that is extremely broken
<dibblego> when I replied yes, I was thinking of scala.Function1, for which andThen is a synonym for what is typically called map
* Sri19 is not sure if dibblego is messing with him or being serious :-s
<dibblego> no I am serious
<dibblego> I cannot do this anymore, sorry
<Sri19> in this essay, maps and andThen are being used in a similar way
<Sri19> http://docs.scala-lang.org/overviews/core/futures.html
<Sri19> andThen: Applies the side-effecting function to the result of this future, and returns a new future with the result of this future.
<Sri19> map: Creates a new future by applying a function to the successful result of this future.
<Sri19> me: wtf :-(
<Sri19> still looks the same to me ...
<dibblego> all I can tell you is: L3 man
<Sri19> ?
<stew> heh
* dibblego tokes on a joint
<dibblego> http://www.scala-lang.org/node/8610
<doctau> I think we need to add L4: understands scala library documentation
<doctau> maybe one day I'll be good enough to figure out what the docs mean
<dibblego> did you know that some people have a L6 vertebra?

Any ideas?

Thanks!

- Sri

Srirangan S.

unread,
Jan 10, 2013, 1:35:56 AM1/10/13
to scala...@googlegroups.com
From the definition, perhaps the only distinction is that `map` only deals with successes while `andThen` just carries along regardless.

Luke Vilnis

unread,
Jan 10, 2013, 1:46:29 AM1/10/13
to Srirangan S., scala...@googlegroups.com
I think andThen is used only for side effects. It's a way to insert a side effect into the chain of callbacks in the future(s), it doesn't do anything to produce a new value inside the future (like a "foreach" that also returns the value that was passed into the body).

Roland Kuhn

unread,
Jan 10, 2013, 5:30:14 AM1/10/13
to Luke Vilnis, Srirangan S., scala...@googlegroups.com
I’ll just let the implementation speak for itself:

  def andThen[U](pf: PartialFunction[Try[T], U])(implicit executor: ExecutionContext): Future[T] = {
    val p = Promise[T]()

    onComplete {
      case r => try if (pf isDefinedAt r) pf(r) finally p complete r
    }(executor)

    p.future
  }

Futures are not functions, so andThen in the function traits is not related at all; in Future the word is meant in a temporal sense, which makes sense given the domain. The reason why this method exists is that onComplete does not give any guarantees about the execution order of registered callbacks, so andThen is the preferred way to attach side-effects which need to respect an ordering. Example:

val f: Future[_] = …
f.andThen(println("first")).andThen(println("second")) // will print them in order

f.andThen(println("first"))
f.andThen(println("second")) // will print in any order

Does that help?

Regards,

Roland


Dr. Roland Kuhn
Akka Tech Lead
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn

Srirangan

unread,
Jan 12, 2013, 9:53:21 AM1/12/13
to Roland Kuhn, Luke Vilnis, scala...@googlegroups.com
Thanks Ronald, makes sense.
Reply all
Reply to author
Forward
0 new messages