1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-13 01:43:50 +03:00
mal/core.mal
Joel Martin db4c329aff All: perf test, Makefile refactor, add *host-language*
Other:
- bash,make,postscript: quasiquote of vectors
- Fix Java slurp
- add time function to core.mal
    - switches on *host-language* for make time-secs vs time-ms
- Ignore */experiments directories
2014-04-17 21:49:07 -05:00

100 lines
2.3 KiB
Plaintext

(def! inc (fn* (a) (+ a 1)))
(def! dec (fn* (a) (- a 1)))
(def! zero? (fn* (n) (= 0 n)))
(def! reduce
(fn* (f init xs)
(if (> (count xs) 0)
(reduce f (f init (first xs)) (rest xs))
init)))
(def! identity (fn* (x) x))
(def! every?
(fn* (pred xs)
(if (> (count xs) 0)
(if (pred (first xs))
(every? pred (rest xs))
false)
true)))
(def! not (fn* (x) (if x false true)))
(def! some
(fn* (pred xs)
(if (> (count xs) 0)
(let* (res (pred (first xs)))
(if (pred (first xs))
res
(some pred (rest xs))))
nil)))
(defmacro! and
(fn* (& xs)
(if (empty? xs)
true
(if (= 1 (count xs))
(first xs)
`(let* (and_FIXME ~(first xs))
(if and_FIXME (and ~@(rest xs)) and_FIXME))))))
(defmacro! or
(fn* (& xs)
(if (empty? xs)
nil
(if (= 1 (count xs))
(first xs)
`(let* (or_FIXME ~(first xs))
(if or_FIXME or_FIXME (or ~@(rest xs))))))))
(defmacro! cond
(fn* (& clauses)
(if (> (count clauses) 0)
(list 'if (first clauses)
(if (> (count clauses) 1)
(nth clauses 1)
(throw "cond requires an even number of forms"))
(cons 'cond (rest (rest clauses)))))))
(defmacro! ->
(fn* (x & xs)
(if (empty? xs)
x
(let* (form (first xs)
more (rest xs))
(if (empty? more)
(if (list? form)
`(~(first form) ~x ~@(rest form))
(list form x))
`(-> (-> ~x ~form) ~@more))))))
(defmacro! ->>
(fn* (x & xs)
(if (empty? xs)
x
(let* (form (first xs)
more (rest xs))
(if (empty? more)
(if (list? form)
`(~(first form) ~@(rest form) ~x)
(list form x))
`(->> (->> ~x ~form) ~@more))))))
(if (= "make" *host-language*)
(defmacro! time
(fn* (exp)
`(let* [start_FIXME (time-secs)
ret_FIXME ~exp]
(do
(prn (str "Elapsed time: " (- (time-secs) start_FIXME) "000 msecs"))
ret_FIXME))))
(defmacro! time
(fn* (exp)
`(let* [start_FIXME (time-ms)
ret_FIXME ~exp]
(do
(prn (str "Elapsed time: " (- (time-ms) start_FIXME) " msecs"))
ret_FIXME)))))