Created
January 8, 2010 08:01
-
-
Save teigen/271913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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