I'm trying to use Stuart Sierra's implementation of cells. I want to
sum the values of a large number of cells. Rather than linearly
summing all the values, I would like to create a tree of cells whose
root contains the sum. I added the function commute-cells:
(defn commute-cells [f cells]
(let [len1 (count cells)
len2 (/ len1 2)]
(cond (= 1 len1)
(first cells)
(= 2 len1)
(cell (f (cv (first cells))
(cv (second cells))))
true
(cell (f (cv (commute-cells f (take len2 cells)))
(cv (commute-cells f (drop len2 cells))))))))
This seems to work for small numbers of cells, but never finishes for
larger values.
user> (reduce + (range 10))
45
user> (def cells (map #(cell %) (range 10)))
#'user/cells
user> (def sum (commute-cells + cells))
45
user> (reduce + (range 1000))
499500
user> (def cells (map #(cell %) (range 1000)))
#'user/cells
user> (def sum (commute-cells + cells))
; Evaluation aborted.
Do I need to make this a macro? If so, could someone help me out, and
make a macro for the linear version as well?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---