gpt4 book ai didi

clojure - 如何找到向量中项目的索引?

转载 作者:行者123 更新时间:2023-12-02 06:46:23 26 4
gpt4 key购买 nike

有什么想法 ?????? 应该是什么吗?有内置的吗?完成这项任务的最佳方法是什么?

(def v ["one" "two" "three" "two"])

(defn find-thing [ thing vectr ]
(????))

(find-thing "two" v) ; ? maybe 1, maybe '(1,3), actually probably a lazy-seq

最佳答案

内置:

user> (def v ["one" "two" "three" "two"])
#'user/v
user> (.indexOf v "two")
1
user> (.indexOf v "foo")
-1

如果您想要所有匹配项的索引的惰性序列:

user> (map-indexed vector v)
([0 "one"] [1 "two"] [2 "three"] [3 "two"])
user> (filter #(= "two" (second %)) *1)
([1 "two"] [3 "two"])
user> (map first *1)
(1 3)
user> (map first
(filter #(= (second %) "two")
(map-indexed vector v)))
(1 3)

关于clojure - 如何找到向量中项目的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4830900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com