Skip to content

Instantly share code, notes, and snippets.

@teigen
Created January 8, 2010 08:01
Show Gist options
  • Save teigen/271913 to your computer and use it in GitHub Desktop.
Save teigen/271913 to your computer and use it in GitHub Desktop.
//playing around with ideas from http://nilanjan-braincasting.blogspot.com/2010/01/ruby-inject-in-scala.html
import collection.TraversableLike
implicit def traversableLike2HasInject[A, Repr](t:TraversableLike[A, Repr]) = new {
def inject[B >: A](op:(B, A) => B):B = t.reduceLeft(op)
}
object &:+ extends ((Int, Int) => Int){
override def apply(a:Int, b:Int) = a + b
}
implicit def int2RangePimpedInt(i:Int) = new {
def `..` (num:Int) = i to num
def `...` (num:Int) = i until num
}
(1`..`3).inject(&:+) //6
(1`...`3).inject(&:+) //3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment