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!

4
  • 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. Commented Aug 16, 2013 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)) Commented Aug 16, 2013 at 12:25
  • 1
    also #(str %1) can be replaced with str Commented Aug 16, 2013 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 ...)). Commented Aug 18, 2013 at 8:55

1 Answer 1

3
(map #(vector :p (link-to (str "/album?photosetid=" %1) %2)) ids names)
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.