Skip to content

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

Merged
merged 1 commit into from
Jun 4, 2017
Merged

foldr': lazy right folds #118

merged 1 commit into from
Jun 4, 2017

Conversation

matthewleon
Copy link
Contributor

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.

@@ -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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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!

Copy link
Contributor Author

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

@paf31
Copy link
Contributor

paf31 commented Jun 3, 2017

Could you please rebase this? Thanks!

@paf31
Copy link
Contributor

paf31 commented Jun 3, 2017

Also can this be called foldrLazy instead? I'm not keen on using primes in general. Thanks!

@matthewleon
Copy link
Contributor Author

rebased and renamed

@paf31 paf31 merged commit 6d1c863 into purescript:master Jun 4, 2017
@paf31
Copy link
Contributor

paf31 commented Jun 4, 2017

Thanks!

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.

2 participants