Skip to content

Add map function #32

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Data/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Data.Ord (class Ord1, compare1)
import Data.Semigroup.Foldable as F1
import Data.Traversable (class Traversable, traverse, sequence)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
import Prelude as Prelude

-- | A non-empty container of elements of type a.
-- |
Expand Down Expand Up @@ -71,6 +72,11 @@ head (x :| _) = x
tail :: forall f a. NonEmpty f a -> f a
tail (_ :| xs) = xs

-- | Useful for functorish non-functors like `Set`.
-- > NonEmpty.map Set.map (_ + 1)
map :: forall a b f. ((a -> b) -> f a -> f b) -> (a -> b) -> NonEmpty f a -> NonEmpty f b
map mapper f (a :| as) = (f a :| mapper f as)

instance showNonEmpty :: (Show a, Show (f a)) => Show (NonEmpty f a) where
show (a :| fa) = "(NonEmpty " <> show a <> " " <> show fa <> ")"

Expand All @@ -90,7 +96,7 @@ instance ord1NonEmpty :: Ord1 f => Ord1 (NonEmpty f) where
c -> c

instance functorNonEmpty :: Functor f => Functor (NonEmpty f) where
map f (a :| fa) = f a :| map f fa
map f (a :| fa) = f a :| Prelude.map f fa

instance functorWithIndex
:: FunctorWithIndex i f
Expand Down