English 中文(简体)
2. 在顺序排列时特别考虑最后一项内容
原标题:Treating last element specially while iterating a sequence

我认为,我经常需要以特别的方式处理顺序的最后部分。 例如,采用<代码>interpose:

> (interpose ", " (range 4))

(0 ", " 1 ", " 2 ", " 3)

一种思维方式是:

  • Take the first element, add ", "
  • Take the second element, add ", "
  • Take the third element, add ", "
  • ...
  • Take the second to last element, add ", "
  • Take the last element and do nothing

我还发现,在利用种子建造米格布局时,我需要做一些特殊的工作。 例如,我要说的是:

+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+

每一数字都是部分(如纽伦)。 我可以这样做,对整个小组的“流量”作出“临时”限制,然后对每个构成部分增加以下限制:

  • "grow" - components 1, 2, 4, 5, 7, 8, 9
  • "grow, wrap" - components 3, 6

Note that the above can be said like this:

  • "grow" for each component except the last component in the row
  • "grow, wrap" for all other components except the last component in the last row

同一“特殊最后要素”主题再次出现。

因此有两个问题:

  • Does the above reasoning make sense? I.e. should I change the design fundamentally and think about this in a different way given the above sample problems and the general theme?
  • If not, is there an idiomatic and short way to do this?

是的,你可以发挥帮助作用和采取宏观措施,但我发现,这往往足以使我倾向于认为它应当是上述之一。 换言之,你是否属于同样类型的“问题”,以及你如何解决这些问题?

问题回答

这里的大多数讨论都是基于您的假设,即所需职能是相互的。 在某些方面,但大的差别是,为了优化权间用途,这种使用是一种 la。

zy的功能是从一个可以不为人知的顺序(即溪流)或甚至无限长度(即范围)中产生的。 只计算得出基本功能价值所需的要素(即假定)。 要求最后履行和依靠最后的职能,如计算,意味着必须完全照搬原先的顺序,才能最终完成或预期完成。 这正是1吨的警告。

然而,在这种情况下,根据大卫的答复,可能已经知道哪些要素,即最后一个要素的指数。 如果你不使用计数,就能够把这一功能作为参数使用,那么你就能够轻而易举地创造这种功能,甚至使之 la。

(def buttons [a  c d e f g h i])

(defn partition-nth-but
  [n b coll]
  (map
    (partial map second)                            ; remove index
    (partition-by
      #(and (integer? (/ (% 0) n)) (not= (% 0) b))  ; partition by index every nth but b
      (map-indexed (fn [i v] [(inc i) v]) coll))))  ; index coll offset 1 

=> (partition-nth-but 3 9 buttons)
((a ) (c) (d e) (f) (g h i))

(def grow str)
(def grow-and-wrap (comp clojure.string/upper-case grow))

=> (map apply (cycle [grow grow-and-wrap]) (partition-nth-but 3 9 buttons))
("ab" "C" "de" "F" "ghi")

But if we re applying a sequence of functions anyway, we could also just cycle through the right repetition of functions

(defn every-but-nth
  [n rf nf]
  (concat (repeat (dec n) rf) (repeat 1 nf)))

=> (apply concat
     (every-but-nth 3
       (every-but-nth 3 "grow" "grow-and")
     (repeat 3 "grow")))
("grow" "grow" "grow-and" "grow" "grow" "grow-and" "grow" "grow" "grow")

=> (map
     #(% %2)
     (apply concat (every-but-nth
                     3
                     (every-but-nth 3 grow grow-and-wrap)
                     (repeat 3 grow)))
     buttons)
("a" "b" "C" "d" "e" "F" "g" "h" "i")

你回想一下顺序的错误结束。 衣物中的所有顺序(事实上,一般的液晶仪)都是与一系列更多的要素相一致的因素。

定期处理是为了你为顺序的第一个要素做一些事情,然后将其余部分作为一个整体处理(可能重新计算)。 事实上,衣着有以下职能:<条码>第1条和<条码>,以鼓励这种思维方式。

正如“Rafal interpose”在一篇评论中指出的那样,“Rafal interpose”可能成为......。

  • take the first element
  • take the second element, precede with ","
  • take the third element, precede with "," ...

如果你能够(几乎)以下列方式执行:

(defn interpose [s]
    (cons (first s) (map #(str "," %) (rest s))))

the actual implementation in core.clj builds on the interleave function, which is more complex because it handles two sequences, but still builds on the first + rest idiom.

许多(多数?)算法可以采用这种思维方式。

由于你知道投入的大小,即n,只是为了第一个n-1要素而做的事情。 这是你最初的interpose的最好解决办法。

In your grow example, grow n-1 (or 8) elements wraping at 3 and 6. Then tack n (9) on the end.

然而,你可能并不总是知道你投入的规模。 如果是这样,那么通过从第一个要素中删除,只能根据其余要素运作,也可以取得同样的结果。 更一般的情况是这样,而且可能更接近于鼓励你在使用衣物时思考。





相关问题
How do you estimate an agile project up front? [closed]

When working on fixed price software development projects, I frequently find myself having to estimate the total number of hours a project will take after the price is set, but before the work is ...

Efficient way of calling set of functions in Python

I have a set of functions: functions=set(...) All the functions need one parameter x. What is the most efficient way in python of doing something similar to: for function in functions: function(...

How do I modify an array while I am iterating over it in Ruby?

I m just learning Ruby so apologies if this is too newbie for around here, but I can t work this out from the pickaxe book (probably just not reading carefully enough). Anyway, if I have an array like ...

More efficient method for this calculation?

a = 218500000000 s = 6 f = 2 k = 49 d = k + f + s r = a i = 0 while (r >= d): r = r - d #print ( r = ,r) i = i+1 #print ( i = ,i) print (i) I think it does what I expect it to, but ...

Java: JGraphT: Iterate through nodes

I m trying to iterate through all nodes, so I can print them out for graphviz. What is the best way to do that using the JGraphT library? public static void main(String[] args) { UndirectedGraph&...

Iterating over arrays in haskell

My problem is that I need to iterate over array and calculate some value depend on every element. I was looking for some fold-like function for arrays, but standard library seems to be very useless ...

热门标签