Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn operator [action]
- (fn [& operands]
- (fn [map] (apply action (mapv #(% map) operands)))))
- (def myMinMax #(fn rec
- ([a] a)
- ([a b] (if (% a b) a b))
- ([a b & more] (apply rec (rec a b) more))))
- (def add (operator +))
- (def subtract (operator -))
- (def multiply (operator *))
- (def divide (operator (fn
- ([a b] (/ (double a) b))
- ([a b & more] (apply divide (divide a b) more)))))
- (def negate (operator -))
- (def sqrt (operator #(Math/sqrt (Math/abs (double %)))))
- (def square (operator #(* % %)))
- (def max (operator (myMinMax >)))
- (def min (operator (myMinMax <)))
- (def med (operator #(nth (sort %&) (/ (count %&) 2))))
- (def avg (operator #(/ (apply + %&) (count %&))))
- (def constant #(fn [_] %))
- (def variable #(fn [map] (get map %)))
- (def fun-operations {'+ add
- '- subtract
- '* multiply
- '/ divide
- 'negate negate
- 'sqrt sqrt
- 'square square
- 'min min
- 'max max
- 'med med
- 'avg avg})
- (defn parseList [s]
- (if (list? s)
- (apply (get fun-operations (first s)) (map parseList (rest s)))
- (if (number? s)
- (constant s)
- (variable (str s)))
- )
- )
- (def parseFunction #(parseList (read-string %)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement