Fork me on GitHub
#beginners
<
2016-08-17
>
gnejs19:08:31

I’m not sure if I should be using reduce or if there’s a better function in Clojure for this: Say I have [1 3 6 10] and want to calculate a new seq where each element is the result of a function of two adjacent elements in the input -> (foo - [1 3 6 10]) ;;=> [2 3 4].

gnejs19:08:09

Is there a foo function that would do that? I guess I would need to push a vector through a reduce otherwise?

gnejs19:08:19

Hmm… maybe map and partition-all is a solution?

gnejs19:08:05

Actually - (map #(apply - %) (partition 2 1 [1 3 6 10])) seems to be what I need 🙂 (Except that my sample output is obviously wrong - it should be [-2 -3 -4]).

jswart19:08:46

@gnejs: You are correct. If you want to use reduce you could. Anything you can solve recursively you can solve with reduce.

guy19:08:31

did you solve it @gnejs ?

dpsutton19:08:51

is there a mapc version of map that is explicitly for side effects? Or do you just have to (doall (map .... )) your mappings?

guy19:08:36

oh i see i reread what you said! nvm

gnejs19:08:29

@jswart: yeah, I started implementing it using reduce, but thought that there must be a better way 🙂

jswart19:08:38

in this case definitely

jswart19:08:47

but if you wanted any state

jswart19:08:51

than reduce is the way to go

gnejs19:08:59

and my “reduce function” is way more complicated than -.

madstap23:08:47

@dpsutton run! is the side effect version of map