From 94763fda24b4a8a7e8902712b2e119db8ca91be0 Mon Sep 17 00:00:00 2001 From: Alex Sturtz Date: Sun, 31 Mar 2019 18:32:01 -0500 Subject: [PATCH 1/6] Fixes #18. Add Semigroup instance for NonEmpty. --- src/Data/NonEmpty.purs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Data/NonEmpty.purs b/src/Data/NonEmpty.purs index 4135c5c..1901447 100644 --- a/src/Data/NonEmpty.purs +++ b/src/Data/NonEmpty.purs @@ -167,3 +167,8 @@ 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 + +instance semigroupNonEmpty + :: (Applicative f, Semigroup (f a)) + => Semigroup (NonEmpty f a) where + append (a1 :| f1) (a2 :| f2) = a1 :| (f1 <> pure a2 <> f2) From 4a98dcc5d6ec68c16f575cfe00864ef3e5d3c999 Mon Sep 17 00:00:00 2001 From: Jun Matsushita Date: Tue, 19 Oct 2021 21:47:52 +0200 Subject: [PATCH 2/6] Add documentation to the Semigroup instance #18 --- src/Data/NonEmpty.purs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Data/NonEmpty.purs b/src/Data/NonEmpty.purs index 1901447..8d06db1 100644 --- a/src/Data/NonEmpty.purs +++ b/src/Data/NonEmpty.purs @@ -168,6 +168,9 @@ 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 +-- | For `Semigroup` we don't know for sure that `pure` will behave sensibly +-- | alongside `<>` because we don't have any laws which govern their +-- | behaviour, in practice, with Array, List, it should behave sensibly. instance semigroupNonEmpty :: (Applicative f, Semigroup (f a)) => Semigroup (NonEmpty f a) where From 9b514fef9dad99bb17b71eda6e743ae1a28c5328 Mon Sep 17 00:00:00 2001 From: Jun Matsushita Date: Tue, 19 Oct 2021 21:52:02 +0200 Subject: [PATCH 3/6] Simple semigroup instance test --- test/Main.purs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Main.purs b/test/Main.purs index acf08e7..14be533 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -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]) From 7c23b5b2df3ed42218887548f40e153b256aa999 Mon Sep 17 00:00:00 2001 From: Jun Matsushita Date: Tue, 19 Oct 2021 21:52:13 +0200 Subject: [PATCH 4/6] Semigroup instance --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b667c25..473f88c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) Bugfixes: From 686310ef418b54079a4fcd91bdc164e4edfb2210 Mon Sep 17 00:00:00 2001 From: Jun Matsushita Date: Thu, 21 Oct 2021 21:32:22 +0200 Subject: [PATCH 5/6] Update CHANGELOG.md Co-authored-by: Thomas Honeyman --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 473f88c..251b5e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: -- Added semigroup instance (#18) +- Added semigroup instance (#18 by @jmatsushita) Bugfixes: From 444beb50060b7c1f54aa35f406129ae49ab07cf0 Mon Sep 17 00:00:00 2001 From: Jun Matsushita Date: Thu, 21 Oct 2021 21:32:38 +0200 Subject: [PATCH 6/6] Improve documentation Co-authored-by: Thomas Honeyman --- src/Data/NonEmpty.purs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Data/NonEmpty.purs b/src/Data/NonEmpty.purs index 8d06db1..11d6bc8 100644 --- a/src/Data/NonEmpty.purs +++ b/src/Data/NonEmpty.purs @@ -168,9 +168,9 @@ 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 --- | For `Semigroup` we don't know for sure that `pure` will behave sensibly --- | alongside `<>` because we don't have any laws which govern their --- | behaviour, in practice, with Array, List, it should behave sensibly. +-- | 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