1

I am trying to get a link for each photoset. It should look like this:

[:p (link-to (str "/album?photosetid="photosetid) photoset-name)

In the following code I get a map of all photoset ids and names:

(def ids (map #(str "/album?photosetid=" %1) photoset-id))
(def names (map #(str  %1) photoset-name))

After that i try to create the links:

  (loop [x (count ids)]
    (when (> x 0)
      [:p (link-to (nth ids x "") name) (nth names x "")]
      (recur (- x 1))
      )
    )

The problem is that I don't get any output.

Thanks for any help!

Dave Liepmann
  • 1,555
  • 1
  • 18
  • 22
wes
  • 33
  • 3
  • Just FYI, the reason this returns nil, is because unlike map or reduce or for, there is no implicit collection of results with loop. While map as lgrapenthin shows is more concise and idiomatic, you would have gotten a proper result if you used a collector argument to loop, adding each element to a result at each step, and an if instead of when, returning the collector if x = -1. – noisesmith Aug 16 '13 at 12:01
  • `(loop [x (dec (count ids)) result []] (if (> x -1) (recur (dec x) (conj result [:p (link-to (nth ids x "") name) (nth names x "")])) result))` – noisesmith Aug 16 '13 at 12:25
  • 1
    also `#(str %1)` can be replaced with `str` – noisesmith Aug 16 '13 at 12:31
  • 1
    @noisesmith That's almost right, but hiccup distinguishes between vectors and seqs, and he needs a seq as output. So he would have to wrap it like `(seq (loop ...))`. – amalloy Aug 18 '13 at 08:55

1 Answers1

3
(map #(vector :p (link-to (str "/album?photosetid=" %1) %2)) ids names)
Leon Grapenthin
  • 9,246
  • 24
  • 37
  • That looks good! The only problem is the id. I need the photoset-id out of "ids". I use it to retrieve the photos for each photoset. – wes Aug 16 '13 at 10:25
  • The difference between this and the implied behavior of the original example is that the original would continue producing empty strings for names if names was shorter than ids. – noisesmith Aug 16 '13 at 12:28
  • I don't know what link-to does, but I suppose it should get a valid name for every ID. Also I would expect id/name pairs to come from the same source (a hash-map or collection). I think if you want empty strings here, it would be more idiomatic to make sure that they are in names first. – Leon Grapenthin Aug 16 '13 at 12:45