Saturday, June 11, 2011

Implementing get-in

This is an implementation of matrix point access:

(defn curried-apply*
  "Apply args to the curried function func."
  [func args]

  (if (= (count args) 1)
    (func (first args))
    (curried-apply* (func (first args)) (rest args))))

(defn decurrify
  "Take a curried function and return an uncurried function."
  [func]

  (fn [& args]
    (curried-apply* func args)))

(defn get-in*
  "Get the value of the matrix at a point."
  [matrix point]

  (apply (decurrify matrix) point))

This really demonstrates the power of functional programming. The function to decurrify a matrix can also be used to get the value at a point, since a matrix is actually just a curried function.

No comments:

Post a Comment