From f109f0b26185edf08fd533d54bae3c5f915bd02c Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Thu, 26 Nov 2020 20:30:40 -0800 Subject: [PATCH 1/7] Add intersperse with tests --- src/Data/Array.purs | 29 +++++++++++++++++++++++++++++ test/Test/Data/Array.purs | 8 ++++++++ 2 files changed, 37 insertions(+) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index cb1dee91..ca2da6bf 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -64,6 +64,7 @@ module Data.Array , modifyAtIndices , alterAt + , intersperse , reverse , concat , concatMap @@ -123,6 +124,7 @@ import Control.Alternative (class Alternative) import Control.Lazy (class Lazy, defer) import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM2) import Control.Monad.ST as ST +import Control.Monad.ST.Internal as STI import Data.Array.NonEmpty.Internal (NonEmptyArray(..)) import Data.Array.ST as STA import Data.Array.ST.Iterator as STAI @@ -551,6 +553,33 @@ alterAt i f xs = maybe Nothing go (xs !! i) -- Transformations ------------------------------------------------------------- -------------------------------------------------------------------------------- +-- | Inserts the given element in-between each element in the array. The array +-- | must have two or more elements for this operation to take effect. +-- | +-- | For example: +-- | ``` +-- | intersperse " " [ "a", "b" ] == [ "a", " ", "b" ] +-- | +-- | intersperse 0 [ 1, 2, 3, 4, 5 ] == [ 1, 0, 2, 0, 3, 0, 4, 0, 5 ] +-- | ``` +-- | If the array has one or zero elements, the outputted array is the same +-- | as the input array. +-- | ``` +-- | intersperse " " [] == [] +-- | intersperse " " ["a"] == ["a"] +-- | ``` +intersperse :: forall a. a -> Array a -> Array a +intersperse a arr = case length arr of + len | len < 2 -> arr + | otherwise -> STA.run do + let unsafeGetElem idx = unsafePartial (unsafeIndex arr idx) + out <- STA.empty + _ <- STA.push (unsafeGetElem 0) out + STI.for 1 len \idx -> do + _ <- STA.push a out + void (STA.push (unsafeGetElem idx) out) + pure out + -- | Reverse an array, creating a new array. -- | -- | ```purescript diff --git a/test/Test/Data/Array.purs b/test/Test/Data/Array.purs index 39ff57a8..ad0b0659 100644 --- a/test/Test/Data/Array.purs +++ b/test/Test/Data/Array.purs @@ -187,6 +187,14 @@ testArray = do log "alterAt should return Nothing if the index is out of A.range" assert $ (A.alterAt 1 (Just <<< (_ + 1)) nil) == Nothing + log "intersperse should return the original array when given an array with zero or one elements" + assert $ (A.intersperse " " []) == [] + assert $ (A.intersperse " " ["a"]) == ["a"] + + log "intersperse should insert the given element in-between each element in an array with two or more elements" + assert $ (A.intersperse " " ["a", "b"]) == ["a", " ", "b"] + assert $ (A.intersperse 0 [ 1, 2, 3, 4, 5 ]) == [ 1, 0, 2, 0, 3, 0, 4, 0, 5 ] + log "reverse should reverse the order of items in an array" assert $ (A.reverse [1, 2, 3]) == [3, 2, 1] assert $ (A.reverse nil) == nil From ce1dd88b679a987cc8db573d29ed159d5145108b Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 14 Dec 2020 20:27:18 -0800 Subject: [PATCH 2/7] Return a copy of array to match docs in intersperse --- src/Data/Array.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index ca2da6bf..190ba642 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -570,7 +570,7 @@ alterAt i f xs = maybe Nothing go (xs !! i) -- | ``` intersperse :: forall a. a -> Array a -> Array a intersperse a arr = case length arr of - len | len < 2 -> arr + len | len < 2 -> STA.run (STA.thaw arr) | otherwise -> STA.run do let unsafeGetElem idx = unsafePartial (unsafeIndex arr idx) out <- STA.empty From f8ee31bf617ba0345bba0644e70ac1534f2dbefe Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 14 Dec 2020 20:28:40 -0800 Subject: [PATCH 3/7] Add purescript to indicate syntax highlighter This doesn't work currently, but it's forward-compatible when we do implement syntax highlighting --- src/Data/Array.purs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 190ba642..68fa1a39 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -557,14 +557,14 @@ alterAt i f xs = maybe Nothing go (xs !! i) -- | must have two or more elements for this operation to take effect. -- | -- | For example: --- | ``` +-- | ```purescript -- | intersperse " " [ "a", "b" ] == [ "a", " ", "b" ] -- | -- | intersperse 0 [ 1, 2, 3, 4, 5 ] == [ 1, 0, 2, 0, 3, 0, 4, 0, 5 ] -- | ``` -- | If the array has one or zero elements, the outputted array is the same -- | as the input array. --- | ``` +-- | ```purescript -- | intersperse " " [] == [] -- | intersperse " " ["a"] == ["a"] -- | ``` From cd2ad76902ec689ba6ecf77cb90687905ed287a1 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Mon, 14 Dec 2020 20:33:05 -0800 Subject: [PATCH 4/7] Incoporate feedback --- src/Data/Array.purs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 68fa1a39..78945a12 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -553,17 +553,16 @@ alterAt i f xs = maybe Nothing go (xs !! i) -- Transformations ------------------------------------------------------------- -------------------------------------------------------------------------------- --- | Inserts the given element in-between each element in the array. The array +-- | Inserts the given element in between each element in the array. The array -- | must have two or more elements for this operation to take effect. -- | --- | For example: -- | ```purescript -- | intersperse " " [ "a", "b" ] == [ "a", " ", "b" ] --- | -- | intersperse 0 [ 1, 2, 3, 4, 5 ] == [ 1, 0, 2, 0, 3, 0, 4, 0, 5 ] -- | ``` --- | If the array has one or zero elements, the outputted array is the same --- | as the input array. +-- | +-- | If the array has one or zero elements, the resulting array is a copy +-- | of the input array. -- | ```purescript -- | intersperse " " [] == [] -- | intersperse " " ["a"] == ["a"] From a69d09624cfaa135f908d9474986738deb201062 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 15 Dec 2020 13:28:03 -0800 Subject: [PATCH 5/7] Return input array when interspersing cannot occur --- src/Data/Array.purs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 78945a12..8cb0460b 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -561,15 +561,14 @@ alterAt i f xs = maybe Nothing go (xs !! i) -- | intersperse 0 [ 1, 2, 3, 4, 5 ] == [ 1, 0, 2, 0, 3, 0, 4, 0, 5 ] -- | ``` -- | --- | If the array has one or zero elements, the resulting array is a copy --- | of the input array. +-- | If the array has one or zero elements, the input array is returned. -- | ```purescript -- | intersperse " " [] == [] -- | intersperse " " ["a"] == ["a"] -- | ``` intersperse :: forall a. a -> Array a -> Array a intersperse a arr = case length arr of - len | len < 2 -> STA.run (STA.thaw arr) + len | len < 2 -> arr | otherwise -> STA.run do let unsafeGetElem idx = unsafePartial (unsafeIndex arr idx) out <- STA.empty From f75b2abf7a0916c0bc7eaef9ed5974627351f432 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 15 Dec 2020 19:13:32 -0800 Subject: [PATCH 6/7] Import `ST.for` from ST rather than ST.Internal --- src/Data/Array.purs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 8cb0460b..4a733a64 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -124,7 +124,6 @@ import Control.Alternative (class Alternative) import Control.Lazy (class Lazy, defer) import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM2) import Control.Monad.ST as ST -import Control.Monad.ST.Internal as STI import Data.Array.NonEmpty.Internal (NonEmptyArray(..)) import Data.Array.ST as STA import Data.Array.ST.Iterator as STAI @@ -573,7 +572,7 @@ intersperse a arr = case length arr of let unsafeGetElem idx = unsafePartial (unsafeIndex arr idx) out <- STA.empty _ <- STA.push (unsafeGetElem 0) out - STI.for 1 len \idx -> do + ST.for 1 len \idx -> do _ <- STA.push a out void (STA.push (unsafeGetElem idx) out) pure out From dcbd3c63e965ad97cdfa1b51bbc677d8926b2b59 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 15 Dec 2020 19:16:34 -0800 Subject: [PATCH 7/7] Use "less than two" phrasing --- src/Data/Array.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 4a733a64..c7a8889a 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -560,7 +560,7 @@ alterAt i f xs = maybe Nothing go (xs !! i) -- | intersperse 0 [ 1, 2, 3, 4, 5 ] == [ 1, 0, 2, 0, 3, 0, 4, 0, 5 ] -- | ``` -- | --- | If the array has one or zero elements, the input array is returned. +-- | If the array has less than two elements, the input array is returned. -- | ```purescript -- | intersperse " " [] == [] -- | intersperse " " ["a"] == ["a"]