Nico
Nico

Reputation: 1081

clojure: howto build a string from two sequences?

I struggling with a problem for hours now... I want to build a link with values from two sequences.

(doseq [item photoset-name] (prn item ))
(doseq [item-name photoset-id] (prn item-name ))

output:

"AlbumTitel2"
"test"
"AlbumTitel"
"album123"
"speciale"
"neues B5 Album"
"Album Nr 2"
"72157632764328569"
"72157632769231530"
"72157632769092584"
"72157632768156156"
"72157632762740183"
"72157632724688181"
"72157632760876608"

Now I want to build a link like this (for every id/name):

<a href="http://example.com?id=72157632764328569">AlbumTitel2</a>

And it should be a sequence or map... anything I can iterate though.

Does anyone have an idea how to archive this?

Thanks!

Upvotes: 1

Views: 134

Answers (2)

Leon Grapenthin
Leon Grapenthin

Reputation: 9266

(map #(str "<a href=\"http://example.com?id="
           %1
           "\">"
           %2
           "</a>") photoset-ids photoset-names)

Upvotes: 2

paul
paul

Reputation: 1705

You could try using a map in one of two ways (I don't have Clojure handy at the moment so I can't verify):

(map #(prn "<a href=\"" %1 "\">" %2 "</a>") item item-name)

or

(doseq [pair (map vector item item-name)]
  (prn "<a href=\"" (first pair) "\">" (second pair) "</a>"))

EDIT: be careful using map; it's lazy so if you don't use the result returned by it then it might not actually be run!

This might also work:

(doseq [[url title] (map vector item item-name)]
  (prn "<a href=\"" url "\">" title "</a>"))

Combining items from multiple lists into pairs in a single list is commonly referred to as "zipping". The (map vector ...) I found in this answer

Upvotes: 1

Related Questions