English 中文(简体)
Clojure:半宽度
原标题:Clojure: Semi-Flattening a nested Sequence

我有一份包含病媒清单的清单,其中涉及:

([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))))

因此,我们回去了没有顺序的所有顺序。

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])




相关问题
How to improve Clojures error messages

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 ...

clojure rmi classpath problem

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 ...

Help translating this Java codeblock to Clojure?

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 ...

Is functional Clojure or imperative Groovy more readable?

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 ...

taking java method names as function arg in clojure

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 ...

how to efficiently apply a medium-weight function in parallel

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 ...

热门标签