Skip to content

Instantly share code, notes, and snippets.

@akiellor
Created October 24, 2011 01:40
Show Gist options
  • Save akiellor/1308190 to your computer and use it in GitHub Desktop.
Save akiellor/1308190 to your computer and use it in GitHub Desktop.
An implementation of Ruby's Tap in Scala.
import collection.mutable.MutableList
import Tap._
class Tap[A](any: A) {
def tap(f: (A) => Unit): A = {
f(any)
any
}
}
object Tap {
implicit def tap[A](toTap: A): Tap[A] = new Tap(toTap)
}
MutableList[String]().tap({m:MutableList[String] =>
m += "Blah"
})
MutableList[String]().tap(_ += "Blah")
MutableList[String]().tap({ l =>
l += "Blah"
l += "Blah"
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment