add Array.foldlM' and foldlWithKeyM'#219
add Array.foldlM' and foldlWithKeyM'#219ocramz wants to merge 3 commits intohaskell-unordered-containers:masterfrom
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.