Created
May 12, 2011 14:02
-
-
Save seratch/968555 to your computer and use it in GitHub Desktop.
Scala LINQ-like impl memo
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
// ---------------------------------------- | |
// * Scala LINQ-like impl memo | |
// http://scala-programming-language.1934581.n4.nabble.com/scala-LINQ-for-scala-td2003452.html | |
// https://github.com/vladimirk/scalalinq/blob/master/src/scalalinq/linq.scala | |
// ---------------------------------------- | |
class linq[A](seq: Seq[A]) { | |
def where(p: A => Boolean) = seq filter p | |
def select[B](p: A => B) = seq map p | |
def orderby[B <% Ordered[B]](f: A => B) = seq.toList sort ((a, b) => f(a) < f(b)) | |
def ascending = seq | |
def descending = seq reverse | |
} | |
object linq { | |
implicit def from[A](s: Seq[A]) = new linq(s); | |
def self[A] = (x: A) => x | |
} | |
import scala.xml._ | |
import linq._ | |
val doc = <root><value>1</value><value>6</value><value>3</value><value>2</value><value>5</value></root> | |
val result = from (doc \ "value") where (_.text.toInt % 2 == 1) select (_.text.toInt * 2) orderby self descending | |
// result: Seq[Int] = List(10, 6, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment