Fork me on GitHub
#clojure-uk
<
2019-07-24
>
guy06:07:02

Morning!

thomas07:07:31

Moin moin...

👍 4
3Jane08:07:31

guys, what's the preferred method of running a side-effecting function over a (potentially lazy) collection of items?

3Jane08:07:33

(mapv, doall, dorun, ...?)

guy08:07:17

I usually use mapv because i’m lazy, also less code writing.

danielneal08:07:44

I guess mapv is good if you need the return value

guy08:07:59

Yeah thats a good point

thomas08:07:18

I have been known just to do (println ...) in the past... 😇

alexlynham09:07:44

doall or dorun depending on whether you want the return value

alexlynham09:07:54

doseq if you want a binding

djtango10:07:57

run! if you don't want the result

djtango10:07:34

whoops bronsa beat me to it

danielneal08:07:03

I use doseq personally

👍 4
danielneal08:07:09

has for like semantics

bronsa08:07:02

run! is also good

👍 8
Ben Hammond10:07:02

I love it when I am shown a new (to me) clojure built-in

danielneal08:07:40

gotta_go_fast

😄 12
💥 4
rickmoynihan09:07:37

What bronsa said. run! is specifically meant for this… doseq, dorun, mapv and others have slightly different purposes. run! is specifically meant for where you have a side effecting function that you want to apply to each thing in the collection.

bronsa09:07:29

to be fair doseq/`dorun` are also specifically meant for side-effecting over collections

bronsa09:07:22

you could argue that doall isn't specifically about that, but the docstring also explicitely mentions side-effecty stuff

rickmoynihan09:07:12

>to be fair doseq/`dorun` are also specifically meant for side-effecting over collections Absolutely. My point is really that run! is really meant for when you have a side effecting procedure of the right shape already; which is (nearly) what @lady3janepl asked for i.e. (run! println coll) is arguably better than (doseq [v coll] (println v)). Though if you’re not breaking the side effect out into a procedure you can use doseq… there’s lots of subtlety around picking the right one here — most of which is arguably also splitting hairs. Though you might prefer doseq if you needed to apply other args too.

djtango10:07:59

run! came after doseq and dorun I guess doseq breaks composability because it is a macro which may have been one of the usecases for adding run!

danm11:07:01

We ended up using run! all over the place. Much tidier than doseq/`dorun` most of the time, as long as you don't care about anything apart from the side effect.

rickmoynihan11:07:49

yeah run! was added in 1.7

rickmoynihan11:07:21

doseq etc were pre 1.0

3Jane13:07:37

Nice, thanks. I didn’t know about run!

3Jane13:07:32

I’ve got a list of elements being converted and then saved as separate files, so I don’t need the result, and I don’t care about their order