本文介绍了doseq和Clojure之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

doseq和Clojure之间有什么区别?

What's the difference between doseq and for in Clojure? What are some examples of when you would choose to use one over the other?

推荐答案

不同之处在于 for 构建一个延迟序列并返回它,而 doseq 用于执行副作用并返回nil。

The difference is that for builds a lazy sequence and returns it while doseq is for executing side-effects and returns nil.

user=> (for [x [1 2 3]] (+ x 5))
(6 7 8)
user=> (doseq [x [1 2 3]] (+ x 5))
nil
user=> (doseq [x [1 2 3]] (println x))
1
2
3
nil

如果要基于其他序列构建新序列,请使用for。如果你想基于一些序列的元素做副作用(打印,写入数据库,发射核弹头等),请使用doseq。

If you want to build a new sequence based on other sequences, use for. If you want to do side-effects (printing, writing to a database, launching a nuclear warhead, etc) based on elements from some sequences, use doseq.

这篇关于doseq和Clojure之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:28