From 33975f15a6687504206b7eb69ecd0af988409846 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 28 Dec 2020 08:30:54 -0800 Subject: [PATCH 1/6] Rename group' to groupAll and add groupAllBy; add docs --- src/Data/Array/NonEmpty.purs | 39 +++++++++++++++++++++++++++++- test/Test/Data/Array/NonEmpty.purs | 13 ++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Data/Array/NonEmpty.purs b/src/Data/Array/NonEmpty.purs index 7ba50641..8af383d3 100644 --- a/src/Data/Array/NonEmpty.purs +++ b/src/Data/Array/NonEmpty.purs @@ -66,8 +66,10 @@ module Data.Array.NonEmpty , dropWhile , span , group + , groupAll , group' , groupBy + , groupAllBy , nub , nubBy @@ -118,6 +120,7 @@ import Data.Tuple (Tuple(..)) import Data.Unfoldable (class Unfoldable) import Data.Unfoldable1 (class Unfoldable1, unfoldr1) import Partial.Unsafe (unsafePartial) +import Prim.TypeError (class Warn, Text) import Unsafe.Coerce (unsafeCoerce) -- | Internal - adapt an Array transform to NonEmptyArray @@ -350,15 +353,49 @@ span -> { init :: Array a, rest :: Array a } span f = adaptAny $ A.span f +-- | Group equal, consecutive elements of an array into arrays. +-- | +-- | ```purescript +-- | group (NonEmptyArray [1, 1, 2, 2, 1]) == +-- | NonEmptyArray [NonEmptyArray [1, 1], NonEmptyArray [2, 2], NonEmptyArray [1]] +-- | ``` group :: forall a. Eq a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) group = unsafeAdapt $ A.group -group' :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) +-- | Group equal elements of an array into arrays. +-- | +-- | ```purescript +-- | groupAll (NonEmptyArray [1, 1, 2, 2, 1]) == +-- | NonEmptyArray [NonEmptyArray [1, 1, 1], NonEmptyArray [2, 2]] +-- | ` +groupAll :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) +groupAll = groupAllBy eq + +-- | Deprecated previous name of `groupAll`. +group' :: forall a. Warn (Text "'group\'' is deprecated, use groupAll instead") => Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) group' = unsafeAdapt $ A.group' +-- | Group equal, consecutive elements of an array into arrays, using the +-- | specified equivalence relation to determine equality. +-- | +-- | ```purescript +-- | groupBy (\a b -> odd a && odd b) (NonEmptyArray [1, 3, 2, 4, 3, 3]) +-- | = NonEmptyArray [NonEmptyArray [1, 3], NonEmptyArray [2], NonEmptyArray [4], NonEmptyArray [3, 3]] +-- | ``` +-- | groupBy :: forall a. (a -> a -> Boolean) -> NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) groupBy op = unsafeAdapt $ A.groupBy op +-- | Group equal elements of an array into arrays, using the specified +-- | equivalence relation to determine equality. +-- | +-- | ```purescript +-- | groupAllBy (\a b -> odd a && odd b) (NonEmptyArray [1, 3, 2, 4, 3]) +-- | = NonEmptyArray [NonEmptyArray [1], NonEmptyArray [2], NonEmptyArray [3, 3], NonEmptyArray [4]] +-- | ``` +groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) +groupAllBy op = unsafeAdapt $ A.groupAllBy op + nub :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray a nub = unsafeAdapt A.nub diff --git a/test/Test/Data/Array/NonEmpty.purs b/test/Test/Data/Array/NonEmpty.purs index a9ffead2..2532bc13 100644 --- a/test/Test/Data/Array/NonEmpty.purs +++ b/test/Test/Data/Array/NonEmpty.purs @@ -25,6 +25,9 @@ testNonEmptyArray = do let fromArray :: forall a. Array a -> NEA.NonEmptyArray a fromArray = unsafePartial fromJust <<< NEA.fromArray + nea :: forall a. Array a -> NEA.NonEmptyArray a + nea = fromArray + log "singleton should construct an array with a single value" assert $ NEA.toArray (NEA.singleton 1) == [1] assert $ NEA.toArray (NEA.singleton "foo") == ["foo"] @@ -247,8 +250,8 @@ testNonEmptyArray = do log "group should group consecutive equal elements into arrays" assert $ NEA.group (fromArray [1, 2, 2, 3, 3, 3, 1]) == fromArray [NEA.singleton 1, fromArray [2, 2], fromArray [3, 3, 3], NEA.singleton 1] - log "group' should sort then group consecutive equal elements into arrays" - assert $ NEA.group' (fromArray [1, 2, 2, 3, 3, 3, 1]) == fromArray [fromArray [1, 1], fromArray [2, 2], fromArray [3, 3, 3]] + log "groupAll should group equal elements into arrays" + assert $ NEA.groupAll (fromArray [1, 2, 2, 3, 3, 3, 1]) == fromArray [fromArray [1, 1], fromArray [2, 2], fromArray [3, 3, 3]] log "groupBy should group consecutive equal elements into arrays based on an equivalence relation" assert $ NEA.groupBy (\x y -> odd x && odd y) (fromArray [1, 1, 2, 2, 3, 3]) == fromArray [fromArray [1, 1], NEA.singleton 2, NEA.singleton 2, fromArray [3, 3]] @@ -256,6 +259,12 @@ testNonEmptyArray = do log "groupBy should be stable" assert $ NEA.groupBy (\_ _ -> true) (fromArray [1, 2, 3]) == fromArray [fromArray [1, 2, 3]] + log "groupAllBy should group equal elements into arrays based on an equivalence relation" + assert $ NEA.groupAllBy (\x y -> odd x && odd y) (fromArray [1, 3, 2, 4, 3, 3]) == fromArray [nea [1], nea [2], nea [3, 3, 3], nea [4]] + + log "groupAllBy should be stable" + assert $ NEA.groupAllBy (\_ _ -> true) (fromArray [1, 2, 3]) == fromArray [nea [1, 2, 3]] + log "nub should remove duplicate elements from the list, keeping the first occurence" assert $ NEA.nub (fromArray [1, 2, 2, 3, 4, 1]) == fromArray [1, 2, 3, 4] From f6abfaef9147b736661d18b60bbe1637d1566492 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 28 Dec 2020 09:20:06 -0800 Subject: [PATCH 2/6] Replace implementation's deprecated `group'` with `groupAll` --- src/Data/Array/NonEmpty.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Array/NonEmpty.purs b/src/Data/Array/NonEmpty.purs index 8af383d3..2fa95d22 100644 --- a/src/Data/Array/NonEmpty.purs +++ b/src/Data/Array/NonEmpty.purs @@ -373,7 +373,7 @@ groupAll = groupAllBy eq -- | Deprecated previous name of `groupAll`. group' :: forall a. Warn (Text "'group\'' is deprecated, use groupAll instead") => Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) -group' = unsafeAdapt $ A.group' +group' = unsafeAdapt $ A.groupAll -- | Group equal, consecutive elements of an array into arrays, using the -- | specified equivalence relation to determine equality. From e489ea17f3524999710ce6e1c96ef7f52826030a Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 28 Dec 2020 09:30:18 -0800 Subject: [PATCH 3/6] Quote `groupAll` in deprecation warning --- src/Data/Array.purs | 2 +- src/Data/Array/NonEmpty.purs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index ae535298..d73b7f03 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -962,7 +962,7 @@ groupAll :: forall a. Ord a => Array a -> Array (NonEmptyArray a) groupAll = groupAllBy eq -- | Deprecated previous name of `groupAll`. -group' :: forall a. Warn (Text "'group\'' is deprecated, use groupAll instead") => Ord a => Array a -> Array (NonEmptyArray a) +group' :: forall a. Warn (Text "'group\'' is deprecated, use 'groupAll' instead") => Ord a => Array a -> Array (NonEmptyArray a) group' = groupAll -- | Group equal, consecutive elements of an array into arrays, using the diff --git a/src/Data/Array/NonEmpty.purs b/src/Data/Array/NonEmpty.purs index 2fa95d22..f25adf78 100644 --- a/src/Data/Array/NonEmpty.purs +++ b/src/Data/Array/NonEmpty.purs @@ -372,7 +372,7 @@ groupAll :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a groupAll = groupAllBy eq -- | Deprecated previous name of `groupAll`. -group' :: forall a. Warn (Text "'group\'' is deprecated, use groupAll instead") => Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) +group' :: forall a. Warn (Text "'group\'' is deprecated, use 'groupAll' instead") => Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) group' = unsafeAdapt $ A.groupAll -- | Group equal, consecutive elements of an array into arrays, using the From b6c34d9c7d6998d5d91f30334a3a421c304e1d99 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 28 Dec 2020 09:59:36 -0800 Subject: [PATCH 4/6] Update `groupAllBy` to use `compare` for both sorting and grouping --- src/Data/Array.purs | 12 ++++++------ src/Data/Array/NonEmpty.purs | 8 ++++---- test/Test/Data/Array.purs | 4 ++-- test/Test/Data/Array/NonEmpty.purs | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index d73b7f03..f0ca76ad 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -959,7 +959,7 @@ group xs = groupBy eq xs -- | groupAll [1, 1, 2, 2, 1] == [NonEmptyArray [1, 1, 1], NonEmptyArray [2, 2]] -- | ``` groupAll :: forall a. Ord a => Array a -> Array (NonEmptyArray a) -groupAll = groupAllBy eq +groupAll = groupAllBy compare -- | Deprecated previous name of `groupAll`. group' :: forall a. Warn (Text "'group\'' is deprecated, use 'groupAll' instead") => Ord a => Array a -> Array (NonEmptyArray a) @@ -987,15 +987,15 @@ groupBy op xs = STA.unsafeFreeze result -- | Group equal elements of an array into arrays, using the specified --- | equivalence relation to determine equality. +-- | comparison relation to determine equality. -- | -- | ```purescript --- | groupAllBy (\a b -> odd a && odd b) [1, 3, 2, 4, 3, 3] --- | = [NonEmptyArray [1], NonEmptyArray [2], NonEmptyArray [3, 3, 3], NonEmptyArray [4]] +-- | groupAllBy (\a b -> compare (odd a) (odd b)) [1, 3, 2, 4, 3, 3] +-- | = [NonEmptyArray [2, 4], NonEmptyArray [1, 3, 3, 3]] -- | ``` -- | -groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> Array a -> Array (NonEmptyArray a) -groupAllBy p = groupBy p <<< sort +groupAllBy :: forall a. (a -> a -> Ordering) -> Array a -> Array (NonEmptyArray a) +groupAllBy cmp = groupBy (\x y -> cmp x y == EQ) <<< sortBy cmp -- | Remove the duplicates from an array, creating a new array. -- | diff --git a/src/Data/Array/NonEmpty.purs b/src/Data/Array/NonEmpty.purs index f25adf78..c38547f7 100644 --- a/src/Data/Array/NonEmpty.purs +++ b/src/Data/Array/NonEmpty.purs @@ -369,7 +369,7 @@ group = unsafeAdapt $ A.group -- | NonEmptyArray [NonEmptyArray [1, 1, 1], NonEmptyArray [2, 2]] -- | ` groupAll :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) -groupAll = groupAllBy eq +groupAll = groupAllBy compare -- | Deprecated previous name of `groupAll`. group' :: forall a. Warn (Text "'group\'' is deprecated, use 'groupAll' instead") => Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) @@ -390,10 +390,10 @@ groupBy op = unsafeAdapt $ A.groupBy op -- | equivalence relation to determine equality. -- | -- | ```purescript --- | groupAllBy (\a b -> odd a && odd b) (NonEmptyArray [1, 3, 2, 4, 3]) --- | = NonEmptyArray [NonEmptyArray [1], NonEmptyArray [2], NonEmptyArray [3, 3], NonEmptyArray [4]] +-- | groupAllBy (\a b -> compare (odd a) (odd b)) (NonEmptyArray [1, 3, 2, 4, 3]) +-- | = NonEmptyArray [NonEmptyArray [2, 4], NonEmptyArray [1, 3, 3, 3]] -- | ``` -groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) +groupAllBy :: forall a. (a -> a -> Ordering) -> NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) groupAllBy op = unsafeAdapt $ A.groupAllBy op nub :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray a diff --git a/test/Test/Data/Array.purs b/test/Test/Data/Array.purs index 02295089..2a2a289d 100644 --- a/test/Test/Data/Array.purs +++ b/test/Test/Data/Array.purs @@ -363,10 +363,10 @@ testArray = do assert $ A.groupBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]] log "groupAllBy should group equal elements into arrays based on an equivalence relation" - assert $ A.groupAllBy (\x y -> odd x && odd y) [1, 3, 2, 4, 3, 3] == [nea [1], nea [2], nea [3, 3, 3], nea [4]] + assert $ A.groupAllBy (\x y -> compare (odd x) (odd y)) [1, 3, 2, 4, 3, 3] == [nea [2, 4], nea [1, 3, 3, 3]] log "groupAllBy should be stable" - assert $ A.groupAllBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]] + assert $ A.groupAllBy (\_ _ -> EQ) [1, 2, 3] == [nea [1, 2, 3]] log "nub should remove duplicate elements from the list, keeping the first occurence" assert $ A.nub [1, 2, 2, 3, 4, 1] == [1, 2, 3, 4] diff --git a/test/Test/Data/Array/NonEmpty.purs b/test/Test/Data/Array/NonEmpty.purs index 2532bc13..c98bf754 100644 --- a/test/Test/Data/Array/NonEmpty.purs +++ b/test/Test/Data/Array/NonEmpty.purs @@ -260,10 +260,10 @@ testNonEmptyArray = do assert $ NEA.groupBy (\_ _ -> true) (fromArray [1, 2, 3]) == fromArray [fromArray [1, 2, 3]] log "groupAllBy should group equal elements into arrays based on an equivalence relation" - assert $ NEA.groupAllBy (\x y -> odd x && odd y) (fromArray [1, 3, 2, 4, 3, 3]) == fromArray [nea [1], nea [2], nea [3, 3, 3], nea [4]] + assert $ NEA.groupAllBy (\x y -> compare (odd x) (odd y)) (fromArray [1, 3, 2, 4, 3, 3]) == fromArray [nea [2, 4], nea [1, 3, 3, 3]] log "groupAllBy should be stable" - assert $ NEA.groupAllBy (\_ _ -> true) (fromArray [1, 2, 3]) == fromArray [nea [1, 2, 3]] + assert $ NEA.groupAllBy (\_ _ -> EQ) (fromArray [1, 2, 3]) == fromArray [nea [1, 2, 3]] log "nub should remove duplicate elements from the list, keeping the first occurence" assert $ NEA.nub (fromArray [1, 2, 2, 3, 4, 1]) == fromArray [1, 2, 3, 4] From 717446a7e9ef49cdbc3d394412669fc64f58c117 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 28 Dec 2020 11:10:15 -0800 Subject: [PATCH 5/6] Address feedback: update docs and use `comparing Down` as example --- src/Data/Array.purs | 6 +++--- src/Data/Array/NonEmpty.purs | 6 +++--- test/Test/Data/Array.purs | 5 +++-- test/Test/Data/Array/NonEmpty.purs | 5 +++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index f0ca76ad..4d72bb85 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -987,11 +987,11 @@ groupBy op xs = STA.unsafeFreeze result -- | Group equal elements of an array into arrays, using the specified --- | comparison relation to determine equality. +-- | partial ordering relation to determine equality. -- | -- | ```purescript --- | groupAllBy (\a b -> compare (odd a) (odd b)) [1, 3, 2, 4, 3, 3] --- | = [NonEmptyArray [2, 4], NonEmptyArray [1, 3, 3, 3]] +-- | groupAllBy (comparing Down) [1, 3, 2, 4, 3, 3] +-- | = [NonEmptyArray [4], NonEmptyArray [3, 3, 3], NonEmptyArray [2], NonEmptyArray [1]] -- | ``` -- | groupAllBy :: forall a. (a -> a -> Ordering) -> Array a -> Array (NonEmptyArray a) diff --git a/src/Data/Array/NonEmpty.purs b/src/Data/Array/NonEmpty.purs index c38547f7..88971d23 100644 --- a/src/Data/Array/NonEmpty.purs +++ b/src/Data/Array/NonEmpty.purs @@ -387,11 +387,11 @@ groupBy :: forall a. (a -> a -> Boolean) -> NonEmptyArray a -> NonEmptyArray (No groupBy op = unsafeAdapt $ A.groupBy op -- | Group equal elements of an array into arrays, using the specified --- | equivalence relation to determine equality. +-- | partial ordering relation to determine equality. -- | -- | ```purescript --- | groupAllBy (\a b -> compare (odd a) (odd b)) (NonEmptyArray [1, 3, 2, 4, 3]) --- | = NonEmptyArray [NonEmptyArray [2, 4], NonEmptyArray [1, 3, 3, 3]] +-- | groupAllBy (comparing Down) (NonEmptyArray [1, 3, 2, 4, 3, 3]) +-- | = NonEmptyArray [NonEmptyArray [4], NonEmptyArray [3, 3, 3], NonEmptyArray [2], NonEmptyArray [1]] -- | ``` groupAllBy :: forall a. (a -> a -> Ordering) -> NonEmptyArray a -> NonEmptyArray (NonEmptyArray a) groupAllBy op = unsafeAdapt $ A.groupAllBy op diff --git a/test/Test/Data/Array.purs b/test/Test/Data/Array.purs index 2a2a289d..a778bec3 100644 --- a/test/Test/Data/Array.purs +++ b/test/Test/Data/Array.purs @@ -9,6 +9,7 @@ import Data.Const (Const(..)) import Data.Foldable (for_, foldMapDefaultR, class Foldable, all, traverse_) import Data.Traversable (scanl, scanr) import Data.Maybe (Maybe(..), isNothing, fromJust) +import Data.Ord.Down (Down(..)) import Data.Tuple (Tuple(..)) import Data.Unfoldable (replicateA) import Effect (Effect) @@ -362,8 +363,8 @@ testArray = do log "groupBy should be stable" assert $ A.groupBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]] - log "groupAllBy should group equal elements into arrays based on an equivalence relation" - assert $ A.groupAllBy (\x y -> compare (odd x) (odd y)) [1, 3, 2, 4, 3, 3] == [nea [2, 4], nea [1, 3, 3, 3]] + log "groupAllBy should group equal elements into arrays based on the result of a comparison function" + assert $ A.groupAllBy (comparing Down) [1, 3, 2, 4, 3, 3] == [nea [4], nea [3, 3, 3], nea [2], nea [1]] log "groupAllBy should be stable" assert $ A.groupAllBy (\_ _ -> EQ) [1, 2, 3] == [nea [1, 2, 3]] diff --git a/test/Test/Data/Array/NonEmpty.purs b/test/Test/Data/Array/NonEmpty.purs index c98bf754..cb183df1 100644 --- a/test/Test/Data/Array/NonEmpty.purs +++ b/test/Test/Data/Array/NonEmpty.purs @@ -10,6 +10,7 @@ import Data.FunctorWithIndex (mapWithIndex) import Data.Maybe (Maybe(..), fromJust) import Data.Monoid.Additive (Additive(..)) import Data.NonEmpty ((:|)) +import Data.Ord.Down (Down(..)) import Data.Semigroup.Foldable (foldMap1, foldr1, foldl1) import Data.Semigroup.Traversable (traverse1) import Data.Tuple (Tuple(..)) @@ -259,8 +260,8 @@ testNonEmptyArray = do log "groupBy should be stable" assert $ NEA.groupBy (\_ _ -> true) (fromArray [1, 2, 3]) == fromArray [fromArray [1, 2, 3]] - log "groupAllBy should group equal elements into arrays based on an equivalence relation" - assert $ NEA.groupAllBy (\x y -> compare (odd x) (odd y)) (fromArray [1, 3, 2, 4, 3, 3]) == fromArray [nea [2, 4], nea [1, 3, 3, 3]] + log "groupAllBy should group equal elements into arrays based on the result of a comparison function" + assert $ NEA.groupAllBy (comparing Down) (fromArray [1, 3, 2, 4, 3, 3]) == fromArray [nea [4], nea [3, 3, 3], nea [2], nea [1]] log "groupAllBy should be stable" assert $ NEA.groupAllBy (\_ _ -> EQ) (fromArray [1, 2, 3]) == fromArray [nea [1, 2, 3]] From 9ed75eb20db2d53709f8738752236671e6f37955 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 28 Dec 2020 11:14:18 -0800 Subject: [PATCH 6/6] Revert back to 'comparison function' phrasing for docs --- src/Data/Array.purs | 2 +- src/Data/Array/NonEmpty.purs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 4d72bb85..fd7b503e 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -987,7 +987,7 @@ groupBy op xs = STA.unsafeFreeze result -- | Group equal elements of an array into arrays, using the specified --- | partial ordering relation to determine equality. +-- | comparison function to determine equality. -- | -- | ```purescript -- | groupAllBy (comparing Down) [1, 3, 2, 4, 3, 3] diff --git a/src/Data/Array/NonEmpty.purs b/src/Data/Array/NonEmpty.purs index 88971d23..bbc51af8 100644 --- a/src/Data/Array/NonEmpty.purs +++ b/src/Data/Array/NonEmpty.purs @@ -387,7 +387,7 @@ groupBy :: forall a. (a -> a -> Boolean) -> NonEmptyArray a -> NonEmptyArray (No groupBy op = unsafeAdapt $ A.groupBy op -- | Group equal elements of an array into arrays, using the specified --- | partial ordering relation to determine equality. +-- | comparison function to determine equality. -- | -- | ```purescript -- | groupAllBy (comparing Down) (NonEmptyArray [1, 3, 2, 4, 3, 3])