Skip to content

Commit 4473953

Browse files
authored
Merge pull request #82 from purescript/unsnoc
Add unsnoc
2 parents 845fb13 + 75b4556 commit 4473953

File tree

2 files changed

+144
-125
lines changed

2 files changed

+144
-125
lines changed

src/Data/Array.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module Data.Array
4949
, tail
5050
, init
5151
, uncons
52+
, unsnoc
5253

5354
, (!!), index
5455
, elemIndex
@@ -272,6 +273,12 @@ foreign import uncons'
272273
-> Array a
273274
-> b
274275

276+
-- | Break an array into its last element and all preceding elements.
277+
-- |
278+
-- | Running time: `O(n)` where `n` is the length of the array
279+
unsnoc :: forall a. Array a -> Maybe { init :: Array a, last :: a }
280+
unsnoc xs = { init: _, last: _ } <$> init xs <*> last xs
281+
275282
--------------------------------------------------------------------------------
276283
-- Indexed operations ----------------------------------------------------------
277284
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)