Skip to content

Instantly share code, notes, and snippets.

@egri-nagy
Last active April 8, 2017 04:03
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 egri-nagy/962e7660f264ee9275dae05a74c0b3fb to your computer and use it in GitHub Desktop.
Save egri-nagy/962e7660f264ee9275dae05a74c0b3fb to your computer and use it in GitHub Desktop.
Standard example of destructuring: calculating the slope of a line
; representation of a line defined by two points
(def l [[1 2] [2 4]])
;information extracted on demand, actual calculation obscure
(defn slope
[line]
(/
(- (first (first line)) (first (second line)))
(- (second (first line)) (second (second line)))))
;argument pre-processed, pieces of information print-named
;actual computation clear, but explicit naming tedious
(defn slope2
[line]
(let [p1 (first line)
p2 (second line)
x1 (first p1)
y1 (second p1)
x2 (first p2)
y2 (second p2)]
(/ (- x1 x2) (- y1 y2))))
; win-win with destructuring
(defn slope3
[[[x1 y1] [x2 y2]]]
(/ (- x1 x2) (- y1 y2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment