Moving average from Java to Clojure
I just wrote a moving average class in Java: https://github.com/CecilWesterhof/JavaExamples/blob/master/MovingAverageQueue.java I was just wondering: what is the best way to translate this to Clojure? At the moment Clojure does not have a queue. Should I just use the Java calls, or is there a better way? -- Cecil Westerhof -- 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: Moving average from Java to Clojure
Hi Cecil Clojure has a clojure.lang.PersistentQueue to work with queues. I don't know why it doesn't have a reader macro, but works fine and returns you a Clojure collection you can handle with cons and peek. You can start with an empty queue with clojure.lang.PersistentQueue/EMPTY or insert your items in the constructor. I wrote some material about it in Portuguese if you have any interest. Regards Plinio > On 20/07/2014, at 08:48, Cecil Westerhof wrote: > > I just wrote a moving average class in Java: > > https://github.com/CecilWesterhof/JavaExamples/blob/master/MovingAverageQueue.java > > I was just wondering: what is the best way to translate this to Clojure? > > At the moment Clojure does not have a queue. Should I just use the Java > calls, or is there a better way? > > -- > Cecil Westerhof > -- > 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: Moving average from Java to Clojure
There actually is a queue implementation. Here is a way to use it for your problem: (defn make-moving-average-queue [n] (atom {:lengthn :current-total 0.0 :old-values(clojure.lang.PersistentQueue/EMPTY)})) (defn update-moving-average-queue [old-queue next-value] (let [current-total (+ (:current-total old-queue) next-value) old-values (conj (:old-values old-queue) next-value)] (if (> (count old-values) (:length old-queue)) (let [current-total (- current-total (first old-values)) old-values (pop old-values)] (assoc old-queue :current-total current-total :old-values old-values)) (assoc old-queue :current-total current-total :old-values old-values (defn moving-average [old-queue next-value] (let [new-queue (swap! old-queue update-moving-average-queue next-value)] (/ (:current-total new-queue) (count (:old-values new-queue) (def queue-06 (make-moving-average-queue 6)) (def inputs-06 [20 22 21 24 24 23 25 26 20 24 26 26 25 27 28 27 29 27 25 24]) (doseq [input inputs-06] (println (moving-average queue-06 input))) (def queue-10 (make-moving-average-queue 10)) (def inputs-10 [20 22 24 25 23 26 28 26 29 27 28 30 27 29 28]) (doseq [input inputs-10] (println (moving-average queue-10 input))) -- 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: Moving average from Java to Clojure
Hey Cecil, In addition to using peek instead of first, as indicated by Plinio, the moving-average function above uses some poor names, in hindsight, especially the "old-queue" parameter name. I'd suggest naming it queue, as it refers to an atom. You could even consider naming the function moving-average!. -- 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: Moving average from Java to Clojure
On Sunday, 20 July 2014 12:48:19 UTC+1, Cecil Westerhof wrote: > or is there a better way? > Probably not the answer you're looking for, but the exponentially-weighted moving average doesn't require any state other than the current value: (defn ewma [alpha] (fn [avg new] (+ (* (- 1 alpha) avg) (* alpha new Jony -- 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.
ANN How to Make Your Open Source Project Really Awesome, part 2
15 months ago we wrote what ended up being quite a popular post on the ClojureWerkz blog: http://blog.clojurewerkz.org/blog/2013/04/20/how-to-make-your-open-source-project-really-awesome/ and now part 2 is up: http://blog.clojurewerkz.org/blog/2014/07/20/how-to-make-your-open-source-project-really-awesome/ Many Clojure libraries are far from being incredibly well maintained, so I thought it may be worth announcing on this list. -- @michaelklishin, github.com/michaelklishin -- 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.
[jobs] for newbie in europe
Hello, I am living in Poland and i have a little problem there... totally 0 jobs for Clojure Programmers. I am also beginner in Clojure. More about my IT skills you can read on site http://wladyka.eu/ . Do you know any option to get a job in Europe or remotely if i don't have any seriously experience with Clojure? But i see it's good and i want use that more then others languages. I can start with less salary but in my area is big trouble to find any job with Clojure... If you know somebody who need fresh blood tell him about me :) Best regards, Krzysztof Władyka -- 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: [jobs] for newbie in europe
Where are you based? On Sun, Jul 20, 2014 at 4:29 PM, Krzysztof Władyka wrote: > Hello, > > I am living in Poland and i have a little problem there... totally 0 jobs > for Clojure Programmers. > > I am also beginner in Clojure. More about my IT skills you can read on > site http://wladyka.eu/ . > > Do you know any option to get a job in Europe or remotely if i don't have > any seriously experience with Clojure? But i see it's good and i want use > that more then others languages. I can start with less salary but in my > area is big trouble to find any job with Clojure... > > If you know somebody who need fresh blood tell him about me :) > > Best regards, > Krzysztof Władyka > > -- > 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.
Stumped: unable to resolve symbol in macro expansion
I don't have a Clojure REPL handy at the moment but it looks like the x symbol is being resolved in the macro context rather than the expansion context. In the macro source, where you have a plain x, try to replace it with ~'x ... This blog post may be relevant: http://amalloy.hubpages.com/hub/Unhygenic-anaphoric-Clojure-macros-for-fun-and-profit -- 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.
Database schema Heroku and Node-Webkit
I'm developing an app that will initially run on Heroku, but will eventually migrate to a Node-Webkit app. What would you suggest is the simplest way to create and maintain a schema that would work well on both a Heroku app (with PostGres) and a Node-Webkit app (perhaps Sqlite or another alternative) ? I considered using raw SQL then using Lobos analyze mode to extract a schema, then use that for multiple back ends, but that might be more work that needed. Any suggestions? -- 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.
positional + keyword args
I'm trying to write a function that takes (up to) 4 arguments. I want to be able to supply every argument positionally, with a keyword or as a default, so that (f) (f 1) (f 1 2) (f 1 2 3) (f 1 2 3 4) (f 1 :b 2) (f 1 2 :c 3) ... (f :a 1 :b 2 :c 3 :d 4) are all equivalent. In Python, I could do this by def f(a=1,b=2,c=3,d=4):... but I'm not sure how to do it in Clojure. -- 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: positional + keyword args
You can get keyword args like this: (defn f [& {:keys [a b c d] :or {a 1 b 2 c 3 d 4}}] [a b c d]) But you cannot also get the ability to call: (f 5 6 7 8) On Sun, Jul 20, 2014 at 4:13 PM, Sam Raker wrote: > I'm trying to write a function that takes (up to) 4 arguments. I want to > be able to supply every argument positionally, with a keyword or as a > default, so that > > (f) > (f 1) > (f 1 2) > (f 1 2 3) > (f 1 2 3 4) > (f 1 :b 2) > (f 1 2 :c 3) > ... > (f :a 1 :b 2 :c 3 :d 4) > > are all equivalent. In Python, I could do this by > > def f(a=1,b=2,c=3,d=4):... > > but I'm not sure how to do it in Clojure. > > -- > 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: positional + keyword args
I hacked this together: (defn f [& args] (let [default-args-atom (atom {:a 1 :b 2 :c 3 :d 4}) kwds (vec (keys @default-args-atom)) kwargs (map-indexed (fn [idx arg] (if (keyword? arg) (swap! default-args-atom assoc arg (get args (inc idx))) (swap! a assoc (get kwds idx) arg))) args)] (function-that-actually-does-what-i-want-to-do (:a @default-args-atom) (:b @default-args-atom) (:c @default-args-atom) (:d @default-args-atom))) But it seems kind of gross to me. Is there a cleaner solution? On Sunday, July 20, 2014 8:16:55 PM UTC-4, Alex Baranosky wrote: > > You can get keyword args like this: > > (defn f [& {:keys [a b c d] > :or {a 1 b 2 c 3 d 4}}] > [a b c d]) > > But you cannot also get the ability to call: > (f 5 6 7 8) > > > On Sun, Jul 20, 2014 at 4:13 PM, Sam Raker > wrote: > >> I'm trying to write a function that takes (up to) 4 arguments. I want to >> be able to supply every argument positionally, with a keyword or as a >> default, so that >> >> (f) >> (f 1) >> (f 1 2) >> (f 1 2 3) >> (f 1 2 3 4) >> (f 1 :b 2) >> (f 1 2 :c 3) >> ... >> (f :a 1 :b 2 :c 3 :d 4) >> >> are all equivalent. In Python, I could do this by >> >> def f(a=1,b=2,c=3,d=4):... >> >> but I'm not sure how to do it in Clojure. >> >> -- >> 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: positional + keyword args
Wait. Should be: (defn f [& args] (let [default-args-atom (atom {:a 1 :b 2 :c 3 :d 4}) kwds (vec [:a :b :c :d) kwargs (map-indexed (fn [idx arg] (if (keyword? arg) (swap! default-args-atom assoc arg (get args (inc idx))) (swap! a assoc (get kwds idx) arg))) args)] (function-that-actually-does-what-i-want-to-do (:a @default-args-atom) (:b @default-args-atom) (:c @default-args-atom) (:d @default-args-atom))) because there's no guarantee that (vec (keys @default-args-atom)) will return the keys in the same order. On Sunday, July 20, 2014 8:45:02 PM UTC-4, Sam Raker wrote: > > I hacked this together: > > (defn f [& args] (let [default-args-atom (atom {:a 1 :b 2 :c 3 :d 4}) kwds > (vec (keys @default-args-atom)) > kwargs (map-indexed (fn [idx arg] (if > (keyword? arg) (swap! default-args-atom assoc arg (get args (inc idx))) > (swap! a assoc (get kwds idx) arg))) args)] > (function-that-actually-does-what-i-want-to-do (:a @default-args-atom) > (:b @default-args-atom) (:c @default-args-atom) (:d @default-args-atom))) > > But it seems kind of gross to me. Is there a cleaner solution? > > On Sunday, July 20, 2014 8:16:55 PM UTC-4, Alex Baranosky wrote: >> >> You can get keyword args like this: >> >> (defn f [& {:keys [a b c d] >> :or {a 1 b 2 c 3 d 4}}] >> [a b c d]) >> >> But you cannot also get the ability to call: >> (f 5 6 7 8) >> >> >> On Sun, Jul 20, 2014 at 4:13 PM, Sam Raker wrote: >> >>> I'm trying to write a function that takes (up to) 4 arguments. I want to >>> be able to supply every argument positionally, with a keyword or as a >>> default, so that >>> >>> (f) >>> (f 1) >>> (f 1 2) >>> (f 1 2 3) >>> (f 1 2 3 4) >>> (f 1 :b 2) >>> (f 1 2 :c 3) >>> ... >>> (f :a 1 :b 2 :c 3 :d 4) >>> >>> are all equivalent. In Python, I could do this by >>> >>> def f(a=1,b=2,c=3,d=4):... >>> >>> but I'm not sure how to do it in Clojure. >>> >>> -- >>> 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: positional + keyword args
Wait. Should be: (defn f [& args] (let [default-args-atom (atom {:a 1 :b 2 :c 3 :d 4}) kwds [:a :b :c :d] kwargs (map-indexed (fn [idx arg] (if (keyword? arg) (swap! default-args-atom assoc arg (get args (inc idx))) (swap! a assoc (get kwds idx) arg))) args)] (function-that-actually-does-what-i-want-to-do (:a @default-args-atom) (:b @default-args-atom) (:c @default-args-atom) (:d @default-args-atom))) because there's no guarantee (vec (keys @default-args-atom)) will return the keys in the same order every time. On Sunday, July 20, 2014 8:45:02 PM UTC-4, Sam Raker wrote: > > I hacked this together: > > (defn f [& args] (let [default-args-atom (atom {:a 1 :b 2 :c 3 :d 4}) kwds > (vec (keys @default-args-atom)) > kwargs (map-indexed (fn [idx arg] (if > (keyword? arg) (swap! default-args-atom assoc arg (get args (inc idx))) > (swap! a assoc (get kwds idx) arg))) args)] > (function-that-actually-does-what-i-want-to-do (:a @default-args-atom) > (:b @default-args-atom) (:c @default-args-atom) (:d @default-args-atom))) > > But it seems kind of gross to me. Is there a cleaner solution? > > On Sunday, July 20, 2014 8:16:55 PM UTC-4, Alex Baranosky wrote: >> >> You can get keyword args like this: >> >> (defn f [& {:keys [a b c d] >> :or {a 1 b 2 c 3 d 4}}] >> [a b c d]) >> >> But you cannot also get the ability to call: >> (f 5 6 7 8) >> >> >> On Sun, Jul 20, 2014 at 4:13 PM, Sam Raker wrote: >> >>> I'm trying to write a function that takes (up to) 4 arguments. I want to >>> be able to supply every argument positionally, with a keyword or as a >>> default, so that >>> >>> (f) >>> (f 1) >>> (f 1 2) >>> (f 1 2 3) >>> (f 1 2 3 4) >>> (f 1 :b 2) >>> (f 1 2 :c 3) >>> ... >>> (f :a 1 :b 2 :c 3 :d 4) >>> >>> are all equivalent. In Python, I could do this by >>> >>> def f(a=1,b=2,c=3,d=4):... >>> >>> but I'm not sure how to do it in Clojure. >>> >>> -- >>> 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: positional + keyword args
Nope, that doesn't actually work... On Sunday, July 20, 2014 8:57:47 PM UTC-4, Sam Raker wrote: > > Wait. Should be: > > (defn f [& args] (let [default-args-atom (atom {:a 1 :b 2 :c 3 :d 4}) kwds > [:a :b :c :d] > kwargs (map-indexed (fn [idx arg] (if > (keyword? arg) (swap! default-args-atom assoc arg (get args (inc idx))) > (swap! a assoc (get kwds idx) arg))) args)] > (function-that-actually-does-what-i-want-to-do (:a @default-args-atom) > (:b @default-args-atom) (:c @default-args-atom) (:d @default-args-atom))) > > because there's no guarantee (vec (keys @default-args-atom)) will return > the keys in the same order every time. > > On Sunday, July 20, 2014 8:45:02 PM UTC-4, Sam Raker wrote: >> >> I hacked this together: >> >> (defn f [& args] (let [default-args-atom (atom {:a 1 :b 2 :c 3 :d 4}) >> kwds (vec (keys @default-args-atom)) >> kwargs (map-indexed (fn [idx arg] (if >> (keyword? arg) (swap! default-args-atom assoc arg (get args (inc idx))) >> (swap! a assoc (get kwds idx) arg))) args)] >> (function-that-actually-does-what-i-want-to-do (:a >> @default-args-atom) (:b @default-args-atom) (:c @default-args-atom) (:d >> @default-args-atom))) >> >> But it seems kind of gross to me. Is there a cleaner solution? >> >> On Sunday, July 20, 2014 8:16:55 PM UTC-4, Alex Baranosky wrote: >>> >>> You can get keyword args like this: >>> >>> (defn f [& {:keys [a b c d] >>> :or {a 1 b 2 c 3 d 4}}] >>> [a b c d]) >>> >>> But you cannot also get the ability to call: >>> (f 5 6 7 8) >>> >>> >>> On Sun, Jul 20, 2014 at 4:13 PM, Sam Raker wrote: >>> I'm trying to write a function that takes (up to) 4 arguments. I want to be able to supply every argument positionally, with a keyword or as a default, so that (f) (f 1) (f 1 2) (f 1 2 3) (f 1 2 3 4) (f 1 :b 2) (f 1 2 :c 3) ... (f :a 1 :b 2 :c 3 :d 4) are all equivalent. In Python, I could do this by def f(a=1,b=2,c=3,d=4):... but I'm not sure how to do it in Clojure. -- 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: positional + keyword args
OK! After a few false starts, I think I got it: (defn f [& args] (let [args (vec args) default-args-atom (atom {:a 1 :b 2 :c 3 :d 4}) kwds [:a :b :c :d]] (do (dorun (map-indexed (fn [idx arg] (if (keyword? arg) (swap! default-args-atom assoc arg (get args (inc idx))) (if (not (keyword? (get args (dec idx (swap! default-args-atom assoc (get kwds idx) arg args)) (the-real-func (:a @default-args-atom) (:b @default-args-atom) (:c @default-args-atom) (:d @default-args-atom) I feel like I should turn this into a macro, but I've spent enough time on it as is, for now. On Sunday, July 20, 2014 8:16:55 PM UTC-4, Alex Baranosky wrote: > > You can get keyword args like this: > > (defn f [& {:keys [a b c d] > :or {a 1 b 2 c 3 d 4}}] > [a b c d]) > > But you cannot also get the ability to call: > (f 5 6 7 8) > > > On Sun, Jul 20, 2014 at 4:13 PM, Sam Raker > wrote: > >> I'm trying to write a function that takes (up to) 4 arguments. I want to >> be able to supply every argument positionally, with a keyword or as a >> default, so that >> >> (f) >> (f 1) >> (f 1 2) >> (f 1 2 3) >> (f 1 2 3 4) >> (f 1 :b 2) >> (f 1 2 :c 3) >> ... >> (f :a 1 :b 2 :c 3 :d 4) >> >> are all equivalent. In Python, I could do this by >> >> def f(a=1,b=2,c=3,d=4):... >> >> but I'm not sure how to do it in Clojure. >> >> -- >> 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.