TL;DR: I want to know best practices for designing functions with multiple 
optional arguments.

Okay, so I'm working to build machine learning algorithms in Clojure, and 
they tend to need many arguments. Being a long-time Ruby dev, I like to 
provide sensible defaults for almost all potential arguments that the 
functions take. However, there are some parameters that have to be explicit 
(namely, the data).

To alleviate the pain here, I've started to experiment with named 
arguments. So far, I've come up with something like the following:

(defn descend [xs ys & args]
  (let [defaults {:gradient-fn gradient
                  :cost-fn cost
                  :yield-fn println
                  :alpha 0.01
                  :iterations 1000
                  :thetas (matrix 0 (second (dim xs)) 1)}
        options (merge defaults (apply hash-map args))
        {:keys [gradient-fn cost-fn yield-fn thetas alpha iterations]} 
options]
    (do-the-algorithm-using-locally-bound-vars)))

It's a little wordy and could be extracted into a macro a la defnk (RIP 
clojure.contrib.def), but it works.

However, if I then want to use method delegation for some algorithms, the 
named argument endeavor gets trickier.

Say I have the same function in two namespaces. One is a general gradient 
descent function, and the other is a specific gradient descent function 
whose only role is to curry a named parameter into the general function.

I want to do something like the following:

(defn descend [xs ys & args]
  (optimization/descend xs ys (conj args :cost-fn cost)))

The problem, of course, is that if I want to delegate the args array to 
another function, I have to destructure args first before passing it into 
another function. In fact, if I ever have a delegating function like this 
(where a partial apply isn't good enough), I can't pass args through to the 
delegating function because it's automatically vectorized.

How do I splat vectors into parameter lists? (Should I be passing in 
records/maps instead of named parameters?)

Thanks,
David

-- 
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

Reply via email to