Clojure 如何使用索引变量迭代两个列表?
我试图把一系列的句子转换成一系列的承诺 句子定义:Clojure 如何使用索引变量迭代两个列表?,clojure,Clojure,我试图把一系列的句子转换成一系列的承诺 句子定义: (def sentences (repeatedly promise)) (future (doseq [sentence (map deref sentences)] (println sentence))) (doall (map deliver (nth sentences n) (parsedSentences))) n = 1 parsedSentences = ["This is a sentence." "Is t
(def sentences (repeatedly promise))
(future
(doseq [sentence (map deref sentences)]
(println sentence)))
(doall (map deliver (nth sentences n) (parsedSentences)))
n = 1
parsedSentences = ["This is a sentence." "Is this a sentence?"]
交付:
(def sentences (repeatedly promise))
(future
(doseq [sentence (map deref sentences)]
(println sentence)))
(doall (map deliver (nth sentences n) (parsedSentences)))
n = 1
parsedSentences = ["This is a sentence." "Is this a sentence?"]
示例:
(def sentences (repeatedly promise))
(future
(doseq [sentence (map deref sentences)]
(println sentence)))
(doall (map deliver (nth sentences n) (parsedSentences)))
n = 1
parsedSentences = ["This is a sentence." "Is this a sentence?"]
我想把每个解析的条目传递给句子中相应的承诺。因为我对clojure很陌生,所以我找不到一种方法来统计n
我正在寻找一种方法来做类似的事情
deliver(nth sentences 1)("This is a sentence")
deliver(nth sentences 2)("Is this a sentence?")
...
deliver(nth sentences n)( sentence n)
因此,基本上,我正在寻找一种Clojure方法,使用索引变量或其他东西在两个列表上进行迭代。映射索引可能就是您想要的。它所使用的函数有两个参数,因此类似于
(fn[idx,el](dosomething idx el)
。我相信您也可以一次映射多个集合,每组元素将作为元组传递,如(c1e1,c2e1,…,cNe1)
(c表示集合,e表示该集合中的元素顺序)
你自己已经写了一半的答案,除了你写了
(第n个句子n)
(这显然是不可能的,因为你知道n
不在范围之内),而不仅仅是句子
map
函数通常只使用一个序列参数进行调用,但当使用多个序列参数进行调用时,它在某些其他语言中充当zipWith
,返回[(f x0 y0)(f x1 y1)(f x2 y2)]
。您甚至可以使用零序参数调用map
,然后它就是一个转换器
所以,你只需要写
(map deliver sentences parsedSentences)
你为什么不把这两个集合都映射到地图上呢?(映射句子解析句)?@leetwinski谢谢,我没有意识到这是可能的,所以请查看混合泳库中的地图索引
和索引
:@leetwinski,这是一个答案!你是对的,在问了我的问题后,我确实这样做了,但在我的特殊情况下,这不起作用,因为我使用了一个无休止的列表,其中索引必须行。最后我使用了以下代码。(定义句子(重复承诺))(定义句子计数器0)(doseq[句子解析句子](交付(第n个句子句子计数器)句子)(定义句子计数器(包括句子计数器)))也许你可以在你的答案中输入我的代码,然后我会将你的答案标记为正确答案。