From 0d1b28dd4b497071471b4166ef9f50912b38df26 Mon Sep 17 00:00:00 2001 From: Joshua Horowitz Date: Tue, 18 Oct 2016 02:02:33 -0700 Subject: [PATCH] Add unsnoc --- src/Data/Array.purs | 7 +++++++ test/Test/Data/Array.purs | 13 ++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 21a96d8e..fce2bcd1 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -49,6 +49,7 @@ module Data.Array , tail , init , uncons + , unsnoc , (!!), index , elemIndex @@ -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 ---------------------------------------------------------- -------------------------------------------------------------------------------- diff --git a/test/Test/Data/Array.purs b/test/Test/Data/Array.purs index 04ba492f..10d8225e 100644 --- a/test/Test/Data/Array.purs +++ b/test/Test/Data/Array.purs @@ -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 ((:|)) @@ -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)