你和我一样学代码sn-ps吗?这里有一些。
我们看一下map的文档。
user=> (doc map)
-------------------------
clojure.core/map
([f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls])
Returns a lazy sequence consisting of the result of applying f to the
set of first items of each coll, followed by applying f to the set
of second items in each coll, until any one of the colls is
exhausted. Any remaining items in other colls are ignored. Function
f should accept number-of-colls arguments.
nil
map 返回一个惰性序列(您应该已经阅读了@noahz 给出的引用)。要完全实现惰性序列(这通常不是一个好习惯,因为惰性序列可能是无限的,因此永远不会结束),您可以使用dorun 或doall。
user=> (doc dorun)
-------------------------
clojure.core/dorun
([coll] [n coll])
When lazy sequences are produced via functions that have side
effects, any effects other than those needed to produce the first
element in the seq do not occur until the seq is consumed. dorun can
be used to force any effects. Walks through the successive nexts of
the seq, does not retain the head and returns nil.
nil
user=> (doc doall)
-------------------------
clojure.core/doall
([coll] [n coll])
When lazy sequences are produced via functions that have side
effects, any effects other than those needed to produce the first
element in the seq do not occur until the seq is consumed. doall can
be used to force any effects. Walks through the successive nexts of
the seq, retains the head and returns it, thus causing the entire
seq to reside in memory at one time.
nil
尽管它们看起来很相似,但它们并不相似 - 请注意它们对待已实现序列的头部的方式不同。
有了这些知识,您可以使用doall 影响映射惰性序列的行为方式。
user=> (defn square [x]
#_=> (println (str "Processing: " x))
#_=> (* x x))
#'user/square
user=> (doall (map square '(1 2 3 4 5)))
Processing: 1
Processing: 2
Processing: 3
Processing: 4
Processing: 5
(1 4 9 16 25)
您可能已经注意到,我还更改了 square 函数的定义,因为您不需要在函数内部使用 do(它隐含在 defn 宏中)。
在Clojure Programming这本书里,有句你可能喜欢'(1 2 3 4 5)的例子:
"对于这种情况,大多数人只是简单地使用向量字面量,其中成员表达式
将始终被评估。”
与其复制相关部分来支持此声明,我更愿意推荐这本书,因为它值得花时间和金钱。