Skip to content

Commit ef91cc0

Browse files
mattpolzingallais
authored andcommitted
Add list difference to base Data.List module.
1 parent 08d9e7d commit ef91cc0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

libs/base/Data/List.idr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,31 @@ public export
206206
delete : Eq a => a -> List a -> List a
207207
delete = deleteBy (==)
208208

209+
||| Delete the first occurrence of each element from the second list in the first
210+
||| list where equality is determined by the predicate passed as the first argument.
211+
||| @ p A function that returns true when its two arguments should be considered equal.
212+
||| @ source The list to delete elements from.
213+
||| @ undesirables The list of elements to delete.
214+
public export
215+
deleteFirstsBy : (p : a -> a -> Bool) -> (source : List a) -> (undesirables : List a) -> List a
216+
deleteFirstsBy p = foldl (flip (deleteBy p))
217+
218+
infix 7 \\
219+
220+
||| The non-associative list-difference.
221+
||| A specialized form of @deleteFirstsBy@ where the predicate is equality under the @Eq@
222+
||| interface.
223+
||| Deletes the first occurrence of each element of the second list from the first list.
224+
|||
225+
||| In the following example, the result is `[2, 4]`.
226+
||| ```idris example
227+
||| [1, 2, 3, 4] // [1, 3]
228+
||| ```
229+
|||
230+
public export
231+
(\\) : (Eq a) => List a -> List a -> List a
232+
(\\) = foldl (flip delete)
233+
209234
||| The unionBy function returns the union of two lists by user-supplied equality predicate.
210235
public export
211236
unionBy : (a -> a -> Bool) -> List a -> List a -> List a

0 commit comments

Comments
 (0)