Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to handle this sequence transformation in Clojure?

I'm new to Clojure, and I've been translating some data manipulation work I did recently as an aid to learning. I've got a function translation that works fine, and is shorter, but feels much less readable. Can anyone suggest a more readable and/or more idiomatic way to handle this?

In Python:

def createDifferenceVector(v,startWithZero=True):
   deltas = []
   for i in range(len(v)):
       if i == 0:
           if startWithZero:
               deltas.append(0.0)
           else:
               deltas.append(v[0])
       else:
           deltas.append(v[i] - v[i-1])
   return deltas

My attempt at a Clojure translation:

(defn create-diff-vector [v start-zero]
  (let [ext-v (if start-zero
                (cons (first v) v)
                (cons 0 v))]
    (for [i (range 1 (count ext-v))] 
      (- (nth ext-v i) (nth ext-v (- i 1))))))

It could be that it's less readable just because of my inexperience with Clojure, but in particular, the trick of prepending an element to the input vector feels to me like it obscures the intention. All the solutions I tried which didn't use the prepending trick were much longer and uglier.

Many sequence transformations are incredibly elegant in Clojure, but the ones that I find challenging so far are ones like this one, which a) lend themselves to manipulation by index rather than by element, and/or b) require special handling for certain elements.

Thanks for any suggestions.

like image 439
eggsyntax Avatar asked Nov 17 '11 12:11

eggsyntax


2 Answers

Idiomatic Clojure tends to manipulate the sequences as a whole, rather than individual elements. You could define create-diff-vector in English as:

The result is a vector consisting of:

  • a zero or the first element of the input, depending of whether start-zero is true or false, respectively; followed by
  • differences between the input sequence without the first element and the input sequence without the last element.

The second part can be illustrated thusly: for the input (31 41 59 26 53), we have

  input without the first element:   (41 59  26 53)
- input without the last element:    (31 41  59 26)
===================================================
  result:                            (10 18 -33 27)

Which, translated to Clojure, becomes remarkably concise:

(defn diff-vector [v start-zero?]
  (into [(if start-zero? 0 (first v))]
    (map - (rest v) v))))

A few points to note:

  • A question mark at the end of start-zero? serves as a hint that a boolean is expected here.
  • The code exploits the fact that mapping a function over sequences of different lengths terminates upon the end of the shortest sequence.
like image 123
Daniel Janus Avatar answered Nov 04 '22 18:11

Daniel Janus


This implementation would be more idiomatic:

(defn create-diff-vector [v start-with-zero?]
  (let [v (cons (if start-with-zero? (first v) 0) v)]
       (map - (rest v) v)))

I first prepend either the first value of the vector or 0 to the input vector. Then I use map to subtract the vector from itself, shifted by one position.

like image 1
Christian Berg Avatar answered Nov 04 '22 17:11

Christian Berg