-
-
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
List's traverse method was applying effects in reverse. #142
Conversation
This commit fixes this issue (which had to do with passing arguments to G.map2 in the wrong order) and adds a regression test to be sure we don't accidentally mess this up in the future.
👍 |
// test sequence order using a contrived, unsafe state monad. | ||
// | ||
|
||
test("#140: confirm sequence order") { |
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.
could we make the same test on List[Option[Int]]
?
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.
yeah it should be possible.
I think this implementation of traverse is problematic when side effects are in play and should be executed in order. This is a problem even if the side effects are properly housed within something like Example with scalaz's Task: scala> traverse(List(1, 2, 3))(i => Task{println(i); i})
res2: scalaz.concurrent.Task[List[Int]] = scalaz.concurrent.Task@3b4c8039
scala> res2.run
3
2
1
res3: List[Int] = List(1, 2, 3) I don't think you can rely on running side effects in a |
I don't think this is about side-effects, but just about the order in which the monad instances (which constrain effects) are composed, right? EDIT: If you add a |
scalaz/scalaz#886 discussion in scalaz |
The reason Scalaz hits this problem is because they need to reverse the list to do a That said, I agree that side-effecting during the fold is a bad idea -- the reason to avoid reversing is to minimize garbage IMO. |
@wedens I would consider my example different than the one you have linked because I'm not using traverse directly for side effects. I'm traversing into @non sorry I'm not sure I completely understand your response. Are you agreeing that this is an issue, or does it not seem like a problem to you? I have no qualms about local side effects for performance reasons. I just am not sure that you can do side effects (without using |
This helps make it a bit more obvious that monadic effect order and side-effect order are the same.
List's traverse method was applying effects in reverse.
This commit fixes this issue (which had to do with passing
arguments to G.map2 in the wrong order) and adds a regression
test to be sure we don't accidentally mess this up in the
future.
Fixes #140
Review by @refried et al.