Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Added semigroup instance (#18 by @jmatsushita)

Bugfixes:

Expand Down
8 changes: 8 additions & 0 deletions src/Data/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,11 @@ instance foldable1NonEmpty :: Foldable f => Foldable1 (NonEmpty f) where

instance unfoldable1NonEmpty :: Unfoldable f => Unfoldable1 (NonEmpty f) where
unfoldr1 f b = uncurry (:|) $ unfoldr (map f) <$> f b

-- | This is a lawful `Semigroup` instance that will behave sensibly for common nonempty
-- | containers like lists and arrays. However, it's not guaranteed that `pure` will behave
-- | sensibly alongside `<>` for all types, as we don't have any laws which govern their behavior.
instance semigroupNonEmpty
:: (Applicative f, Semigroup (f a))
=> Semigroup (NonEmpty f a) where
append (a1 :| f1) (a2 :| f2) = a1 :| (f1 <> pure a2 <> f2)
1 change: 1 addition & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ main = do
assert $ oneOf (0 :| Nothing) == oneOf (0 :| Just 1)
assert $ second (1 :| 2 :| [3, 4]) == 2
assert $ U1.range 0 9 == (0 :| [1, 2, 3, 4, 5, 6, 7, 8, 9])
assert $ (0 :| [1,2]) <> (3 :| [4,5]) == (0 :| [1, 2, 3, 4, 5])