From 09aef4d51f54a13303b075ff6104bd9fca033c86 Mon Sep 17 00:00:00 2001 From: sigma-andex <77549848+sigma-andex@users.noreply.github.com> Date: Mon, 27 Jun 2022 10:38:04 +0100 Subject: [PATCH 1/5] Add isEmpty --- src/Data/Array.purs | 210 ++++++++++++++++++----------------- src/Data/Array/NonEmpty.purs | 26 +++-- src/Data/Array/ST.purs | 10 +- 3 files changed, 127 insertions(+), 119 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 6bc035a..01711bb 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -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 @@ -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`. @@ -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 @@ -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 @@ -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`. -- | @@ -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 ----------------------------------------------------------- @@ -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. -- | @@ -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. @@ -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. @@ -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 diff --git a/src/Data/Array/NonEmpty.purs b/src/Data/Array/NonEmpty.purs index 332cd69..2c35aad 100644 --- a/src/Data/Array/NonEmpty.purs +++ b/src/Data/Array/NonEmpty.purs @@ -10,13 +10,15 @@ module Data.Array.NonEmpty , toUnfoldable , toUnfoldable1 , singleton - , (..), range + , (..) + , range , replicate , some , length - , (:), cons + , (:) + , cons , cons' , snoc , snoc' @@ -31,7 +33,8 @@ module Data.Array.NonEmpty , uncons , unsnoc - , (!!), index + , (!!) + , index , elem , notElem , elemIndex @@ -94,7 +97,8 @@ module Data.Array.NonEmpty , delete , deleteBy - , (\\), difference + , (\\) + , difference , difference' , intersect , intersect' @@ -173,7 +177,7 @@ toArray :: forall a. NonEmptyArray a -> Array a toArray (NonEmptyArray xs) = xs toNonEmpty :: forall a. NonEmptyArray a -> NonEmpty Array a -toNonEmpty = uncons >>> \{head: x, tail: xs} -> x :| xs +toNonEmpty = uncons >>> \{ head: x, tail: xs } -> x :| xs fromFoldable :: forall f a. Foldable f => f a -> Maybe (NonEmptyArray a) fromFoldable = fromArray <<< A.fromFoldable @@ -189,7 +193,7 @@ toUnfoldable1 xs = unfoldr1 f 0 where len = length xs f i = Tuple (unsafePartial unsafeIndex xs i) $ - if i < (len - 1) then Just (i + 1) else Nothing + if i < (len - 1) then Just (i + 1) else Nothing singleton :: forall a. a -> NonEmptyArray a singleton = unsafeFromArray <<< A.singleton @@ -207,7 +211,8 @@ some :: forall f a . Alternative f => Lazy (f (Array a)) - => f a -> f (NonEmptyArray a) + => f a + -> f (NonEmptyArray a) some = unsafeFromArrayF <<< A.some length :: forall a. NonEmptyArray a -> Int @@ -323,7 +328,7 @@ partition :: forall a . (a -> Boolean) -> NonEmptyArray a - -> { yes :: Array a, no :: Array a} + -> { yes :: Array a, no :: Array a } partition f = adaptAny $ A.partition f filterA @@ -489,10 +494,10 @@ difference xs = adaptAny $ difference' xs difference' :: forall a. Eq a => NonEmptyArray a -> Array a -> Array a difference' xs = A.difference $ toArray xs -intersect :: forall a . Eq a => NonEmptyArray a -> NonEmptyArray a -> Array a +intersect :: forall a. Eq a => NonEmptyArray a -> NonEmptyArray a -> Array a intersect = intersectBy eq -intersect' :: forall a . Eq a => NonEmptyArray a -> Array a -> Array a +intersect' :: forall a. Eq a => NonEmptyArray a -> Array a -> Array a intersect' = intersectBy' eq intersectBy @@ -521,7 +526,6 @@ zipWith -> NonEmptyArray c zipWith f xs ys = unsafeFromArray $ A.zipWith f (toArray xs) (toArray ys) - zipWithA :: forall m a b c . Applicative m diff --git a/src/Data/Array/ST.purs b/src/Data/Array/ST.purs index 2817bee..a657906 100644 --- a/src/Data/Array/ST.purs +++ b/src/Data/Array/ST.purs @@ -60,8 +60,8 @@ run st = ST.run (st >>= unsafeFreeze) withArray :: forall h a b . (STArray h a -> ST h b) - -> Array a - -> ST h (Array a) + -> Array a + -> ST h (Array a) withArray f xs = do result <- thaw xs _ <- f result @@ -163,7 +163,7 @@ foreign import popImpl -- | Append an element to the end of a mutable array. Returns the new length of -- | the array. push :: forall h a. a -> STArray h a -> ST h Int -push a = pushAll [a] +push a = pushAll [ a ] -- | Append the values in an immutable array to the end of a mutable array. -- | Returns the new length of the mutable array. @@ -176,7 +176,7 @@ foreign import pushAll -- | Append an element to the front of a mutable array. Returns the new length of -- | the array. unshift :: forall h a. a -> STArray h a -> ST h Int -unshift a = unshiftAll [a] +unshift a = unshiftAll [ a ] -- | Append the values in an immutable array to the front of a mutable array. -- | Returns the new length of the mutable array. @@ -191,7 +191,7 @@ modify :: forall h a. Int -> (a -> a) -> STArray h a -> ST h Boolean modify i f xs = do entry <- peek i xs case entry of - Just x -> poke i (f x) xs + Just x -> poke i (f x) xs Nothing -> pure false -- | Remove and/or insert elements from/into a mutable array at the specified index. From 1222b7c3333c74cc6e04ebb9cbcdf01b4196c63e Mon Sep 17 00:00:00 2001 From: sigma-andex <77549848+sigma-andex@users.noreply.github.com> Date: Mon, 27 Jun 2022 10:40:17 +0100 Subject: [PATCH 2/5] Update Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 387f4a3..93486fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: - +- Add `isEmpty` (#222 by @sigma-andex) Bugfixes: Other improvements: From 8b8cd2fe947335abccf40fbda25fbc3b7a2a497b Mon Sep 17 00:00:00 2001 From: sigma-andex <77549848+sigma-andex@users.noreply.github.com> Date: Mon, 27 Jun 2022 14:20:44 +0100 Subject: [PATCH 3/5] Undo formatting --- src/Data/Array.purs | 199 ++++++++++++++++++----------------- src/Data/Array/NonEmpty.purs | 26 ++--- src/Data/Array/ST.purs | 10 +- 3 files changed, 118 insertions(+), 117 deletions(-) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 01711bb..1335ffe 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -28,99 +28,107 @@ -- | allowing you to STAI.iterate over an array and accumulate effects. -- | module Data.Array - ( (!!) - , (..) - , (:) - , (\\) - , all - , alterAt - , any - , catMaybes - , concat - , concatMap - , cons - , delete - , deleteAt - , deleteBy - , difference - , drop - , dropEnd - , dropWhile + ( fromFoldable + , toUnfoldable + , singleton + , (..), range + , replicate + , some + , many + + , null + , isEmpty + , length + + , (:), cons + , snoc + , insert + , insertBy + + , head + , last + , tail + , init + , uncons + , unsnoc + + , (!!), index , 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 - , insertBy - , intercalate - , intersect - , intersectBy - , intersperse - , isEmpty - , last - , length - , many - , mapMaybe - , mapWithIndex + , deleteAt + , updateAt + , updateAtIndices , modifyAt , modifyAtIndices - , notElem - , nub - , nubBy - , nubByEq - , nubEq - , null - , partition - , range - , replicate + , alterAt + + , intersperse , reverse + , concat + , concatMap + , filter + , partition + , splitAt + , filterA + , mapMaybe + , catMaybes + , mapWithIndex + , foldl + , foldr + , foldMap + , fold + , intercalate , scanl , scanr - , singleton - , slice - , snoc - , some + , sort , sortBy , sortWith - , span - , splitAt - , tail + , slice , take , takeEnd , takeWhile - , toUnfoldable - , uncons + , drop + , dropEnd + , dropWhile + , span + , group + , groupAll + , groupBy + , groupAllBy + + , nub + , nubEq + , nubBy + , nubByEq , union , unionBy - , unsafeIndex - , unsnoc - , unzip - , updateAt - , updateAtIndices - , zip + , delete + , deleteBy + + , (\\), difference + , intersect + , intersectBy + , zipWith , zipWithA + , zip + , unzip + + , any + , all + + , foldM + , foldRecM + + , unsafeIndex ) where import Prelude @@ -147,7 +155,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`. @@ -171,7 +179,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 @@ -244,7 +252,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`. -- | @@ -284,10 +292,8 @@ 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 ----------------------------------------------------------- @@ -612,16 +618,15 @@ 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. -- | @@ -704,7 +709,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. @@ -1048,16 +1053,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. @@ -1124,7 +1129,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 diff --git a/src/Data/Array/NonEmpty.purs b/src/Data/Array/NonEmpty.purs index 2c35aad..332cd69 100644 --- a/src/Data/Array/NonEmpty.purs +++ b/src/Data/Array/NonEmpty.purs @@ -10,15 +10,13 @@ module Data.Array.NonEmpty , toUnfoldable , toUnfoldable1 , singleton - , (..) - , range + , (..), range , replicate , some , length - , (:) - , cons + , (:), cons , cons' , snoc , snoc' @@ -33,8 +31,7 @@ module Data.Array.NonEmpty , uncons , unsnoc - , (!!) - , index + , (!!), index , elem , notElem , elemIndex @@ -97,8 +94,7 @@ module Data.Array.NonEmpty , delete , deleteBy - , (\\) - , difference + , (\\), difference , difference' , intersect , intersect' @@ -177,7 +173,7 @@ toArray :: forall a. NonEmptyArray a -> Array a toArray (NonEmptyArray xs) = xs toNonEmpty :: forall a. NonEmptyArray a -> NonEmpty Array a -toNonEmpty = uncons >>> \{ head: x, tail: xs } -> x :| xs +toNonEmpty = uncons >>> \{head: x, tail: xs} -> x :| xs fromFoldable :: forall f a. Foldable f => f a -> Maybe (NonEmptyArray a) fromFoldable = fromArray <<< A.fromFoldable @@ -193,7 +189,7 @@ toUnfoldable1 xs = unfoldr1 f 0 where len = length xs f i = Tuple (unsafePartial unsafeIndex xs i) $ - if i < (len - 1) then Just (i + 1) else Nothing + if i < (len - 1) then Just (i + 1) else Nothing singleton :: forall a. a -> NonEmptyArray a singleton = unsafeFromArray <<< A.singleton @@ -211,8 +207,7 @@ some :: forall f a . Alternative f => Lazy (f (Array a)) - => f a - -> f (NonEmptyArray a) + => f a -> f (NonEmptyArray a) some = unsafeFromArrayF <<< A.some length :: forall a. NonEmptyArray a -> Int @@ -328,7 +323,7 @@ partition :: forall a . (a -> Boolean) -> NonEmptyArray a - -> { yes :: Array a, no :: Array a } + -> { yes :: Array a, no :: Array a} partition f = adaptAny $ A.partition f filterA @@ -494,10 +489,10 @@ difference xs = adaptAny $ difference' xs difference' :: forall a. Eq a => NonEmptyArray a -> Array a -> Array a difference' xs = A.difference $ toArray xs -intersect :: forall a. Eq a => NonEmptyArray a -> NonEmptyArray a -> Array a +intersect :: forall a . Eq a => NonEmptyArray a -> NonEmptyArray a -> Array a intersect = intersectBy eq -intersect' :: forall a. Eq a => NonEmptyArray a -> Array a -> Array a +intersect' :: forall a . Eq a => NonEmptyArray a -> Array a -> Array a intersect' = intersectBy' eq intersectBy @@ -526,6 +521,7 @@ zipWith -> NonEmptyArray c zipWith f xs ys = unsafeFromArray $ A.zipWith f (toArray xs) (toArray ys) + zipWithA :: forall m a b c . Applicative m diff --git a/src/Data/Array/ST.purs b/src/Data/Array/ST.purs index a657906..2817bee 100644 --- a/src/Data/Array/ST.purs +++ b/src/Data/Array/ST.purs @@ -60,8 +60,8 @@ run st = ST.run (st >>= unsafeFreeze) withArray :: forall h a b . (STArray h a -> ST h b) - -> Array a - -> ST h (Array a) + -> Array a + -> ST h (Array a) withArray f xs = do result <- thaw xs _ <- f result @@ -163,7 +163,7 @@ foreign import popImpl -- | Append an element to the end of a mutable array. Returns the new length of -- | the array. push :: forall h a. a -> STArray h a -> ST h Int -push a = pushAll [ a ] +push a = pushAll [a] -- | Append the values in an immutable array to the end of a mutable array. -- | Returns the new length of the mutable array. @@ -176,7 +176,7 @@ foreign import pushAll -- | Append an element to the front of a mutable array. Returns the new length of -- | the array. unshift :: forall h a. a -> STArray h a -> ST h Int -unshift a = unshiftAll [ a ] +unshift a = unshiftAll [a] -- | Append the values in an immutable array to the front of a mutable array. -- | Returns the new length of the mutable array. @@ -191,7 +191,7 @@ modify :: forall h a. Int -> (a -> a) -> STArray h a -> ST h Boolean modify i f xs = do entry <- peek i xs case entry of - Just x -> poke i (f x) xs + Just x -> poke i (f x) xs Nothing -> pure false -- | Remove and/or insert elements from/into a mutable array at the specified index. From 8bc8be1ea299956bae17d4e7c6d5e8a3b4d93ee0 Mon Sep 17 00:00:00 2001 From: sigma-andex <77549848+sigma-andex@users.noreply.github.com> Date: Tue, 28 Jun 2022 10:26:21 +0100 Subject: [PATCH 4/5] Add deprecation warning for null --- CHANGELOG.md | 3 ++- src/Data/Array.purs | 3 ++- test/Test/Data/Array.purs | 10 +++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93486fd..4586319 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: -- Add `isEmpty` (#222 by @sigma-andex) +- Add `isEmpty` and add deprecation warning for `null` (#222 by @sigma-andex) + Bugfixes: Other improvements: diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 1335ffe..910352f 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -148,6 +148,7 @@ import Data.Traversable (sequence, traverse) import Data.Tuple (Tuple(..), fst, snd) import Data.Unfoldable (class Unfoldable, unfoldr) import Partial.Unsafe (unsafePartial) +import Prim.TypeError (class Warn, Text) -- | Convert an `Array` into an `Unfoldable` structure. toUnfoldable :: forall f. Unfoldable f => Array ~> f @@ -223,7 +224,7 @@ many v = some v <|> pure [] -- | null [] = true -- | null [1, 2] = false -- | ``` -null :: forall a. Array a -> Boolean +null :: forall a. Warn (Text "'null' is deprecated, use 'isEmpty'") => Array a -> Boolean null = isEmpty -- | Test whether an array is empty. diff --git a/test/Test/Data/Array.purs b/test/Test/Data/Array.purs index 9325c0f..eb8bfac 100644 --- a/test/Test/Data/Array.purs +++ b/test/Test/Data/Array.purs @@ -49,12 +49,12 @@ testArray = do -- some -- many - log "null should return false for non-empty arrays" - assert $ A.null [1] == false - assert $ A.null [1, 2, 3] == false + log "isEmpty should return false for non-empty arrays" + assert $ A.isEmpty [1] == false + assert $ A.isEmpty [1, 2, 3] == false - log "null should return true for an empty array" - assert $ A.null nil == true + log "isEmpty should return true for an empty array" + assert $ A.isEmpty nil == true log "length should return the number of items in an array" assert $ A.length nil == 0 From 4110e01b70a13d26eade01d0ce824195523df0e0 Mon Sep 17 00:00:00 2001 From: sigma-andex <77549848+sigma-andex@users.noreply.github.com> Date: Tue, 28 Jun 2022 16:26:09 +0100 Subject: [PATCH 5/5] Undo deprecation warning --- CHANGELOG.md | 2 +- src/Data/Array.purs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4586319..ed46d5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: -- Add `isEmpty` and add deprecation warning for `null` (#222 by @sigma-andex) +- Add `isEmpty` (#222 by @sigma-andex) Bugfixes: diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 910352f..1335ffe 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -148,7 +148,6 @@ import Data.Traversable (sequence, traverse) import Data.Tuple (Tuple(..), fst, snd) import Data.Unfoldable (class Unfoldable, unfoldr) import Partial.Unsafe (unsafePartial) -import Prim.TypeError (class Warn, Text) -- | Convert an `Array` into an `Unfoldable` structure. toUnfoldable :: forall f. Unfoldable f => Array ~> f @@ -224,7 +223,7 @@ many v = some v <|> pure [] -- | null [] = true -- | null [1, 2] = false -- | ``` -null :: forall a. Warn (Text "'null' is deprecated, use 'isEmpty'") => Array a -> Boolean +null :: forall a. Array a -> Boolean null = isEmpty -- | Test whether an array is empty.