Created
September 6, 2013 07:47
-
-
Save tiensonqin/6460750 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
beautiful :)