-
Notifications
You must be signed in to change notification settings - Fork 99
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 Array.foldlM' and foldlWithKeyM' #219
base: master
Are you sure you want to change the base?
Conversation
Hi ! I was missing a monadic indexed fold, I hope you like this addition :) |
ping! |
Sorry, I seem to have missed this. I have some concerns. If we add a left fold, we will certainly also want to add a right fold, since If we add these to Why a strict fold? In strict monads (the only ones for which this strictness is really meaningful anyway), the user can insert strictness into a lazy fold by supplying a function that's strict in the accumulator. I'm not saying we necessarily shouldn't offer the strict version (there are likely efficiency benefits for certain monads), but offering only that, and not the more fundamental lazy version, seems odd. Do we get a real performance benefit by implementing this directly rather than using foldlWithKeyM :: Monad m => (a -> k -> v -> m a) -> a -> HashMap k v -> m a
foldlWithKeyM f a0 hm = foldrWithKey go pure hm a0
where
go k v r a = f a k v >>= r
{-# INLINE foldlWithKeyM #-}
foldlWithKeyM' :: Monad m => (a -> k -> v -> m a) -> a -> HashMap k v -> m a
foldlWithKeyM' f a0 hm = foldrWithKey go (pure $!) hm a0
where
go k v r !a = f a k v >>= r
{-# INLINE foldlWithKeyM' #-} |
Thank you @treeowl for your feedback! I hadn't thought of performance implications to be honest and I'll apply the changes you propose:
|
Oh, silly me... We don't need those lazy versions in |
Marking as a draft, since this PR doesn't seem to be ready for review. |
Thanks for the update, @ocramz! For future reference, here's a related feature request for Since it's an API extension without an obvious precedent, I agree that it should be discussed on the libraries list. |
No description provided.