用于序列comprehnsion的clojure一次添加两个元素

Mar*_*tus 2 clojure

理解:

(for [i (range 5])] i)
Run Code Online (Sandbox Code Playgroud)

...收益率:(0 1 2 3 4)

是否有一种惯用的方法来获得(0 0 1 1 2 4 3 9 4 16)(即数字和它们的方块)主要用于理解?

我到目前为止找到的唯一方法是:

(apply concat (for [i (range 5)] (list i (* i i))))
Run Code Online (Sandbox Code Playgroud)

Bey*_*mor 5

实际上,for如果考虑为每个值应用每个函数(标识和正方形),则仅使用它非常简单.

(for [i (range 5),             ; for every value
      f [identity #(* % %)]]   ; for every function
  (f i))                       ; apply the function to the value

 ; => (0 0 1 1 2 4 3 9 4 16)
Run Code Online (Sandbox Code Playgroud)

  • 等价地,`(对于[i(范围5),x [i(*ii)]] x)`. (2认同)