Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Data.Array
, tail
, init
, uncons
, unsnoc

, (!!), index
, elemIndex
Expand Down Expand Up @@ -267,6 +268,12 @@ foreign import uncons'
-> Array a
-> b

-- | Break an array into its last element and all preceding elements.
-- |
-- | Running time: `O(n)` where `n` is the length of the array
unsnoc :: forall a. Array a -> Maybe { init :: Array a, last :: a }
unsnoc xs = { init: _, last: _ } <$> init xs <*> last xs

--------------------------------------------------------------------------------
-- Indexed operations ----------------------------------------------------------
--------------------------------------------------------------------------------
Expand Down
13 changes: 12 additions & 1 deletion test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (log, CONSOLE)

import Data.Array (range, replicate, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, mapWithIndex, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, init, tail, last, head, insertBy, insert, snoc, (:), length, null, singleton, fromFoldable)
import Data.Array (range, replicate, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, mapWithIndex, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, (:), length, null, singleton, fromFoldable)
import Data.Foldable (for_, foldMapDefaultR, class Foldable, all)
import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.NonEmpty ((:|))
Expand Down Expand Up @@ -113,6 +113,17 @@ testArray = do
assert $ (unsafePartial $ fromJust u2).head == 1
assert $ (unsafePartial $ fromJust u2).tail == [2, 3]

log "unsnoc should return nothing when used on an empty array"
assert $ isNothing (unsnoc nil)

log "unsnoc should split an array into an init and last record when there is at least one item"
let u3 = unsnoc [1]
assert $ (unsafePartial $ fromJust u3).init == []
assert $ (unsafePartial $ fromJust u3).last == 1
let u4 = unsnoc [1, 2, 3]
assert $ (unsafePartial $ fromJust u4).init == [1, 2]
assert $ (unsafePartial $ fromJust u4).last == 3

log "(!!) should return Just x when the index is within the bounds of the array"
assert $ [1, 2, 3] !! 0 == (Just 1)
assert $ [1, 2, 3] !! 1 == (Just 2)
Expand Down