English 中文(简体)
什么是最偏爱的 写这番话的正确方式?
原标题:What s the most idiomatic Clojure way to write this?

我写了这一职能(更不用说说明):

<代码>(第2页(清单1 2 3 4 5 6)

=> ((1 2) (2 3) (3 4) (4 5) (5 6)

(defn split [n xs] 
  (if (> (count xs) (dec n))
      (cons (take n xs) (split n (rest xs)))
       ()))

我的理解是,在Clojure,该清单并不是第一个类别数据结构。 撰写这一数据结构分析是否合理? 不管怎样,我的执行是否效率最高,如果没有,我如何提高其效率和(或)辅助性?

感谢!

最佳回答

您可以使用分治功能,

(partition 2 1 (list 1 2 3 4 5 6))
=> ((1 2) (2 3) (3 4) (4 5) (5 6))

无论先后顺序如何。


clojure.core/partition
([n coll] [n step coll] [n step pad coll])
  Returns a lazy sequence of lists of n items each, at offsets step
  apart. If step is not supplied, defaults to n, i.e. the partitions
  do not overlap. If a pad collection is supplied, use its elements as
  necessary to complete last partition upto n items. In case there are
  not enough padding elements, return a partition with less than n items.

问题回答

无需写出自己的执行情况。 Clojure提供partition,。 如果仅使用数字字面,也无需使用list:

 (partition 2  (1 2 3 4 5 6)) 

您可以从您的版本中得出一个 la的顺序:

  (defn split [n xs]
     (lazy-seq
         (let [m (take n xs)]
           (if (= n (count m))
             (cons m (split n (rest xs)))))))

(如果(......)和(......)(x) (dec n))情况不同,其原因是,其较有效率地将M元素从XS中挑选出来,而不是每次计算全部XS收集量(这是反常的,因为我们想走整个收集工作)。

想象一下它想把 every形形形形形形色色的元素计为:

  (take 10 (split 2 (range 100000000000)))

    => ((0 1) (1 2) (2 3)...)

我曾用克隆技术长达一个月,因此,我可能没有资格任命最偏爱的方法。

但是,你的实施工作很短,而且到了一点(认为这还重复了所建功能>,如前所述,

实施工作已经是相当可靠的数据结构,因为它使用,即操作,与所有标准数据结构合作:

(split 2 [1 2 3 4 5 6])
=> ((1 2) (2 3) (3 4) (4 5) (5 6))

(split 2 #{1 2 3 4 5 6})
=> ((1 2) (2 3) (3 4) (4 5) (5 6))

(split 2 {1 :a 2 :b 3 :c 4 :d})
=> (([1 :a] [2 :b]) ([2 :b] [3 :c]) ([3 :c] [4 :d]))

(split 2 "abcd")
=> ((a ) ( c) (c d))

使用平原再入侵的主要限制是,你受 st体大小的限制:

(split 2 (range 10000))
=> java.lang.StackOverflowError

因此,如果你期望投入量大大高于1k,那么它就更能使用回旋/回声,而后者没有使用 st子:

(defn split-loop [n coll]
  (loop [elms coll res [] ]
    (if (< (count elms) n)
      res
      (recur (next elms) (conj res (take n elms))))))




相关问题
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 ...