Problem with lazy seqs in macros
Hi! A while back, I was implementing some datastructures using functions and macros. While implementing hashmaps, I stumbled upon a problem: (ns hmap) (defmacro hmap [& kvs] "Returns an immutable hashmap. Keys must be compile-time constants." (if (even? (count kvs)) (let [tups (partition 2 kvs) keys (mapv first tups) kvs+ (concat kvs [::keys keys])] `(fn [k#] (case k# ~@kvs+))) (throw (Exception. "hmap takes an EVEN number of args" (comment ;; USAGE (def m (hmap :a 1 :b 2)) ;; => #'hmap/m (m ::keys) ;; => [:a :b] (m :a) ;; => 1 (m :c) ;; => IllegalArgumentException No matching clause: :c ) *(m ::keys)* returns *nil* if *(mapv first tups)* is changed to *(map first tups)*. I had originally used *map*, but seeing that it was not behaving as expected, my friend Divyanshu suggested converting the *keys* binding into a vector, which made it work. Some questions - * Is this because of the lazy seq not being realized in the context of the macro? * Is this correct/expected behavior? Thanks! -- 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: Problem with lazy seqs in macros
On Monday, 5 February 2018 09:22:58 UTC, Divyansh Prakash wrote: You see what's happening with macroexpand (macroexpand '(hmap :a 1 :b 2)) (fn* ([k__4009__auto__] (clojure.core/case k__4009__auto__ :a 1 :b 2 :user/keys [:a :b]))) versus (fn* ([k__4146__auto__] (clojure.core/case k__4146__auto__ :a 1 :b 2 :user/keys (:a :b So the value of :user/keys becomes a function call (:a :b) which is nil. -- 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: Problem with lazy seqs in macros
@Peter Great catch, thanks! I was scratching my head over this. -- 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: Problem with lazy seqs in macros
You could (I think) simplify to: (defmacro hmap [& kvs] "Returns an immutable hashmap. Keys must be compile-time constants." (if (even? (count kvs)) (let [keys (into [] (take-nth 2) kvs)] `(fn [k#] (case k# ~@kvs ::keys ~keys))) (throw (Exception. "hmap takes an EVEN number of args" -- 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: Problem with lazy seqs in macros
that (:a :b) *really* looks like a seq -- 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: Problem with lazy seqs in macros
And yes, take-nth is what I wanted! -- 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.
What does the ref *loaded-libs* do?
It is there to support the “:reload” and “:reload-all” features of “require” and to help separate the concern of loading libs from the concern of tracking namespaces. --Steve -- 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: What does the ref *loaded-libs* do?
It is there to support the “:reload” and “:reload-all” features of “require” and to help separate the concern of loading libs from the concern of tracking namespaces. --Steve > On Jan 29, 2018, at 6:36 PM, Raymond Huang <12ay.hu...@gmail.com> wrote: > > I was poking around `tools.namespace` and I found it interesting that the > implementation of `remove-lib` is: > > ```(defn remove-lib > "Remove lib's namespace and remove lib from the set of loaded libs." > [lib] > (remove-ns lib) > (dosync (alter @#'clojure.core/*loaded-libs* disj lib))) > ``` > > > I’m wondering if someone can enlighten me to explain why `*loaded-libs*` > needs to exist, as opposed to checking `clojure.lang.Namespace` directly? > > This is a question carried over from slack: > https://clojurians.slack.com/archives/C03S1KBA2/p151721185721 > -- > 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.
Help please: New to clojure development
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 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.
[JOB] Clojure Developer for smart lighting IoT platform
*Clojure Developer* Work with highly scaling back-end smart lighting IoT platform. The group is a startup purchased by a large corporation (6-12+ contract through HCL), so you get the best of both worlds. Must Have: -- 1 to 3 years hands, production on experience in Clojure or another Lisp based language Strong functional programming background Minimum 1 year experience in Java Hands on experience with leiningen and git Hands on experience in API Experience in Scrum and CI Nice to have: -- +1 year Experience in Neo4j and Cassandra +1 year Experience in Nodejs and javascript Experience in microservices and swagger and Postman Contact krmishras at hcl.com (978-990-6636) or kevin.degraaf at hcl.com -- 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: Help please: New to clojure development
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 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 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. > -- 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 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.