You could use Clojure protocols.

(defprotocol Rect
  (upper-left [this])
  (lower-right [this])
  (area [this]))

(defn rect1 [x y width height]
  (reify Rect
    (upper-left [_] [x y])
    (lower-right [_] [ (+ width x) (+ width y)])
    (area [_] (* width height))))

(defn rect2 [x y width height]
  (let [lr  [(+ width x) (+ width y)]]
    (reify Rect
      (upper-left [_] [x y])
      (lower-right [_] lr)
      (area [_] (* width height)))))



On Thursday, March 5, 2015 at 4:48:11 PM UTC+1, Felix E. Klee wrote:
>
> Disclaimer: I’ve never done *any* Clojure programming, but I’m curious. 
>
> Here’s how I may model an on-screen rectangle in JavaScript, a classless 
> object oriented language: 
>
>     let createRectangle = function (x, y, width, height) { 
>         return { 
>             get upperLeft() { 
>                 return [x, y]; 
>             }, 
>             get lowerRight() { 
>                 return [x + width, y + height]; 
>             }, 
>             get area() { 
>                 return width * height; 
>             } 
>         }; 
>     }; 
>
>     let r = createRectangle(0, 0, 50, 20); 
>     let s = createRectangle(10, 20, 40, 5); 
>     console.log(r.upperLeft, r.lowerRight, r.area); 
>     console.log(s.upperLeft, s.lowerRight, s.area); 
>
> Now I run the profiler. I discover that in the inner loop of my program 
> there are lots of calls needing the upper left and the lower right 
> coordinates. So I change the inner workings of the object to store these 
> instead: 
>
>     let createRectangle = function (x, y, width, height) { 
>         let upperLeft = [x, y], lowerRight = [x + width, y + height]; 
>
>         return { 
>             get upperLeft() { 
>                 return upperLeft; 
>             }, 
>             get lowerRight() { 
>                 return lowerRight; 
>             }, 
>             get area() { 
>                 return (lowerRight[0] - upperLeft[0]) * 
>                     (lowerRight[1] - upperLeft[1]); 
>             } 
>         }; 
>     }; 
>
> Boom: Now the program is twice as fast, and - what gives me great 
> comfort - I know that this change breaks nothing. I only had to edit the 
> code in *one* module. I didn’t need to think too much about efficiency 
> of data structures in the beginning. I could just start going, then 
> optimize in the end. 
>
> *Can I program in a similar way using Clojure?* 
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to