Skip to content

Add unsnoc #94

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 4 commits into from
Jan 29, 2017
Merged
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
19 changes: 13 additions & 6 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Data.List
, tail
, init
, uncons
, unsnoc

, (!!), index
, elemIndex
Expand Down Expand Up @@ -242,12 +243,7 @@ tail (_ : xs) = Just xs
-- |
-- | Running time: `O(n)`
init :: forall a. List a -> Maybe (List a)
init Nil = Nothing
init lst = Just $ reverse $ go lst Nil
where
go (x : Nil) acc = acc
go (x : xs) acc = go xs (x : acc)
go _ acc = acc
init lst = _.init <$> unsnoc lst

-- | Break a list into its first element, and the remaining elements,
-- | or `Nothing` if the list is empty.
Expand All @@ -257,6 +253,17 @@ uncons :: forall a. List a -> Maybe { head :: a, tail :: List a }
uncons Nil = Nothing
uncons (x : xs) = Just { head: x, tail: xs }

-- | Break a list into its last element, and the preceding elements,
-- | or `Nothing` if the list is empty.
-- |
-- | Running time: `O(n)`
unsnoc :: forall a. List a -> Maybe { init :: List a, last :: a }
unsnoc lst = (\h -> { init: reverse h.revInit, last: h.last }) <$> go lst Nil
where
go Nil acc = Nothing
go (x : Nil) acc = Just { revInit: acc, last: x }
go (x : xs) acc = go xs (x : acc)

--------------------------------------------------------------------------------
-- Indexed operations ----------------------------------------------------------
--------------------------------------------------------------------------------
Expand Down
13 changes: 12 additions & 1 deletion test/Test/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Data.List.NonEmpty as NEL
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Foldable (foldMap, foldl)
import Data.List (List(..), (..), length, range, 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, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
import Data.List (List(..), (..), length, range, 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, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.Monoid.Additive (Additive(..))
import Data.NonEmpty ((:|))
Expand Down Expand Up @@ -106,6 +106,17 @@ testList = do
assert $ unsafePartial (fromJust u2).head == 1
assert $ unsafePartial (fromJust u2).tail == l [2, 3]

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

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

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