Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice for a Clojure record to implement IFn?

Suppose I have a record that is "function-like", at least in the sense that it represents an operation that could be applied to some arguments.

I can make it work as a function by implementing clojure.lang.IFn, something like:

(defrecord Func [f x]
  clojure.lang.IFn
    (invoke [this arg]
      (f x arg))
    (applyTo [this args]
      (apply f x args)))

 ((->Func + 7) 1)
 => 8

(yes I know that I've just reimplemented an inferior version of partial.... it's just an example :-) )

Is making a record implement clojure.lang.IFn a good practice or not?

Any pitfalls to this approach?

like image 413
mikera Avatar asked Aug 15 '12 02:08

mikera


1 Answers

I'm surprised it doesn't already. Records are supposed to be "a complete implementation of a persistent map". So to answer your question, I'd expect it to be a function of its keys, as a map is; anything else would be quite surprising.

like image 123
Alex Taggart Avatar answered Oct 22 '22 15:10

Alex Taggart