On Tue, 7 Dec 2010 00:44:52 -0500
Alex Baranosky <[email protected]> wrote:
> Here is the code I'm working on. The first function is wanted. The second
> is not. (and the duplication is waiting to be factored out somehow...)
>
> Is there an idiomatic Clojure way to use map-of-distances on the "Line of
> Note" below, instead of map-of-distances* ?
>
> Thanks for all your help.
>
> (defn map-of-distances [origin & locations]
> (loop [dists {} locs locations]
> (if (seq locs)
> (let [[loc & more] locs
> dist (dist-in-miles origin loc)
> dists (assoc dists loc dist)]
> (recur dists more))
> dists)))
>
> (defn map-of-distances* [origin locations]
> (loop [dists {} locs locations]
> (if (seq locs)
> (let [[loc & more] locs
> dist (dist-in-miles origin loc)
> dists (assoc dists loc dist)]
> (recur dists more))
> dists)))
>
> (defn relative-distance
> "Gives distance * frequency.
> frequencies are in days out of 365"
> [origin & locations-n-frequencies]
> (let [loc-w-dists (map-of-distances* origin (take-nth 2
> locations-n-frequencies)) ;;;;; <-- Line of Note
> loc-w-freqs (apply hash-map locations-n-frequencies)]
> (multi-fmap (fn [d f] (* d f)) loc-w-dists loc-w-freqs)))
I haven't verified it, but based on the two interfaces, you want to
use apply again:
(map-of-distances* origin (take-nth 2 locations-n-frequencies))
can be written as:
(apply map-of-distances origin (take-nth 2 locations-n-frequencies))
FWIW, if you really needed them both, it'd be idiomatic to write one
in terms of the other:
(defn map-of-distances [origin & locations]
(map-of-distances* origin locations))
or (going the other way):
(defn map-of-distances* [origin locations]
(apply map-of-distances origin locations))
though this one you might not bother to write out, and just use the
apply inline as above.
<mike
--
Mike Meyer <[email protected]> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
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