-
Notifications
You must be signed in to change notification settings - Fork 69
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
Changes from 5 commits
f109f0b
ce1dd88
f8ee31b
cd2ad76
a69d096
f75b2ab
dcbd3c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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. | ||
-- | ```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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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."