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

Add Apply.map2Eval and allow traverse laziness #1015

Merged
merged 2 commits into from
May 11, 2016

Conversation

ceedubs
Copy link
Contributor

@ceedubs ceedubs commented May 4, 2016

Fixes #513.

I think this should solve the majority of use-cases for lazy traversal.
One thing I noticed is that both reduceRightToOption and traverse1_
(which uses the former) seem to be impossible to implement lazily such
that they allow for short-circuiting. At least I couldn't figure out a
way. I left a note in the traverse1_ scaladoc, but in practice I don't
anticipate this being a problem, because at the cost of just having an
Applicative instance as opposed to Apply, you can use traverse_
which is sufficiently lazy.

Fixes typelevel#513.

I think this should solve the majority of use-cases for lazy traversal.
One thing I noticed is that both `reduceRightToOption` and `traverse1_`
(which uses the former) seem to be impossible to implement lazily such
that they allow for short-circuiting. At least I couldn't figure out a
way. I left a note in the `traverse1_` scaladoc, but in practice I don't
anticipate this being a problem, because at the cost of just having an
`Applicative` instance as opposed to `Apply`, you can use `traverse_`
which is sufficiently lazy.
@ceedubs
Copy link
Contributor Author

ceedubs commented May 4, 2016

Another note: it kind of seems like there should be things like apEval, ap2Eval, etc now, because those are also methods that could benefit from laziness. I opted to omit them for now since map2 probably satisfies most use-cases. We may want to add them in a future PR though.

@ceedubs
Copy link
Contributor Author

ceedubs commented May 10, 2016

@julien-truffaut would you want to review this since you originally opened up #513?

@non
Copy link
Contributor

non commented May 10, 2016

👍

foldLeft(fa, G.pure(())) { (acc, a) =>
G.map2(acc, f(a)) { (_, _) => () }
}
foldRight(fa, Later(G.pure(()))) { (a, acc) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

You might want to use Always here, since it will be evaluated once and there's no need for memoization.

This avoids a bit of memoization overhead in places that we only expect
to be evaluated once.
@codecov-io
Copy link

Current coverage is 82.23%

Merging #1015 into master will increase coverage by +0.20%

  1. 2 files in ...main/scala/cats/data were modified. more
    • Hits +3
  2. 2 files in .../src/main/scala/cats were modified. more
    • Misses +8
    • Hits -6
  3. 2 files (not in diff) in ...in/scala/cats/syntax were modified. more
    • Misses -4
    • Hits +4
  4. 2 files (not in diff) in ...n/scala/cats/functor were modified. more
  5. 2 files (not in diff) in .../src/main/scala/cats were modified. more
    • Misses -10
    • Hits +8
@@             master      #1015   diff @@
==========================================
  Files           215        215          
  Lines          2704       2713     +9   
  Methods        2639       2647     +8   
  Messages          0          0          
  Branches         60         61     +1   
==========================================
+ Hits           2217       2231    +14   
+ Misses          487        482     -5   
  Partials          0          0          

Sunburst

Powered by Codecov. Last updated by 8355a19...1236cf8

@non
Copy link
Contributor

non commented May 10, 2016

👍

1 similar comment
@julien-truffaut
Copy link
Contributor

👍

@julien-truffaut julien-truffaut merged commit 15fc946 into typelevel:master May 11, 2016
@stew stew removed the in progress label May 11, 2016
ceedubs added a commit to ceedubs/cats that referenced this pull request Dec 10, 2017
This adds a test case that the `traverse` implementation for `Vector`
short-circuits the traversal when possible. This would have failed the build
in typelevel#2091.

See typelevel#1015 for when similar tests were added for similar structures.
johnynek pushed a commit that referenced this pull request Dec 10, 2017
This adds a test case that the `traverse` implementation for `Vector`
short-circuits the traversal when possible. This would have failed the build
in #2091.

See #1015 for when similar tests were added for similar structures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants