-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
remove iteratorFoldM fixes #1716 #1740
Changes from 1 commit
33352a2
ba99572
9705b47
e2f94dc
e955595
a8b54a9
e2f53ae
312ee35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,9 +118,6 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances { | |
|
||
override def collect[A, B](fa: Stream[A])(f: PartialFunction[A, B]): Stream[B] = fa.collect(f) | ||
|
||
override def foldM[G[_], A, B](fa: Stream[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] = | ||
Foldable.iteratorFoldM(fa.toIterator, z)(f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could copy the def step(in: (Stream[A], B])): G[Either[(Stream[A], B), A]] = in match {
case (Stream.empty, b) => G.pure(Right(b))
case (a #:: tail, b) => G.map(f(a, b)) { bnext => Left((tail, bnext)) }
}
G.tailRecM((fa, z))(step) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Patten match Stream into |
||
|
||
override def fold[A](fa: Stream[A])(implicit A: Monoid[A]): A = A.combineAll(fa) | ||
|
||
override def toList[A](fa: Stream[A]): List[A] = fa.toList | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,9 +90,6 @@ trait VectorInstances extends cats.kernel.instances.VectorInstances { | |
|
||
override def collect[A, B](fa: Vector[A])(f: PartialFunction[A, B]): Vector[B] = fa.collect(f) | ||
|
||
override def foldM[G[_], A, B](fa: Vector[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] = | ||
Foldable.iteratorFoldM(fa.toIterator, z)(f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
override def fold[A](fa: Vector[A])(implicit A: Monoid[A]): A = A.combineAll(fa) | ||
|
||
override def toList[A](fa: Vector[A]): List[A] = fa.toList | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,12 +197,7 @@ class FoldableTestsAdditional extends CatsSuite { | |
} | ||
assert(result.value) | ||
|
||
// test trampolining | ||
val large = Stream((1 to 10000): _*) | ||
assert(contains(large, 10000).value) | ||
|
||
// test laziness of foldM | ||
dangerous.foldM(0)((acc, a) => if (a < 2) Some(acc + a) else None) should === (None) | ||
} | ||
|
||
test(".foldLeftM short-circuiting") { | ||
|
@@ -214,17 +209,23 @@ class FoldableTestsAdditional extends CatsSuite { | |
} | ||
|
||
test(".foldLeftM short-circuiting optimality") { | ||
// test that no more elements are evaluated than absolutely necessary | ||
// test that no more than 1 elements are evaluated than absolutely necessary | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we look if we can solve this additional evaluation with @TomasMikula's suggestion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
def concatUntil(ss: Stream[String], stop: String): Either[String, String] = | ||
Foldable[Stream].foldLeftM[Either[String, ?], String, String](ss, "") { (acc, s) => | ||
if (s == stop) Left(acc) else Right(acc + s) | ||
} | ||
|
||
def boom: Stream[String] = sys.error("boom") | ||
assert(concatUntil("STOP" #:: boom, "STOP") == Left("")) | ||
assert(concatUntil("Zero" #:: "STOP" #:: boom, "STOP") == Left("Zero")) | ||
assert(concatUntil("Zero" #:: "One" #:: "STOP" #:: boom, "STOP") == Left("ZeroOne")) | ||
assert(concatUntil("STOP" #:: "AFTER" #:: boom, "STOP") == Left("")) | ||
assert(concatUntil("Zero" #:: "STOP" #:: "AFTER" #:: boom, "STOP") == Left("Zero")) | ||
assert(concatUntil("Zero" #:: "One" #:: "STOP" #:: "AFTER" #:: boom, "STOP") == Left("ZeroOne")) | ||
} | ||
|
||
test("Foldable[List] doesn't break substitution") { | ||
val result = List.range(0,10).foldM(List.empty[Int])((accum, elt) => Eval.always(elt :: accum)) | ||
|
||
assert(result.value == result.value) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we maybe do this one?
right? If we had this, we could cover the common case for
List
andNonEmptyList
, no?