Skip to content
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

Added premapMaybe(M) #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Added premapMaybe(M) #95

wants to merge 1 commit into from

Conversation

YPares
Copy link
Contributor

@YPares YPares commented Nov 6, 2017

While I was at it :)
Derived from mapMaybe (in base, Data.Maybe).
It can be useful for the same reasons than prefilter.

@Gabriella439
Copy link
Owner

My main suggestion here is to refactor this as:

folded :: Foldable f => Fold a b -> Fold (f a) b
folded (Fold step begin done) = Fold (foldl' step) begin done

... and then you don't need premapMaybe because it becomes the composition of premap and folded (where f is instantiated to Maybe)

@Profpatsch
Copy link

I just implemented the same, because it’s such a common function when dealing with Foldables

-- | Applies a mapMaybe filter to every input element in the fold.
-- Every element for which `Nothing` is returned is dropped from the input.
premapMaybe :: (b -> Maybe a) -> Fold a r -> Fold b r
premapMaybe f (Fold step begin done) = Fold step' begin done
  where
    step' x b = case f b of
      Nothing -> x
      Just a' -> step x a'

Another idea is to also have Fold implement Filterable and maybe Witherable from https://hackage.haskell.org/package/witherable-0.4.2/docs/Witherable.html

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.

3 participants