"into" 函数有什么用?

What is "into" function for?

我有一个定义如下的函数:

(defn delete-rule [rules rule]
  (om/transact! rules
    (fn [rules] (into [] (remove #(= rule %) rules)))))

这里into的目的是什么? 这不会产生与上述结果完全相同的结果吗:

(defn delete-rule [rules rule]
  (om/transact! rules
    (fn [rules] (remove #(= rule %) rules))))

来自 remove 的文档:

Returns a lazy sequence of the items in coll for which (pred item) returns false. pred must be free of side-effects. Returns a transducer when no collection is provided.

来自 into 的文档:

Returns a new coll consisting of to-coll with all of the items of from-coll conjoined. A transducer may be supplied.

所以不同之处在于,带有 into returns 的删除规则版本是一个非惰性向量,而没有 into returns 的版本是一个惰性序列。

你的函数的细节是 om 不支持列表(或惰性序列)作为游标,只支持映射和向量,这就是必须将 remove 的输出转换为向量的原因。

希望对您有所帮助。