我有一份包含病媒清单的清单,其中涉及:
([1 2])([3 4] [5 6])([7 8])
我知道的是,与我合作并不理想。 我愿把这段话打上<代码>[1 2][3 4] [5 6] [7 8]>。
flatten doesn t work: it gives me (1 2 3 4 5 6 7 8)
.
我如何这样做? 图一
我有一份包含病媒清单的清单,其中涉及:
([1 2])([3 4] [5 6])([7 8])
我知道的是,与我合作并不理想。 我愿把这段话打上<代码>[1 2][3 4] [5 6] [7 8]>。
flatten doesn t work: it gives me (1 2 3 4 5 6 7 8)
.
我如何这样做? 图一
如果你只想平整一个等级,你可以使用<条码>。
(apply concat (([1 2]) ([3 4] [5 6]) ([7 8])))
=> ([1 2] [3 4] [5 6] [7 8])
To turn a list-of-lists into a single list containing the elements of every sub-list, you want apply concat
as nickik suggests.
然而,通常的解决办法是:不要提出从名单上开始的名单! 例如,请想一下,你有一份名为的植被名称-for
的功能,该功能具有象征意义,并列出了你可以称之为这一标志的所有冷却物品:
(get-names-for +) => (plus add cross junction)
如果你想获得某些标志清单的所有名字,你可能会尝试尝试。
(map get-names-for [+ /])
=> ((plus add cross junction) (slash divide stroke))
但这导致了你面临的问题。 您可以将其与<条码>应用于目录/代码”一起浏览,但最好使用<条码>,而不是<条码><>>>>。
(mapcat get-names-for [+ /])
=> (plus add cross junction slash divide stroke)
The code for flatten
is fairly short:
(defn flatten
[x]
(filter (complement sequential?)
(rest (tree-seq sequential? seq x))))
它使用<条码>树-seq,通过数据结构行走,并回归原子序列。 由于我们想到底线上的所有顺序,我们可以修改如下:
(defn almost-flatten
[x]
(filter #(and (sequential? %) (not-any? sequential? %))
(rest (tree-seq #(and (sequential? %) (some sequential? %)) seq x))))
因此,我们回去了没有顺序的所有顺序。
也可发现,在:
(defn flatten-1
"Flattens only the first level of a given sequence, e.g. [[1 2][3]] becomes
[1 2 3], but [[1 [2]] [3]] becomes [1 [2] 3]."
[seq]
(if (or (not (seqable? seq)) (nil? seq))
seq ; if seq is nil or not a sequence, don t do anything
(loop [acc [] [elt & others] seq]
(if (nil? elt) acc
(recur
(if (seqable? elt)
(apply conj acc elt) ; if elt is a sequence, add each element of elt
(conj acc elt)) ; if elt is not a sequence, add elt itself
others)))))
例:
(flatten-1 (([1 2]) ([3 4] [5 6]) ([7 8])))
=>[[1 2] [3 4] [5 6] [7 8]]
(flatten-1 (1 2 ([3 4] [5 6]) ([7 8])))
=>[1 2 [3 4] [5 6] [7 8]]
;whereas
(apply concat (1 2 ([3 4] [5 6]) ([7 8])))
=> java.lang.IllegalArgumentException:
Don t know how to create ISeq from: java.lang.Integer
Here s a function that will flatten down to the sequence level, regardless of uneven nesting:
(fn flt [s] (mapcat #(if (every? coll? %) (flt %) (list %)) s))
因此,如果你最初的顺序是:
(([1 2]) (([3 4]) ((([5 6])))) ([7 8]))
你也取得了同样的结果:
([1 2] [3 4] [5 6] [7 8])
I ve been playing a bit with Clojure and so far is fairly impressed, but one thing that I keep running into is wierd error messages from Clojure. This comes in two forms: Java errors, like null ...
I m using Clojure and I need to run a small simulation. I have a vector of length n (n is usually between 10 and 100) that holds values. On each simulation round (maybe 1000 rounds together), one of ...
I am trying to use clojure to implement a "plugin" for some vendor supplied software. Here is a little background on the vendor supplied software. It expects me to implement a particular interface ...
I m getting my feet wet with Clojure, and trying to get used to functional programming. I ve been translating various imperative functions from other languages into their Clojure equivalents -- and ...
OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...
All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...
I m looking to map a modestly-expensive function onto a large lazy seq in parallel. pmap is great but i m loosing to much to context switching. I think I need to increase the size of the chunk of work ...
Let s say I have a LazySeq of java.lang.Character like ( ! / \% 1 9 / . i \% $ i space ^@) How can I convert this to a String? I ve tried the obvious (String. my-char-seq) but ...