Skip to content

Instantly share code, notes, and snippets.

@patmcnally
Created August 18, 2010 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patmcnally/535261 to your computer and use it in GitHub Desktop.
Save patmcnally/535261 to your computer and use it in GitHub Desktop.
(defn map-apply-sum-exclusive [f s]
"Maps each element in s to the sum of f applied that element and all other elements in s"
(map-indexed
(fn [i item]
(reduce (fn [sum x] (+ sum x))
0
(keep-indexed (fn [j other-item]
(when (not (= i j))
(f item other-item)))
s
)))
s))
(map-sum-exclusive (fn [x y] (+ x y)) '(1 2 3))
;; 1 => (+ (+ 1 2) (+ 1 3))
;; 2 => (+ (+ 2 1) (+ 2 3))
;; 3 => (+ (+ 3 1) (+ 3 2))
;; output is (7 8 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment