1 minute read

Sometimes I miss the named arguments feature in Python, for example:

def slope(p1=(0,0), p2=(1,1))
  return (float(p2[1] - p1[1])) / (p2[0] - p1[0])
  
=> slope((1,2), (4,5))

=> slope(p2=(2, 1))

The equivalent in clojure can be done using destructuring:

(defn slope
  [& {:keys [p1 p2] :or {p1 [0 0] p2 [1 1]}}]
  (float (/ (- p2 1) (p1 1))
            (- p2 0) (p1 0)))
            
=> (slope :p1 [1 2] :p2 [3 4])

=> (slope :p2 [3 4])

=> (slope)
comments powered by Disqus