-
Notifications
You must be signed in to change notification settings - Fork 141
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
Straighten folds and scans. #364
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b391f3a
Add strict right folds.
kindaro cf9fae1
Add property checks.
kindaro f0af764
Add benchmarks.
kindaro 4fcfc01
Inline strictness checks.
kindaro 818bcdc
Straighten scans.
kindaro 799d3d6
Fix whitespace.
kindaro 3823057
Use `===` for equality.
kindaro 0c10e05
Use infix operator for brevity.
kindaro db41966
Add bench marks for lazy scans.
kindaro d8ffe46
Use standard recursion schemes.
kindaro 8ac8ad8
Dodge import conflicts on older GHC versions.
kindaro d0d708d
Final considerations according to the last review.
kindaro 5fa8aed
Final considerations according to one more last review.
kindaro af6c605
Add bench mark for lazy accumulating maps.
kindaro 93df278
Throw away `mapAccum[LR]Chunks`.
kindaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I was wondering why
scanr
is less lazy thanscanl
. The thing is that its output starts from an accumulator, andData.ByteString.mapAccumR
is too strict in this respect.bytestring/Data/ByteString.hs
Lines 734 to 743 in 05a09c3
I think this is fine: there are no particular expectations about strictness of
scanr
(there is noscanr'
in Prelude).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.
Not sure I follow. Is there a specific proposition you are reasoning towards? Or a question I may answer? For example:
Proposition
Data.ByteString.Lazy.scanr
cannot be lazy.Proof
As you noted, the output of a
scanr
starts from the end, so this is the sort of laziness we can have:So, first the spine of the input list is evaluated to the end, then elements are evaluated from the end backwards. (Whether the accumulator is evaluated before or after the first element depends on the order of evaluation of
+
.) Similarly, the byte stream's spine would have to be evaluated first. But the spine of the byte stream is strict in the leaf:bytestring/Data/ByteString/Lazy/Internal.hs
Lines 74 to 85 in 05a09c3
The leaf itself is a byte array and therefore also strict throughout. So, once we force the spine, every byte is also forced. There is no lazy
scanr
for byte streams. ∎Something like this?
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.
This was just a remark for myself and @sjakobi and anyone else who is puzzled why we have laziness properties for
scanl
, but not forscanr
.It's not like you cannot make
Data.ByteString.Lazy.scanr
a bit lazier. E. g., for the proposed implementationHowever, if we are ready to sacrifice performance, one can define
for which
You can define an even lazier (and slower) version, capable to return first few chunks of bytestring, as long as
f
is very lazy (e. g.,f = const
).My point is that this is a rare use case, which does not justify performance sacrifices, especially given that there is no general expectation how lazy
scanr
should be. I'm fine with your implementation, no action required.