Skip to content

Commit

Permalink
Fixes #5. Add implicit class to generate range operator for LArray
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Mar 26, 2013
1 parent 0eadbb5 commit cee5ce1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/xerial/larray/LIterable.scala
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/xerial/larray/LIterator.scala
Expand Up @@ -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()
}

Expand Down
43 changes: 43 additions & 0 deletions src/main/scala/xerial/larray/package.scala
Expand Up @@ -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
}
}

}



Expand Down
26 changes: 16 additions & 10 deletions src/test/scala/xerial/larray/LArrayFunctionTest.scala
Expand Up @@ -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 {

Expand All @@ -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(", "))
}
Expand All @@ -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)
}
Expand Down

0 comments on commit cee5ce1

Please sign in to comment.