Permalink
Browse files

Fixes #5. Add implicit class to generate range operator for LArray

  • Loading branch information...
xerial committed Mar 26, 2013
1 parent 0eadbb5 commit cee5ce10680b52c049ebfde14c7e7ed6de454aa7
@@ -198,7 +198,7 @@ trait LIterable[A] { self : LSeq[A] =>
def withFilter(p: A=>Boolean) : LIterator[A] = iterator.filter(p)
def map[B](f:A=>B): LIterator[B] = iterator.map(f)
- def flatMap[B](f: A => LIterable[B]) : LIterator[B] = iterator.flatMap(f)
+ def flatMap[B](f: A => LIterator[B]) : LIterator[B] = iterator.flatMap(f)
def reverse[A]: Repr = {
val b = newBuilder
@@ -154,10 +154,10 @@ trait LIterator[+A] {
def hasNext: Boolean = self.hasNext
}
- def flatMap[B](f: A => LIterable[B]) : LIterator[B] = new AbstractLIterator[B] {
+ def flatMap[B](f: A => LIterator[B]) : LIterator[B] = new AbstractLIterator[B] {
private var current : LIterator[B] = empty
def hasNext: Boolean =
- current.hasNext || self.hasNext && { current = f(self.next()).toIterator; hasNext }
+ current.hasNext || self.hasNext && { current = f(self.next()); hasNext }
def next(): B = (if(hasNext) current else empty).next()
}
@@ -47,6 +47,49 @@ package object larray {
}
}
+ implicit class AsLRange(from:Long) {
+ def Until(to:Long) : LIterator[Long] = new AbstractLIterator[Long] {
+ private var cursor = from
+ def hasNext = cursor < to
+ def next() = {
+ val v = cursor
+ cursor += 1
+ v
+ }
+ }
+
+ def Until(to:Long, step:Long) : LIterator[Long] = new AbstractLIterator[Long] {
+ private var cursor = from
+ def hasNext = cursor < to
+ def next() = {
+ val v = cursor
+ cursor += step
+ v
+ }
+ }
+
+
+ def To(to:Long) : LIterator[Long] = new AbstractLIterator[Long] {
+ private var cursor = from
+ def hasNext = cursor <= to
+ def next() = {
+ val v = cursor
+ cursor += 1
+ v
+ }
+ }
+
+ def To(to:Long, step:Long) : LIterator[Long] = new AbstractLIterator[Long] {
+ private var cursor = from
+ def hasNext = cursor <= to
+ def next() = {
+ val v = cursor
+ cursor += step
+ v
+ }
+ }
+
+ }
@@ -24,22 +24,20 @@ class LArrayFunctionTest extends LArraySpec {
}
}
-
- implicit class LIterableMatcher[A:ClassTag](left:LIterator[A]) {
+ abstract class LMatcher[A](left:LSeq[A]) {
def ===[A:ClassTag](answer:Seq[A]) {
val l = left.mkString(", ")
val a = answer.mkString(", ")
+ trace(s"target:$l, answer:$a")
l should be (a)
}
}
- implicit class LArrayMatcher[A:ClassTag](left:LSeq[A]) {
- def ===[A:ClassTag](answer:Seq[A]) {
- val l = left.mkString(", ")
- val a = answer.mkString(", ")
- l should be (a)
- }
- }
+
+ implicit class LIterableMatcher[A:ClassTag](leftIt:LIterator[A]) extends LMatcher[A](leftIt.toLArray[A])
+ implicit class LArrayMatcher[A:ClassTag](left:LSeq[A]) extends LMatcher[A](left)
+
+
"LArray" should {
@@ -51,6 +49,10 @@ class LArrayFunctionTest extends LArraySpec {
val l = a.toLArray
}
+ trait Input2 {
+
+ }
+
"have iterator" in new Input1 {
l.iterator.mkString(", ") should be (a.mkString(", "))
}
@@ -59,12 +61,16 @@ class LArrayFunctionTest extends LArraySpec {
l.map(_*2) === a.map(_*2)
}
+ "flatMap nested elements" in new Input1 {
+ l.flatMap(x => (0 Until x).map(x=>x)) === a.flatMap(x => (0 until x).map(x => x))
+ }
+
"filter elements" in new Input1 {
l.filter(_ % 2 == 1) === a.filter(_ % 2 == 1)
}
- "have slice" in new Input1 {
+ "slice elements" in new Input1 {
l.slice(1, 3) === a.slice(1, 3)
l.slice(2) === a.slice(2, a.length)
}

0 comments on commit cee5ce1

Please sign in to comment.