Created
August 18, 2010 16:06
-
-
Save patmcnally/535261 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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