feat: add Dynamic.List.find-index (#1316)

This commit is contained in:
Veit Heller 2021-09-17 05:58:39 +02:00 committed by GitHub
parent e307654521
commit 0efb9825f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -347,11 +347,26 @@ elements is uneven, the trailing element will be discarded.")
(defndynamic set-nth [l n elem]
(List.update-nth l n (fn [_] elem)))
(doc find "finds the first element in the list `l` that matches `pred`.")
(doc find
"finds the first element in the list `l` that matches `pred`.
Returns `nil` on failure")
(defndynamic find [l pred]
(cond
(empty? l) '()
(pred (car l)) (car l)
(List.find (cdr l) pred)))
(doc find-index "like [`find](#find), but returns the index instead.
Returns `nil` on failure")
(defndynamic find-index [l pred]
(cond
(empty? l) '()
(pred (car l)) 0
(let [res (List.find-index (cdr l) pred)]
(if (nil? res)
res
(inc res)))))
)
)