I'm assuming the StackOverflow link you refer to is
http://stackoverflow.com/questions/3259825/trouble-with-lazy-convolution-fn-in-clojure.
I would think about the problem this way: to compute the value at index i in
the output list, you multiply together each pair of values in the input lists
whose indices add up to i, then add them all together. So I would first write a
small helper function to return a list of such index pairs for a given output
index, then use it with map to compute each output value in one go.
(defn convolve-indices [i max-m max-n]
"Lists all index pairs adding up to i, where the first index is less than
max-m and the second is less than max-n."
(filter #(< (first %) max-m)
(filter #(< (second %) max-n)
(map #(vector % (- i %))
(range (inc i))))))
(defn convolve-1 [i ms ns]
"Returns the ith value of the convolution of ms with ns."
(reduce +
(map #(* (ms (first %)) (ns (second %)))
(convolve-indices i (count ms) (count ns)))))
(defn convolve [ms ns]
"Convolves ms with ns."
(map #(convolve-1 % ms ns)
(range (dec (+ (count ms) (count ns))))))
I would expect this to be much slower than the imperative approach, due to all
the temporary objects and the lack of type hints. There should be a much less
wasteful way to write convolve-indices, for instance. On the plus side, you may
be able to use pmap to parallelize the work with little effort.
--
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