Skip to content

Indexed foldable-traversable instances #23

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

Merged
merged 4 commits into from
Nov 21, 2017
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
TraversableWithIndex instance
  • Loading branch information
matthewleon committed Nov 19, 2017
commit 70972d45bd64959b4b9e8f09d8ab38dbd19db7f0
7 changes: 7 additions & 0 deletions src/Data/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Data.FoldableWithIndex (class FoldableWithIndex, foldMapWithIndex, foldlW
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
import Data.Ord (class Ord1, compare1)
import Data.Traversable (class Traversable, traverse, sequence)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)

-- | A non-empty container of elements of type a.
-- |
Expand Down Expand Up @@ -113,3 +114,9 @@ instance foldableWithIndexNonEmpty
instance traversableNonEmpty :: Traversable f => Traversable (NonEmpty f) where
sequence (a :| fa) = NonEmpty <$> a <*> sequence fa
traverse f (a :| fa) = NonEmpty <$> f a <*> traverse f fa

instance traversableWithIndexNonEmpty
:: (TraversableWithIndex i f, Semiring i)
=> TraversableWithIndex i (NonEmpty f) where
traverseWithIndex f (a :| fa) =
NonEmpty <$> f zero a <*> traverseWithIndex (incIndex f) fa