Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added scanLeft #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions geny/src/geny/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ trait Generator[+A]{
result
}

def scanLeft[B](start: B)(f: (B,A) => B): Generator[B] = new Generator.Scanned(this,start,f)

def reduce[B >: A](f: (B, A) => B): B = reduceLeft(f)
def reduceLeft[B >: A](f: (B, A) => B): B = {
Expand Down Expand Up @@ -297,6 +298,16 @@ object Generator{
override def toString = s"$inner.map($func)"
}

private class Scanned[+T, V](inner: Generator[V], initial: T, func: (T,V) => T) extends Generator[T]{
def generate(f: T => Generator.Action) = {
var acc = initial
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var acc = initial
var acc = initial
f(acc)

inner.generate{ v =>
acc = func(acc,v)
f(acc)
}
}
}

private class Sliced[+T](inner: Generator[T], start: Int, end: Int) extends Generator[T]{
def generate(f: T => Generator.Action) = {
var count = 0
Expand Down
4 changes: 4 additions & 0 deletions geny/test/src/geny/TestGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ object TestGenerator extends TestSuite{
assert(Generator.from[IndexedSeq, Int](0 until 10).foldLeft(0)(_ + _) == 45)
assert(Generator.from[IndexedSeq, Int](0 until 0).foldLeft(0)(_ + _) == 0)
}
test("scanLeft"){
assert(Generator(1,2,3,4).scanLeft(100)(_ + _).toList == List(101,103,106,110))
assert(Generator[Int]().scanLeft(100)(_ + _).toList == List())
Comment on lines +66 to +67
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(Generator(1,2,3,4).scanLeft(100)(_ + _).toList == List(101,103,106,110))
assert(Generator[Int]().scanLeft(100)(_ + _).toList == List())
assert(Generator(1,2,3,4).scanLeft(100)(_ + _).toList == List(100,101,103,106,110))
assert(Generator[Int]().scanLeft(100)(_ + _).toList == List(100))

}


test("concat"){
Expand Down