Skip to content

Instantly share code, notes, and snippets.

@m0smith
Created July 19, 2012 20:00
Show Gist options
  • Save m0smith/3146395 to your computer and use it in GitHub Desktop.
Save m0smith/3146395 to your computer and use it in GitHub Desktop.
A simple function to add vectors. Zero pads vectors to the length of the longest
(defn vec+
([] nil)
([v1] v1)
([v1 v2]
(let [c1 (count v1)
c2 (count v2)
seq (if (< c1 c2)
(map + (concat v1 (repeat 0)) v2)
(map + (concat v2 (repeat 0)) v1))]
(vec seq)))
([ v1 v2 & vn]
(reduce vec+ (vec+ v1 v2) vn)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment