Clojure;从大小不等的列表中选择所有第n个元素,n = 1、2 [英] Clojure; select all nth element from list of lists with unequal size, for n = 1, 2,
本文介绍了Clojure;从大小不等的列表中选择所有第n个元素,n = 1、2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个函数,例如,
I'd like to have a function, such that,
(f '([1 4 7] [2 5 9] [3 6]))
愿意给予
([1 2 3] [4 5 6] [7 9])
我尝试了
(apply map vector '([1 4 7] [2 5 9] [3 6]))
只会产生:
([1 2 3] [4 5 6])
我很难描述我的要求,即我很难找到一个现成的解决方案。
I find it hard to describe my requirements that it's difficult for me to search for a ready solution.
请帮助我改善描述或提供解决方案的指针。
Please help me either to improve my description, or pointer to a solution.
预先感谢!
推荐答案
我会解决更多问题一般问题,这意味着您将来可能会重复使用该功能。我会更改地图
,以便它一直越过最小的地图。
I'd solve a more general problem which means you might reuse that function in the future. I'd change map
so that it keeps going past the smallest map.
(defn map-all
"Like map but if given multiple collections will call the function f
with as many arguments as there are elements still left."
([f] (map f))
([f coll] (map f coll))
([f c1 & colls]
(let [step (fn step [cs]
(lazy-seq
(let [ss (keep seq cs)]
(when (seq ss)
(cons (map first ss)
(step (map rest ss)))))))]
(map #(apply f %) (step (conj colls c1))))))
(apply map-all vector '([1 4 7] [2 5 9] [3 6]))
(apply map-all vector '([1 false 7] [nil 5 9] [3 6] [8]))
请注意,与许多其他解决方案相反,即使任何序列包含 nil
或 false
。
这篇关于Clojure;从大小不等的列表中选择所有第n个元素,n = 1、2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文