Re: How can I avoid needing this other function?

2010-12-06 Thread Alexandre Patry

On 10-12-07 01:37 AM, Alex Baranosky wrote:

Hello Alexandre,

This code makes my tests pass:

(defn distances [origin & locations]
  (map #(dist-in-miles origin %) locations))

(defn map-of-distances [origin & locations]
  (apply hash-map (interleave locations (apply distances origin 
locations

Glad it helped :)

Alex

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: How can I avoid needing this other function?

2010-12-06 Thread Alex Baranosky
Hello Alexandre,

This code makes my tests pass:

(defn distances [origin & locations]
  (map #(dist-in-miles origin %) locations))

(defn map-of-distances [origin & locations]
  (apply hash-map (interleave locations (apply distances origin
locations

Thanks for the inspiration!

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: How can I avoid needing this other function?

2010-12-06 Thread Alexandre Patry

On 10-12-07 12:44 AM, Alex Baranosky 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* ?
you could use (apply map-of-distances origin (take-nth 2 
locations-n-frequencies)), but I would also rewrite map-of-distances to 
something like:


(defn distances
[origin & locations]
(map (fn [l] [l (dist-in-miles origin l)]) locations))

(defn map-of-distances
[origin & locations]
(into {} (distances origin & locations))

Alex


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)))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient 
with your first post.

To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en 


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: How can I avoid needing this other function?

2010-12-06 Thread Mike Meyer
On Tue, 7 Dec 2010 00:44:52 -0500
Alex Baranosky  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.

   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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


How can I avoid needing this other function?

2010-12-06 Thread Alex Baranosky
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)))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en