Thanks for the quick response James. Yes, you hit the nail on the head with
me not understanding map, explanation you provided helps. I like the second
option better. I have tried both options and now I am getting
"illegalargumentexception, key must be an integer"
When I call
(state-desc3 input-file)
When I do this, it works find of course cause it's only one
(state-desc3 {:st_abbr "AZ", :first name "John", :lastname "Smith"})
1) maybe an index problem which I don't understand yet and researching
2) I need to figure out how to call function for testing
On Feb 5, 2018 9:57 PM, "James Reeves" <[email protected]> wrote:
First:
(#(map :st_abbrev input-file))
Is equivalent to:
(map :st_abbrev input-file)
Because your putting the form in an anonymous function, then immediately
calling it. This is equivalent to just evaluating the form.
Next, I think you're confused as to how `map` handles multiple arguments.
If you call:
(map f [1 2 3] [4 5 6])
Then it will return:
[(f 1 4) (f 2 5) (f 3 6)]
The two collections are lined up, then passed as arguments to the function.
If you want to put this into one function, then you don't need an inner
map. You instead want:
(defn state-desc2 [input-file]
(let [desc2 (:st_abbrev input-file)]
(case desc2
"AZ" (assoc input-file :state "Arizona")
"FL" (assoc input-file :state "Florida")
"OH" (assoc input-file :state "Ohio")
"default")))
You could also write it as:
(def state-names
{"AZ" "Arizona", "FL" "Florida", "OH" "Ohio"})
(defn state-desc3 [input-file]
(assoc input-file :state (state-names (:st_abbrev input-file))))
On 6 February 2018 at 01:22, Nadeen Kurz <[email protected]> wrote:
> Can someone help me with the following please: I am new to clojure and i
> haven't developed in 4 years, previous was mainframe. See questions in blue
>
> ; Task is to add full state name based on st_abbr
>
>
> (def input-file [{:st_abbrev "AZ", :firstname "john", :lastname "smith"}
> {:st_abbrev "FL", :firstname "roy", :lastname
> "wills"}
> {:st_abbrev "OH", :firstname "jay", :lastname
> "louis"}])
>
> *Question 1: How do I make these between the lines into one Defn?*
> -----------------------------------------------------------------------
> (def get-state
> (#(map :st_abbrev input-file)))
>
> #'user/get-state
> ("AZ" "FL" "OH")
>
> (defn state-desc [get-state input-file]
> (let [desc get-state]
> (case desc
> "AZ" (assoc input-file :state "Arizona")
> "FL" (assoc input-file :state "Florida")
> "OH" (assoc input-file :state "Ohio")
> "default"
> )))
>
> (map state-desc get-state input-file)
>
> #'user/state-desc
> ({:st_abbrev "AZ", :firstname "john", :lastname "smith", :state
> "Arizona"}
> {:st_abbrev "FL", :firstname "roy", :lastname "wills", :state "Florida"}
> {:st_abbrev "OH", :firstname "ja y", :lastname "louis", :state "Ohio"})
> ------------------------------------------------------------
> ---------------
>
> Question 2: I tried to combine in one defn, but it's not working, any help
> would be appreciated
>
> (defn state-desc2 [input-file]
> (let [desc2 (#(map :st_abbrev input-file))]
> (case desc2
> "AZ" (assoc input-file :state "Arizona")
> "FL" (assoc input-file :state "Florida")
> "OH" (assoc input-file :state "Ohio")
> "default"
> )))
>
> (map state-desc2 input-file)
>
> #'user/state-desc2
> ("default" "default" "default")
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
James Reeves
booleanknot.com
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with
your first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.