r/dailyprogrammer • 1 3 • Jul 14 '14

[Weekly #2] Pre-coding Work

Weekly Topic #2:

What work do you do before coding your solution? What kind of planning or design work if any do you do? How do you do it? Paper and pencil? Draw a picture? Any online/web based tools?

Give some examples of your approach to handling the dailyprogrammer challenges and your process that occurs before you start coding.

Last week's Topic:

[Weekly Topic #1] (http://www.reddit.com/r/dailyprogrammer/comments/2a3pzl/weekly_1_handling_console_input/)

70 Upvotes

56 comments sorted by

View all comments

Show parent comment

4

u/skeeto -9 8 Jul 15 '14 edited Jul 15 '14

I'm by no means an expert in functional programming yet, but it's pulled me away from OO entirely now.

OO is entirely compatible with functional programming. Functional-style OO treats objects as immutable values, and methods will return new objects rather than mutate the target object. Classes are for grouping values together as a new type of value, and methods are functions that can be specialized for one (single dispatch) or more (multiple dispatch) specific types.

Here's an example in Common Lisp, which combines functional and OO style via CLOS. Define a 2D vector class with two read-only numeric fields, x and y.

(defclass vec2 ()
  ((x :reader x :initarg :x)
   (y :reader y :initarg :y)))

(defun vec2 (x y)
  (make-instance 'vec2 :x x :y y))

In Common Lisp methods don't belong to classes. They're functions specialized to specific types of objects/values. The defclass above creates two methods, x and y, specialized for accessing the slots on vec2 objects. The following magnitude and dot methods are also specialized to vec2.

(defmethod magnitude ((v vec2))
  (sqrt (+ (expt (x v) 2)
           (expt (y v) 2))))

(defmethod dot ((a vec2) (b vec2))
  (+ (* (x a) (x b))
     (* (y a) (y b))))

Here's a normalize method that returns a new vec2 object as a normalized version of its argument.

(defmethod normalize ((v vec2))
  (let ((length (magnitude v)))
    (vec2 (/ (x v) length)
          (/ (y v) length))))

(normalize (vec2 1 1.5))
;; => (vec2 0.5547002 0.8320503)

Now define the same methods for a vec3 class. Note that I'm not bothering with inheritance (the empty parens after the class name).

(defclass vec3 ()
  ((x :reader x :initarg :x)
   (y :reader y :initarg :y)
   (z :reader z :initarg :z)))

(defun vec3 (x y z)
  (make-instance 'vec3 :x x :y y :z z))

(defmethod magnitude ((v vec3))
  (sqrt (+ (expt (x v) 2)
           (expt (y v) 2)
           (expt (z v) 2))))

(defmethod dot ((a vec3) (b vec3))
  (+ (* (x a) (x b))
     (* (y a) (y b))
     (* (z a) (z b))))

(defmethod normalize ((v vec3))
  (let ((length (magnitude v)))
    (vec3 (/ (x v) length)
          (/ (y v) length)
          (/ (z v) length))))

(normalize (vec3 1 2 3))
;; => (vec2 0.26726124 0.5345225 0.8017837)

Here's the cool part: define an angle-between function that computes the angle between two vectors of the same dimension. It uses methods, but it's not actually a method itself, yet it will work with any type with reasonable definitions for dot and normalize (duck typing), such as some future vec4 class.

(defun angle-between (a b)
  (acos (dot (normalize a) (normalize b))))

(angle-between (vec2 1 1) (vec2 0 1))
;; => 0.7853982 (45°)

(angle-between (vec3 0.1 0 0) (vec3 0 0 1.2))
;; => 1.5707964 (90°)

This is all functional-style programming, since there's no statefulness and everything is a value -- and it's also OO, since we're defining classes and methods to operate on those classes.

This year I finally realized it just needed to be some simple maps, and some functions that act on those, and it removed all roadblocks

Your maps are really just structs without a formally-declared type. They're like anonymous structs. JavaScript, and any language prototype-based OO, works like this. All objects are just maps. Classes are not formally declared to the language. Instead, methods -- functions assigned to keys on the map -- are just looked up on the maps themselves along with everything else.

The advantage of a declared struct would be:

  • Performance: the compiler knows exactly what "keys" you plan to use
  • Clarity: you're declaring your intentions up front
  • Correctness: the compiler can verify you don't pass the wrong map
  • Dispatch: even if two maps have the same structure (the same keys), you could still behave differently (dispatch) depending on an intrinsic quality (type). This is refered to as nominative typing vs. structural typing.

As a final note: in my opinion, you can't practically build large systems without some form of OOP. Encapsulation and polymorphism is too essential. If your language doesn't support it, then you'll end up spinning your own (i.e. the Linux kernel with C). Is there any large, complex piece of software on your computer not primarily using OOP?

1

u/gfixler Jul 16 '14

So in answer to my question, yes, you have tried this :)

Thanks for the great reply! I've played a bit in CL (made it halfway through PCL), and I play in Clojure quite a bit now, so I know about method dispatch as a nicer form of the more commonly-used OO out there. I agree with your points, and could have been more clear in mine. I was referring to the way most are using classes, i.e. as mutable, non-values.

It's an area of ongoing study, but I feel you're right about needing OO-like concepts in this functional world. I often do want to know what a collection of things as a whole actually is, or can be considered, without relying heuristics over its internals. I do want to have things like pre/post conditions to keep my sanity. I have indeed spun my own workarounds, or just let things be, and hoped :)