本质上,我想要一个像这样工作的函数:
user=> (pos 'c '(a b c d e f g) =)
2
user=> (pos 'z '(a b c d e f g) =)
nil
我想出了这个:
(defn pos
"Gets position of first object in a sequence that satisfies match"
[object sequence match]
(loop [aseq sequence position 0]
(cond (match object (first aseq)) position
(empty? aseq) nil
:else (recur (rest aseq) (inc position)))))
所以我的问题是,是否有一些内置函数可以让我们这样做,或者是否有更好、更实用/Clojure-ish 的方式来编写pos
函数?