映射到执行副作用的向量 [英] Mapping over a vector performing side-effects

查看:69
本文介绍了映射到执行副作用的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Clojure中遍历一个线的向量。基本上,它看起来像:

I am attempting to iterate over a vector of "lines" in Clojure. Essentially, it looks like:

[{:start {:x 1 :y 3 :z 4}, :end {:x 3 :y 7 :z 0}}, ...]

函数将这些行打印到一个新行ala:

I would like to apply a function that prints each of these "lines" onto a new line, ala:

(map #(println %) vector-of-lines)

但是看起来没有调用该函数。我应该不在这个实例中使用map函数?

but that doesn't appear to call the function. Should I not be using the "map" function in this instance?

推荐答案

(dorun (map println vector-of-lines))

dorun 强制延迟序列的评估,而且丢弃序列中每个项目的单独结果。这是完美的序列,纯粹的副作用,这正是你想要的在这里。

dorun forces the evaluation of the lazy sequence, but also discards the individual results of each of item in the sequence. This is perfect for sequences that are purely for side-effects which is exactly what you want here.

这篇关于映射到执行副作用的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆