Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining reduced arity partial functions

I've sometimes found it convenient in Clojure to define reduced arity versions of functions, that return a partial function, e.g.

(defn prefix 
  ([pre string] 
    (str pre ":" string))

  ([pre] 
    (fn [string]
      (prefix pre string)))) 

This means that you can do either:

(prefix "foo" 78979)
=> "foo:78979"

((prefix "foo") 78979)
=> "foo:78979"

This seems quite Haskell-ish and avoids the need for partial to create partial functions.

But is it considered good coding style / API design in Lisp?

like image 233
mikera Avatar asked May 21 '12 02:05

mikera


1 Answers

Using partial to create a curried function is based on the concept of Explicit is better (in most cases :)). And I have found that this concept to be more applicable/used in dynamically typed languages like Clojure, Python etc may be because of the missing type signature/static-typing it makes more sense to make things explicit .

like image 148
Ankur Avatar answered Oct 07 '22 22:10

Ankur