Pergunta

Iam new to clojure and need some help to get a value out of a lazy sequence.

You can have a look at my full data structure here: http://pastebin.com/ynLJaLaP What I need is the content of the title:

{: _content AlbumTitel2}

I managed to get a list of all _content values:

(def albumtitle (map #(str (get % :title)) photosets))
(println albumtitle)

and the result is:

({:_content AlbumTitel2} {:_content test} {:_content AlbumTitel} {:_content album123}   {:_content speciale} {:_content neues B5 Album} {:_content Album Nr 2})

But how can I get the value of every :_content?

Any help would be appreciated!

Thanks!

Foi útil?

Solução

You could simply do this

(map (comp :_content :title) photosets)

Keywords work as functions, so the composition with comp will first retrieve the :title value of each photoset and then further retrieve the :_content value of that value.

Alternatively this could be written as

(map #(get-in % [:title :_content]) photosets)

Outras dicas

A semi alternative solution is to do

(->> data
 (map :title) 
 (map :_content))

This take advances of the fact that keywords are functions and the so called thread last macro. What it does is injecting the result of the first expression in as the last argument of the second etc..

The above code gets converted to

(map :_content (map :title data))

Clearly not as readable, and not easy to expand later either.

PS I asume something went wrong when the data was pasted to the web, because:

{: _content AlbumTitel2}

Is not Clojure syntax, this however is:

{:_content "AlbumTitel2"}  

No the whitespace after :, and "" around text. Just in case you might want to paste some Clojure some other time.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top