title: In clojure, what's the efficient way to convolve a vector by a kernel?
body: <p>I came up with this: </p>
  
  <pre><code>(def kernel [0 1 1 2 3 3 0 0 0 0 0 0])
  (def data [1 5 7 4 8 3 9 5 6 3 2 1 1 7 4 9 3 2 1 8 6 4])
  
  (defn capped+ [a b c] (let [s (+ a b)] (if (&gt; s c) c s)))
  
  (defn *+ [a b]
    (if (&gt; (count a) (count b))  
      (reduce + (map-indexed (fn _ [i x] (* (a i) (b i))) b))
      (reduce + (map-indexed (fn _ [i x] (* (a i) (b i))) a))))
  
  (defn slide*i [d k] 
   (let [ki (into [] (reverse k)) kl (count k) dl (count d)]
     (map-indexed 
        (fn [idx item] 
          (/ (*+ ki (subvec d idx (capped+ idx kl dl))) 
             (reduce + ki))) 
        d)))
  
  (def s (slide*i data kernel))
  </code></pre>
  
  <p>It's not the most elegant code, but it works fine.
  I actually want to use it to smooth some very spiky! data.</p>
  
  <p>Any suggestions to make this more beautiful or more efficient or more accurate (personally I don't care about the tail being inaccurate because in my case I never use it) are welcomed.  </p>