Hi,
If I understand your pb correctly, and form what I have seen in the Scala
world, there is maybe a simpler way to do it :
(defn process-args [args fnkeys fndefault]
(let [[positional-args named-args] (split-with (complement keyword?)
args)
named-args (into {} (map vec (partition
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)
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? ar
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)]
(
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)]
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
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 t
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 t