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

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

问题描述

我试图在 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)

但这似乎没有调用该函数.在这种情况下,我不应该使用地图"功能吗?

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天全站免登陆