-
-
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
Adds foldM #356
Adds foldM #356
Conversation
@@ -26,6 +26,12 @@ class FoldableTests extends CatsSuite { | |||
// more basic checks | |||
val names = List("Aaron", "Betty", "Calvin", "Deirdra") | |||
assert(F.foldMap(names)(_.length) == names.map(_.length).sum) | |||
val sumM = F.foldM(names, "") { (acc, x) => (Some(acc + x): Option[String]) } | |||
assert(sumM == Some("AaronBettyCalvinDeirdra")) | |||
val notCalvin = F.foldM(names, "") { (acc, x) => |
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.
Scalastyle doesn't like some trailing whitespace here.
This looks good to me once the whitespace is fixed and the build is passing. @non could you please be a second set of eyes on the use of |
Added the whitespace fix. It's based on |
Current coverage is
|
This looks good. I want to see if I can make it stack-safe, but in principle I'm 👍 on this. |
I suspect that to make it completely lazy, it's not enough to fold lazily, but also make |
I'm a little hesitant on this one, now that I know it isn't stack-safe. If we think we could make it stack-safe in the future, I'm less concerned. Could we at least leave a scaladoc comment warning about stack safety? |
I think the issue was that I could make it lazy, but laziness only goes to calculating a |
@eed3si9n yes, I suspect that you are right. |
s/Trampoline/Eval/ |
This seems useful but it looks like it got lost in the PRs - very sorry @eed3si9n . If looks like there's some actual failing tests: https://travis-ci.org/typelevel/cats/builds/99910227 Once that's fixed this LGTM |
You seem to first build a function, that you then apply at once to the argument. Unless the target monad
as ki := w => G.flatMap(f(w, ai))(ki-1) Thus, if Now, for the solution, I might be missing something, but wouldn't an eager |
@adelbertc I'm fairly certain this was submitted when FoldableTestsAdditional was flickering (see #769); it's possible that if you close and reopen the PR it will pass. |
Rebased against master to prod Travis. |
Here's how to blow the stack: scala> import cats._, std.all._, syntax.all._
import cats._
import std.all._
import syntax.all._
scala> def nonzero(acc: Int, x: Int): Option[Int] =
| if (x == 0) None else Some(acc + x)
nonzero: (acc: Int, x: Int)Option[Int]
scala> (Foldable[List].foldM((1 to 10000).toList, Eval.later { 0 }) {nonzero}).value
java.lang.StackOverflowError
at $anonfun$2.apply(<console>:21)
at $anonfun$2.apply(<console>:21)
at cats.Foldable$$anonfun$foldM$2$$anonfun$apply$5$$anonfun$apply$6.apply(Foldable.scala:86)
at scala.Option.flatMap(Option.scala:171)
at cats.std.OptionInstances$$anon$1.flatMap(option.scala:20)
at cats.std.OptionInstances$$anon$1.flatMap(option.scala:8)
at cats.Foldable$$anonfun$foldM$2$$anonfun$apply$5$$anonfun$apply$6.apply(Foldable.scala:86)
at scala.Option.flatMap(Option.scala:171)
at cats.std.OptionInstances$$anon$1.flatMap(option.scala:20)
at cats.std.OptionInstances$$anon$1.flatMap(option.scala:8)
at cats.Foldable$$anonfun$foldM$2$$anonfun$apply$5$$anonfun$apply$6.apply(Foldable.scala:86)
at scala.Option.flatMap(Option.scala:171) |
Closing this in favor of #925 |
This implements
foldM
.