Skip to content

Commit

Permalink
feat: add Dynamic.List.find-index (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellerve authored Sep 17, 2021
1 parent e307654 commit 0efb982
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/List.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))
)
)

0 comments on commit 0efb982

Please sign in to comment.