-
Notifications
You must be signed in to change notification settings - Fork 50
foldr': lazy right folds #118
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
Conversation
src/Data/List/Lazy.purs
Outdated
@@ -705,3 +706,11 @@ foldM f a xs = | |||
Nothing -> pure a | |||
Just { head: b, tail: bs } -> | |||
f a b >>= \a' -> foldM f a' bs | |||
|
|||
-- | Perform a right fold lazily | |||
foldr' :: forall a b. Z.Lazy b => (a -> b -> b) -> b -> List a -> b |
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 this be written with regular foldr
?
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.
Well, something like this will compile:
foldr' op = foldr op'
where op' a b = Z.defer \_ -> a `op` b
But using foldr
kills the laziness. So the test, for example, will just freeze on its infinite input.
Thinking if there is any kind of trick to be done here.
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.
Honestly, given the definition of foldr
as foldl
(strict) on the reversed list, and the initial impetus for this change in #111 , I'm at a loss for how such a thing could be done.
Would love to know if you have something in mind.
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 wasn't sure if it was possible, but thanks for trying. This looks good!
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.
Ah, I thought you knew of a way and were attempting to deliver it from me Socratically :p
Could you please rebase this? Thanks! |
Also can this be called |
addresses purescript#111
rebased and renamed |
Thanks! |
addresses #111
This looks a bit different from the signature discussed in that issue, but my inclination is that this is what we are actually looking for. Happy to discuss better ways if anyone can think of one, though.