Re: Returning multiple values
I bet you it doesn't :-). It may be rendered like that but I doubt very much the server is returning the form ({..} {..} {..}) as that is interpreted as a function call. It will almost certainly be returning a sequence of the form '({..} {..} {..}) which isn't a functional call but is a sequence of maps. Let's break it down a bit. The arguments to map are (map fn sequence) so it returns the result of executing fn with each element in the sequence. Rather than (map (-> class1 :people) [:name :age])) you want something like (map [:name :age] (-> class1 :people)). That still isn't going to work though as it will execute [:name :age] against each element in the value of the key :people in the class1 map. (remember that keywords (e.g. :people) are functions that look themselves up in the argument to the keyword which is assumed to be associative. So (:people class1) will be replaced with the value of calling :people on class1 which will retrieve the value of :people in class1. Getting closer though. You want to extract the :name and :age from each map in (:people class1). A perfect function for that is juxt which returns a vector filled with the results of calling each function passed to juxt. So (juxt :name :age) returns a function, which when called with a parameter will return a vector of the result of calling :name against the parameter and :age against the parameter. Remember that keywords are functions. A longer, but maybe more transparent way would be (map (fn [m] [(:name m) (:age m)]) (:people class1)). Finally, map returns a sequence but you want it to be a vector (why?) so you can use either (into [] sequence), (vector sequence) or mapv which is identical to map except it returns a sequence. So, all together: ( -- returns a sequence by applying mapv -- a function which returns [(:name m) (:age m)] which is applied to (juxt :name :age) -- each item in the sequence that is the value of :people in the map class1 (:people class1) ) HTH. On 30 November 2016 at 11:54, 'Rickesh Bedia' via Clojure wrote: > Because the information is coming from a table I don't know if I can change > it to look like that. > > The information from the table looks like {:people ({:name "John" :age 25} > {:name "Harry" :age 23} {:name "Peter" :age 24})} > > I was wondering if you could apply (into [] (map (-> class1 :people) [:name > :age])) to each of the {:name "Peter" :age "24"}, {:name "John" :age "25"}, > {:name "Harry" :age "23"} and then put that into a vector so the final > vector is in the form > [["John" "25"] > ["Harry" "23"] > ["Peter" "24"]] > > On Wednesday, 30 November 2016 10:37:04 UTC, Colin Yates wrote: >> >> (def class1 {:people '({:name "John" :age "25"} {:name "Harry" :age >> "23"} {:name "Peter" :age "24"})}) or (def class1 {:people [{:name >> "John" :age "25"} {:name "Harry" :age "23"} {:name "Peter" :age >> "24"}]}) is probably what you want. >> >> (mapv (juxt :name :age) (:people class1)) on either of those will give >> you your result. >> >> On 30 November 2016 at 10:34, Colin Yates wrote: >> > Ah, I just realised people is _not_ a sequence of maps but the result >> > of calling '{:name "John" :age "25"}' passing in the other two maps as >> > arguments. You probably want a literal literal '({:name "John" :age >> > "25"}.) or a vector [{:name "John" :age "25"}...] >> > >> > On 30 November 2016 at 10:29, Colin Yates wrote: >> >> (mapv (juxt :name :age) (:people class1)) should work >> >> >> >> On 30 November 2016 at 10:27, 'Rickesh Bedia' via Clojure >> >> wrote: >> >>> I have a definition: >> >>> (def class1 {:people ({:name "John" :age "25"} >> >>> {:name "Harry" :age "23"} >> >>> {:name "Peter" :age "24"})}) >> >>> >> >>> The result I want is a vector that looks like >> >>> [["John" "25"] >> >>> ["Harry" "23"] >> >>> ["Peter" "24"]] >> >>> >> >>> If I call (map (-> class1 :people) [:name :age]) >> >>> then I get the result ("Peter" "24"). Why do I only get the values >> >>> from the >> >>> last hashmap and not the others? >> >>> >> >>> If I then call (into [] (map (-> class1 :people) [:name :age])) >> >>> then I get the result ["Peter" "24"] >> >>> >> >>> What I need to do is run the 'into' function again on the other two >> >>> hashmaps >> >>> and then put the 3 results into a vector but I don't know how to do >> >>> this. >> >>> >> >>> Any help would be much appreciated >> >>> >> >>> -- >> >>> You received this message because you are subscribed to the Google >> >>> Groups "Clojure" group. >> >>> To post to this group, send email to clo...@googlegroups.com >> >>> Note that posts from new members are moderated - please be patient >> >>> with your >> >>> first post. >> >>> To unsubscribe from this group, send email to >> >>> clojure+u...@googlegroups.com >> >>> For more options, visit this group at >> >>> http://groups.google.com/group/clojure?hl=en >> >>> --- >> >>> You received this message because you are subscri
Re: Returning multiple values
Because the information is coming from a table I don't know if I can change it to look like that. The information from the table looks like {:people ({:name "John" :age 25} {:name "Harry" :age 23} {:name "Peter" :age 24})} I was wondering if you could apply (into [] (map (-> class1 :people) [:name :age])) to each of the {:name "Peter" :age "24"}, {:name "John" :age "25"}, {:name "Harry" :age "23"} and then put that into a vector so the final vector is in the form [["John" "25"] ["Harry" "23"] ["Peter" "24"]] On Wednesday, 30 November 2016 10:37:04 UTC, Colin Yates wrote: > > (def class1 {:people '({:name "John" :age "25"} {:name "Harry" :age > "23"} {:name "Peter" :age "24"})}) or (def class1 {:people [{:name > "John" :age "25"} {:name "Harry" :age "23"} {:name "Peter" :age > "24"}]}) is probably what you want. > > (mapv (juxt :name :age) (:people class1)) on either of those will give > you your result. > > On 30 November 2016 at 10:34, Colin Yates > wrote: > > Ah, I just realised people is _not_ a sequence of maps but the result > > of calling '{:name "John" :age "25"}' passing in the other two maps as > > arguments. You probably want a literal literal '({:name "John" :age > > "25"}.) or a vector [{:name "John" :age "25"}...] > > > > On 30 November 2016 at 10:29, Colin Yates > wrote: > >> (mapv (juxt :name :age) (:people class1)) should work > >> > >> On 30 November 2016 at 10:27, 'Rickesh Bedia' via Clojure > >> > wrote: > >>> I have a definition: > >>> (def class1 {:people ({:name "John" :age "25"} > >>> {:name "Harry" :age "23"} > >>> {:name "Peter" :age "24"})}) > >>> > >>> The result I want is a vector that looks like > >>> [["John" "25"] > >>> ["Harry" "23"] > >>> ["Peter" "24"]] > >>> > >>> If I call (map (-> class1 :people) [:name :age]) > >>> then I get the result ("Peter" "24"). Why do I only get the values > from the > >>> last hashmap and not the others? > >>> > >>> If I then call (into [] (map (-> class1 :people) [:name :age])) > >>> then I get the result ["Peter" "24"] > >>> > >>> What I need to do is run the 'into' function again on the other two > hashmaps > >>> and then put the 3 results into a vector but I don't know how to do > this. > >>> > >>> Any help would be much appreciated > >>> > >>> -- > >>> You received this message because you are subscribed to the Google > >>> Groups "Clojure" group. > >>> To post to this group, send email to clo...@googlegroups.com > > >>> Note that posts from new members are moderated - please be patient > with your > >>> first post. > >>> To unsubscribe from this group, send email to > >>> clojure+u...@googlegroups.com > >>> For more options, visit this group at > >>> http://groups.google.com/group/clojure?hl=en > >>> --- > >>> You received this message because you are subscribed to the Google > Groups > >>> "Clojure" group. > >>> To unsubscribe from this group and stop receiving emails from it, send > an > >>> email to clojure+u...@googlegroups.com . > >>> For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Returning multiple values
(def class1 {:people '({:name "John" :age "25"} {:name "Harry" :age "23"} {:name "Peter" :age "24"})}) or (def class1 {:people [{:name "John" :age "25"} {:name "Harry" :age "23"} {:name "Peter" :age "24"}]}) is probably what you want. (mapv (juxt :name :age) (:people class1)) on either of those will give you your result. On 30 November 2016 at 10:34, Colin Yates wrote: > Ah, I just realised people is _not_ a sequence of maps but the result > of calling '{:name "John" :age "25"}' passing in the other two maps as > arguments. You probably want a literal literal '({:name "John" :age > "25"}.) or a vector [{:name "John" :age "25"}...] > > On 30 November 2016 at 10:29, Colin Yates wrote: >> (mapv (juxt :name :age) (:people class1)) should work >> >> On 30 November 2016 at 10:27, 'Rickesh Bedia' via Clojure >> wrote: >>> I have a definition: >>> (def class1 {:people ({:name "John" :age "25"} >>> {:name "Harry" :age "23"} >>> {:name "Peter" :age "24"})}) >>> >>> The result I want is a vector that looks like >>> [["John" "25"] >>> ["Harry" "23"] >>> ["Peter" "24"]] >>> >>> If I call (map (-> class1 :people) [:name :age]) >>> then I get the result ("Peter" "24"). Why do I only get the values from the >>> last hashmap and not the others? >>> >>> If I then call (into [] (map (-> class1 :people) [:name :age])) >>> then I get the result ["Peter" "24"] >>> >>> What I need to do is run the 'into' function again on the other two hashmaps >>> and then put the 3 results into a vector but I don't know how to do this. >>> >>> Any help would be much appreciated >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To post to this group, send email to clojure@googlegroups.com >>> Note that posts from new members are moderated - please be patient with your >>> first post. >>> To unsubscribe from this group, send email to >>> clojure+unsubscr...@googlegroups.com >>> For more options, visit this group at >>> http://groups.google.com/group/clojure?hl=en >>> --- >>> You received this message because you are subscribed to the Google Groups >>> "Clojure" group. >>> To unsubscribe from this group and stop receiving emails from it, send an >>> email to clojure+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Returning multiple values
Ah, I just realised people is _not_ a sequence of maps but the result of calling '{:name "John" :age "25"}' passing in the other two maps as arguments. You probably want a literal literal '({:name "John" :age "25"}.) or a vector [{:name "John" :age "25"}...] On 30 November 2016 at 10:29, Colin Yates wrote: > (mapv (juxt :name :age) (:people class1)) should work > > On 30 November 2016 at 10:27, 'Rickesh Bedia' via Clojure > wrote: >> I have a definition: >> (def class1 {:people ({:name "John" :age "25"} >> {:name "Harry" :age "23"} >> {:name "Peter" :age "24"})}) >> >> The result I want is a vector that looks like >> [["John" "25"] >> ["Harry" "23"] >> ["Peter" "24"]] >> >> If I call (map (-> class1 :people) [:name :age]) >> then I get the result ("Peter" "24"). Why do I only get the values from the >> last hashmap and not the others? >> >> If I then call (into [] (map (-> class1 :people) [:name :age])) >> then I get the result ["Peter" "24"] >> >> What I need to do is run the 'into' function again on the other two hashmaps >> and then put the 3 results into a vector but I don't know how to do this. >> >> Any help would be much appreciated >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with your >> first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Returning multiple values
(mapv (juxt :name :age) (:people class1)) should work On 30 November 2016 at 10:27, 'Rickesh Bedia' via Clojure wrote: > I have a definition: > (def class1 {:people ({:name "John" :age "25"} > {:name "Harry" :age "23"} > {:name "Peter" :age "24"})}) > > The result I want is a vector that looks like > [["John" "25"] > ["Harry" "23"] > ["Peter" "24"]] > > If I call (map (-> class1 :people) [:name :age]) > then I get the result ("Peter" "24"). Why do I only get the values from the > last hashmap and not the others? > > If I then call (into [] (map (-> class1 :people) [:name :age])) > then I get the result ["Peter" "24"] > > What I need to do is run the 'into' function again on the other two hashmaps > and then put the 3 results into a vector but I don't know how to do this. > > Any help would be much appreciated > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Returning multiple values
I have a definition: (def class1 {:people ({:name "John" :age "25"} {:name "Harry" :age "23"} {:name "Peter" :age "24"})}) The result I want is a vector that looks like [["John" "25"] ["Harry" "23"] ["Peter" "24"]] If I call (map (-> class1 :people) [:name :age]) then I get the result ("Peter" "24"). Why do I only get the values from the last hashmap and not the others? If I then call (into [] (map (-> class1 :people) [:name :age])) then I get the result ["Peter" "24"] What I need to do is run the 'into' function again on the other two hashmaps and then put the 3 results into a vector but I don't know how to do this. Any help would be much appreciated -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.