Skip to content

Instantly share code, notes, and snippets.

@tiensonqin
Created September 6, 2013 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiensonqin/6460750 to your computer and use it in GitHub Desktop.
Save tiensonqin/6460750 to your computer and use it in GitHub Desktop.
;; named arguments from the joy of clojure
;; the clojure version just feels not elegant, why?
;; clojure version
(defn slope
[& {:keys [p1 p2] :or {p1 [0 0] p2 {1 1}}}]
(float (/ (- (p2 1) (p1 1))
(- (p2 0) (p1 0)))))
(slope :p1 [4 15] :p2 [3 21])
(slope [4 15] [3 21])
(slope)
;; python version
def slope(p1=(0,0), p2=(1,1)):
return (float(p2[1] - p1[1])) / (p2[0] - p1[0])
slope((4,15), (3,21))
slope(p2=(2,1))
slope()
@xyhp915
Copy link

xyhp915 commented Sep 7, 2013

beautiful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment