Skip to content

Instantly share code, notes, and snippets.

@sunilnandihalli
Created October 28, 2012 14:40
Show Gist options
  • Save sunilnandihalli/3968777 to your computer and use it in GitHub Desktop.
Save sunilnandihalli/3968777 to your computer and use it in GitHub Desktop.
(defprotocol polynomial
(eval-poly [this t])
(derivative [this])
(derivative [this n])
(coeffs [this]))
(deftype poly [coeffs]
polynomial
(eval-poly [this t]
(reduce #(+ (* %1 t) %2) 0.0 coeffs))
(derivative [this]
(->poly (map #(* %1 %2) coeffs (range (count coeffs) 0 -1))))
(derivative [this n]
(loop [c-poly this n-left n]
(if-not (> n 0) c-poly
(recur (derivative c-poly) (dec n)))))
(coeffs [this] coeffs))
@sunilnandihalli
Copy link
Author

it keeps giving me the following error
1 compiler notes:

Unknown location:
error: java.lang.IllegalArgumentException: Can't define method not in interfaces: derivative

core.clj:113:1:
error: java.lang.IllegalArgumentException: Can't define method not in interfaces: derivative, compiling:(/home/sunil/swig-java/one/src/clj/
one/core.clj:113)

Compilation failed.

@kumarshantanu
Copy link

(defprotocol polynomial
    (eval-poly [this t])
    (derivative [this])
    (derivative [this n])
    (coeffs [this]))

(deftype poly [coeffs]
    polynomial
    (eval-poly [this t]
          (reduce #(+ (* %1 t) %2) 0.0 coeffs))
    (derivative
      ([this]
        (->poly (map #(* %1 %2) coeffs (range (count coeffs) 0 -1))))
      ([this n]
          (loop [c-poly this n-left n]
                  (if-not (> n 0) c-poly
                            (recur (derivative c-poly) (dec n))))))
    (coeffs [this] coeffs))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment