On Thursday, June 4, 2015 at 12:55:23 PM UTC-4, Phillip Lord wrote:
>
>
> I have a number of fairly nasty functions with a form that looks like 
> this: 
>
> (defn default-ontology 
>   ([f] 
>      (dispatch f)) 
>   ([f a] 
>      (dispatch f a)) 
>   ([f a b] 
>      (dispatch f a b)) 
>   ([f a b c] 
>      (dispatch f a b c)) 
>   ([f a b c d] 
>      (dispatch f a b c d)) 
>   ([f a b c d e] 
>      (dispatch f a b c d e)) 
>   ([f a b c d e fa] 
>      (dispatch f a b c d e fa)) 
>   ([f a b c d e fa g] 
>      (dispatch f a b c d e fa g)) 
>   ([f a b c d e fa g h] 
>      (dispatch f a b c d e fa g h)) 
>   ([f a b c d e fa g h i] 
>      (dispatch f a b c d e fa g h i)) 
>   ([f a b c d e fa g h i j] 
>      (dispatch f a b c d e fa g h i j))) 
>
> The reason for all of this is that I need to avoid the use of variadic 
> function calls for performance reasons -- this function gets called a 
> lot in my code base, and without this unwinding, I box and unbox 
> consistantly. 
>
> Now, I dislike the code repetition here, and indeed have found already 
> found one bug in my code where I missed a variable out, something like.... 
>
> ([f a b c d] 
>  (dispatch f a b d)) 
>
> which is hard to pick up on. 
>
> I can't make the whole thing a macro because I need to pass this as a 
> first class function. And I can't macro each of the variadic elements 
> since the I'd need to return two elements at once. 
>
> The best I have done up with so far is: 
>
> (defn ^:private form-with-arity[n] 
>   ;; left as an exercise for the reader     
> ) 
>
> (defmacro ^:private m-default-ontology 
>   `(defn default-ontology 
>       ~@(map form-with-arity (range 1 10)))) 
>
> (m-default-ontology) 
>
> Or am I missing something more obvious? 
>
> Phil 
>

Well, the example case could be replaced with (def default-ontology 
dispatch). :)

Beyond that, you might want to consider generating such repetitive and 
predictably-structured functions with a custom def-something style macro, 
but it looks like you already are.

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

Reply via email to