Skip to content

Add isEmpty #222

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add isEmpty
  • Loading branch information
sigma-andex committed Jun 27, 2022
commit 09aef4d51f54a13303b075ff6104bd9fca033c86
210 changes: 107 additions & 103 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,106 +28,99 @@
-- | allowing you to STAI.iterate over an array and accumulate effects.
-- |
module Data.Array
( fromFoldable
, toUnfoldable
, singleton
, (..), range
, replicate
, some
, many

, null
, length

, (:), cons
, snoc
, insert
, insertBy

, head
, last
, tail
, init
, uncons
, unsnoc

, (!!), index
( (!!)
, (..)
, (:)
, (\\)
, all
, alterAt
, any
, catMaybes
, concat
, concatMap
, cons
, delete
, deleteAt
, deleteBy
, difference
, drop
, dropEnd
, dropWhile
, elem
, notElem
, elemIndex
, elemLastIndex
, filter
, filterA
, find
, findMap
, findIndex
, findLastIndex
, findMap
, fold
, foldM
, foldMap
, foldRecM
, foldl
, foldr
, fromFoldable
, group
, groupAll
, groupAllBy
, groupBy
, head
, index
, init
, insert
, insertAt
, deleteAt
, updateAt
, updateAtIndices
, modifyAt
, modifyAtIndices
, alterAt

, insertBy
, intercalate
, intersect
, intersectBy
, intersperse
, reverse
, concat
, concatMap
, filter
, partition
, splitAt
, filterA
, isEmpty
, last
, length
, many
, mapMaybe
, catMaybes
, mapWithIndex
, foldl
, foldr
, foldMap
, fold
, intercalate
, modifyAt
, modifyAtIndices
, notElem
, nub
, nubBy
, nubByEq
, nubEq
, null
, partition
, range
, replicate
, reverse
, scanl
, scanr

, singleton
, slice
, snoc
, some
, sort
, sortBy
, sortWith
, slice
, span
, splitAt
, tail
, take
, takeEnd
, takeWhile
, drop
, dropEnd
, dropWhile
, span
, group
, groupAll
, groupBy
, groupAllBy

, nub
, nubEq
, nubBy
, nubByEq
, toUnfoldable
, uncons
, union
, unionBy
, delete
, deleteBy

, (\\), difference
, intersect
, intersectBy

, unsafeIndex
, unsnoc
, unzip
, updateAt
, updateAtIndices
, zip
, zipWith
, zipWithA
, zip
, unzip

, any
, all

, foldM
, foldRecM

, unsafeIndex
) where

import Prelude
Expand All @@ -154,7 +147,7 @@ toUnfoldable xs = unfoldr f 0
where
len = length xs
f i
| i < len = Just (Tuple (unsafePartial (unsafeIndex xs i)) (i+1))
| i < len = Just (Tuple (unsafePartial (unsafeIndex xs i)) (i + 1))
| otherwise = Nothing

-- | Convert a `Foldable` structure into an `Array`.
Expand All @@ -178,7 +171,7 @@ foreign import fromFoldableImpl
-- | singleton 2 = [2]
-- | ```
singleton :: forall a. a -> Array a
singleton a = [a]
singleton a = [ a ]

-- | Create an array containing a range of integers, including both endpoints.
-- | ```purescript
Expand Down Expand Up @@ -217,13 +210,21 @@ many v = some v <|> pure []
-- Array size ------------------------------------------------------------------
--------------------------------------------------------------------------------

-- | Test whether an array is empty.
-- | Test whether an array is empty. Alias for `isEmpty`.
-- | ```purescript
-- | null [] = true
-- | null [1, 2] = false
-- | ```
null :: forall a. Array a -> Boolean
null xs = length xs == 0
null = isEmpty

-- | Test whether an array is empty.
-- | ```purescript
-- | isEmpty [] = true
-- | isEmpty [1, 2] = false
-- | ```
isEmpty :: forall a. Array a -> Boolean
isEmpty xs = length xs == 0

-- | Get the number of elements in an array.
-- | ```purescript
Expand All @@ -243,7 +244,7 @@ foreign import length :: forall a. Array a -> Int
-- |
-- | Note, the running time of this function is `O(n)`.
cons :: forall a. a -> Array a -> Array a
cons x xs = [x] <> xs
cons x xs = [ x ] <> xs

-- | An infix alias for `cons`.
-- |
Expand Down Expand Up @@ -283,8 +284,10 @@ insert = insertBy compare
-- |
insertBy :: forall a. (a -> a -> Ordering) -> a -> Array a -> Array a
insertBy cmp x ys =
let i = maybe 0 (_ + 1) (findLastIndex (\y -> cmp x y == GT) ys)
in unsafePartial (fromJust (insertAt i x ys))
let
i = maybe 0 (_ + 1) (findLastIndex (\y -> cmp x y == GT) ys)
in
unsafePartial (fromJust (insertAt i x ys))

--------------------------------------------------------------------------------
-- Non-indexed reads -----------------------------------------------------------
Expand Down Expand Up @@ -609,15 +612,16 @@ alterAt i f xs = maybe Nothing go (xs !! i)
-- | ```
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.new
_ <- STA.push (unsafeGetElem 0) out
ST.for 1 len \idx -> do
_ <- STA.push a out
void (STA.push (unsafeGetElem idx) out)
pure out
len
| len < 2 -> arr
| otherwise -> STA.run do
let unsafeGetElem idx = unsafePartial (unsafeIndex arr idx)
out <- STA.new
_ <- STA.push (unsafeGetElem 0) out
ST.for 1 len \idx -> do
_ <- STA.push a out
void (STA.push (unsafeGetElem idx) out)
pure out

-- | Reverse an array, creating a new array.
-- |
Expand Down Expand Up @@ -700,7 +704,7 @@ splitAt i xs = { before: slice 0 i xs, after: slice i (length xs) xs }
filterA :: forall a f. Applicative f => (a -> f Boolean) -> Array a -> f (Array a)
filterA p =
traverse (\x -> Tuple x <$> p x)
>>> map (mapMaybe (\(Tuple x b) -> if b then Just x else Nothing))
>>> map (mapMaybe (\(Tuple x b) -> if b then Just x else Nothing))

-- | Apply a function to each element in an array, keeping only the results
-- | which contain a value, creating a new array.
Expand Down Expand Up @@ -1044,16 +1048,16 @@ nubBy :: forall a. (a -> a -> Ordering) -> Array a -> Array a
nubBy comp xs = case head indexedAndSorted of
Nothing -> []
Just x -> map snd $ sortWith fst $ ST.run do
-- TODO: use NonEmptyArrays here to avoid partial functions
result <- STA.unsafeThaw $ singleton x
ST.foreach indexedAndSorted \pair@(Tuple _ x') -> do
lst <- snd <<< unsafePartial (fromJust <<< last) <$> STA.unsafeFreeze result
when (comp lst x' /= EQ) $ void $ STA.push pair result
STA.unsafeFreeze result
-- TODO: use NonEmptyArrays here to avoid partial functions
result <- STA.unsafeThaw $ singleton x
ST.foreach indexedAndSorted \pair@(Tuple _ x') -> do
lst <- snd <<< unsafePartial (fromJust <<< last) <$> STA.unsafeFreeze result
when (comp lst x' /= EQ) $ void $ STA.push pair result
STA.unsafeFreeze result
where
indexedAndSorted :: Array (Tuple Int a)
indexedAndSorted = sortBy (\x y -> comp (snd x) (snd y))
(mapWithIndex Tuple xs)
(mapWithIndex Tuple xs)

-- | Remove the duplicates from an array, where element equality is determined
-- | by the specified equivalence relation, creating a new array.
Expand Down Expand Up @@ -1120,7 +1124,7 @@ delete = deleteBy eq
-- | ```
-- |
deleteBy :: forall a. (a -> a -> Boolean) -> a -> Array a -> Array a
deleteBy _ _ [] = []
deleteBy _ _ [] = []
deleteBy eq x ys = maybe ys (\i -> unsafePartial $ fromJust (deleteAt i ys)) (findIndex (eq x) ys)

-- | Delete the first occurrence of each element in the second array from the
Expand Down
Loading