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

catMaybes and mapMaybe written generally #2

Closed
tonymorris opened this issue Jan 16, 2017 · 0 comments
Closed

catMaybes and mapMaybe written generally #2

tonymorris opened this issue Jan 16, 2017 · 0 comments

Comments

@tonymorris
Copy link
Collaborator

Belongs in papa-lens-implement

import Control.Lens
import Data.Monoid

newtype Compose f g a =
  Compose (f (g a))

instance (Foldable f, Foldable g) => Foldable (Compose f g) where
  foldr f z (Compose x) =
    foldr (\a b -> foldr f b a) z x

collapse0 ::
 (Cons r r a a, AsEmpty r) =>
 Getting (Endo r) s a
 -> s
 -> r
collapse0 x =
  foldrOf x cons (_Empty # ())

collapse1 ::
  (Cons r r a a, AsEmpty r) =>
  Getting (Endo r) (Compose f g b) a
  -> f (g b)
  -> r
collapse1 x =
  collapse0 x . Compose

collapse2 ::
  (Cons r r a a, AsEmpty r) =>
  Getting (Endo r) (Compose (Compose f g) h b) a
  -> f (g (h b))
  -> r
collapse2 x =
  collapse1 x . Compose

map1 ::
  (Cons r r a a, AsEmpty r, Functor f) =>
  Getting (Endo r) (f c) a
  -> (b -> c)
  -> f b
  -> r
map1 x f =
  collapse0 x . fmap f

map2 ::
  (Cons r r a a, AsEmpty r, Functor f) =>
  Getting (Endo r) (Compose f g c) a
  -> (b -> g c)
  -> f b
  -> r
map2 x f =
  collapse1 x . fmap f

map3 ::
  (Cons r r a a, AsEmpty r, Functor f) =>
  Getting (Endo r) (Compose (Compose f g) h c) a
  -> (b -> g (h c))
  -> f b
  -> r
map3 x f =
  collapse2 x . fmap f

----

mapMaybe ::
  (a -> Maybe b)
  -> [a]
  -> [b]
mapMaybe = 
  map2 folded

catMaybes ::
  [Maybe a]
  -> [a]
catMaybes =
  collapse1 folded
tonymorris added a commit to data61/papa-lens-implement that referenced this issue Jan 16, 2017
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

No branches or pull requests

1 participant