Skip to content

Add intersperse with tests #188

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 7 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
27 changes: 27 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module Data.Array
, modifyAtIndices
, alterAt

, intersperse
, reverse
, concat
, concatMap
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -551,6 +553,31 @@ 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.
-- |
-- | ```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 input array is returned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "less than two" might read a bit more naturally here? Up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. I think I could go either way.
Meh, let's just use the "less than two."

-- | ```purescript
-- | 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for is re-exported from Control.Monad.ST, so I don't think we need to import the Internal module here (and if we can avoid importing the Internal module, it would be good to do that).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Good catch! Fixed in latest commits.

_ <- STA.push a out
void (STA.push (unsafeGetElem idx) out)
pure out

-- | Reverse an array, creating a new array.
-- |
-- | ```purescript
Expand Down
8 changes: 8 additions & 0 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down