I had another running into abstraction programming with clojure. This came up when I was coding pacman’s pill representations. I need to store coordinates. At some point I had settled on the following representation
{[ -1 2]}
which is a set of vectors with two points.
At this point I remembered how it is important to code against interfaces and not implementation. So I came up with the following function to create the representation of a coordinate.
defn coordinates “It creates a coordinate input: x y output: coordinate representation” [x y] [x y])
At this point the implementation is a vector with two values. To work with these coordinates I wrote two functions to get x and y. I could use the vector directly, but then that is not using abstraction. The functions create that abstraction.
(defn get-x “retrives the x coordinate from a coordinate representation” [coordinates] (first coordinates))
(defn get-y “retrives the y coordinate from a coordinate representation” [coordinates] (second coordinates))
I ran into an issue when trying to test the function that creates the abstraction. The problem is that one cannot test it. Testing it would be testing the implementation. Whenever you have a function that creates an abstraction, you should not test that function. Instead, you must test the functions that work with that implementation.