Skip to content

Commit 4442096

Browse files
committed
Merge pull request #7 from purescript/deletions
Add delete, deleteBy, (\\)
2 parents 9772fb2 + 89c1a19 commit 4442096

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
(!!) :: forall a. [a] -> Prim.Number -> Maybe a
2525

26+
(\\) :: forall a. (Eq a) => [a] -> [a] -> [a]
27+
2628
append :: forall a. [a] -> [a] -> [a]
2729

2830
catMaybes :: forall a. [Maybe a] -> [a]
@@ -31,8 +33,12 @@
3133

3234
concatMap :: forall a b. (a -> [b]) -> [a] -> [b]
3335

36+
delete :: forall a. (Eq a) => a -> [a] -> [a]
37+
3438
deleteAt :: forall a. Prim.Number -> Prim.Number -> [a] -> [a]
3539

40+
deleteBy :: forall a. (a -> a -> Prim.Boolean) -> a -> [a] -> [a]
41+
3642
drop :: forall a. Prim.Number -> [a] -> [a]
3743

3844
elemIndex :: forall a. (Eq a) => a -> [a] -> Prim.Number

src/Data/Array.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module Data.Array
2323
, insertAt
2424
, deleteAt
2525
, updateAt
26+
, deleteBy
27+
, delete
28+
, (\\)
2629
, concatMap
2730
, filter
2831
, range
@@ -193,6 +196,24 @@ foreign import updateAt
193196
\ };\
194197
\}":: forall a. Number -> a -> [a] -> [a]
195198

199+
deleteBy :: forall a. (a -> a -> Boolean) -> a -> [a] -> [a]
200+
deleteBy _ _ [] = []
201+
deleteBy eq x ys = case findIndex (eq x) ys of
202+
i | i < 0 -> ys
203+
i -> deleteAt i 1 ys
204+
205+
delete :: forall a. (Eq a) => a -> [a] -> [a]
206+
delete = deleteBy (==)
207+
208+
infix 5 \\
209+
210+
(\\) :: forall a. (Eq a) => [a] -> [a] -> [a]
211+
(\\) xs ys = go xs ys
212+
where
213+
go [] ys = ys
214+
go _ [] = []
215+
go (x:xs) ys = go xs (delete x ys)
216+
196217
foreign import concatMap
197218
"function concatMap (f) {\
198219
\ return function (arr) {\

0 commit comments

Comments
 (0)