1

I have an array of result ({:a one}). I want a result which returns one when I select or give :a.

I used vals,get-in functions but since the map is inside a list they are not working. Can you please help me how do I do that.

I need to return value for a given key.

Also,

What if the data is of the form

({:a “Favorite fruit", :measurements [{:a “fav game?", :b "x",
:name “foot ball", :max 7.0, :min 0.0, :id “12345"}], :name “Frequency”})

and I want to select :a "fav game?" :name "foot ball" and :min 0.0 ?

Thank you

0

4 Answers 4

4

Try this:

clj.core=> (def data [ {:a 1} {:b 2} ] )
#'clj.core/data
clj.core=> (:a (first data))
1
clj.core=> (mapv :a data)
[1 nil]
Sign up to request clarification or add additional context in comments.

4 Comments

Not sure if your sequence has more than one map like data above. Post additional example if I'm missing something.
What if the data is of the form ({:a “Favorite fruit", :measurements [{:a “fav game?", :b "x", :name “foot ball", :max 7.0, :min 0.0, :id “12345"}], :name “Frequency”}) and I want to select -:a "fav :name "football" and :min 0.0 ?
No, The data exactly looks like this ({:a “Favorite fruit", :measurements [{:a “fav game?", :b "x", :name “foot ball", :max 7.0, :min 0.0, :id “12345"}], :name “Frequency”})
And what should the output be?
3

List comprehension

List comprehension using for with any quantity of elements in the list:

(for [x data :let [m (:measurements x)]] (map #(let [{:keys [a name min]} %] [a name min]) m))

Details:

  • for [x data for every element in data
  • :let [m (:measurements x)]] set m as a symbol for :measurements
  • (map #(let [{:keys [a name min]} %] [a name min]) m) for every m get the keys a name min and output a vector [a name min].

Output:

((["fav name?" "foot ball" 0.0]))

Or one result

Directly using apply:

(let [{:keys [a name min]} (apply (comp first :measurements) data)] [a name min])

Details:

  • (apply (comp first :measurements) data) apply composition, first get :measurements then the first elem.
  • let [{:keys [a name min]} get the keys a name min from the map and output a vector [a name min].

Output:

["fav name?" "foot ball" 0.0]

2 Comments

Thank you so much... This was really helpful and easily understood for starters like me.
If you have lots of these nested operations, a library like Specter or functional lenses might be helpful too. They make nested Clojure code much easier to maintain.
2

If you have a vector of results (usually more idiomatic than a list), you can use get in, but with numbers as keys. e.g.

(get-in [{:a "one"}] [0 :a])  ;=> "one"

3 Comments

I actually used (map (juxt :a)(into [] ({:a "one"})))
Can you help with the next continued question which I just added
I'm not sure why you're using juxt in that case; do you want a nested vector?
0

To add to @Marcs answer (and for some extra Clojure help) there are a few ways to pull values out using destructuring too.

Starting with the example you gave

(def foobar
  {:a "Favorite fruit", :measurements [{:a "fav game?", :b "x",
   :name "foot ball", :max 7.0, :min 0.0, :id "12345"}], :name "Frequency"})

We can do associative destructuring since Clojure maps are associative data structures

;; associative destructuring
(let [{:keys [a]} foobar]
  a)

;; also associative destructuring in the function params this time
((fn [{:keys [a]}]
   a)
 foobar)

To a get single value out of a map wrapped in an array we can use a combination of sequential and associative destructuring

(let [[{:keys [a]}] '({:a 'one})]
  a)

For the last part you asked about with getting :a :name :min we can use another destructuring combo

(let [{a :a [{min :min}] :measurements name :name} foobar]
  [a name min])

That clojure.org article I linked to is pretty awesome for explaining more details!

Comments

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.