1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-16 17:20:23 +03:00

Added alias hacks from frock to lib. See #321.

Thanks @asarhaddon for your guidance.
This commit is contained in:
Chris McCormick 2019-07-01 16:05:26 +08:00
parent eacb46df65
commit d0efab8745
2 changed files with 79 additions and 0 deletions

24
lib/alias-hacks.mal Normal file
View File

@ -0,0 +1,24 @@
;; aliases for common clojure names to mal builtins
;; NOTE: this is a hack
;;
;; Origin: https://github.com/chr15m/frock
; TODO: re-implement as actually useful macros:
; destructuring, arg checking, etc.
(def! _alias_add_implicit
(fn* [special added]
(fn* [x & xs]
(list special x (cons added xs)))))
(defmacro! let (_alias_add_implicit 'let* 'do))
(defmacro! when (_alias_add_implicit 'if 'do))
(defmacro! def (_alias_add_implicit 'def! 'do))
(defmacro! fn (_alias_add_implicit 'fn* 'do))
(defmacro! defn (_alias_add_implicit 'def! 'fn))
(def! partial (fn* [pfn & args]
(fn* [& args-inner]
(apply pfn (concat args args-inner)))))
nil

55
tests/lib/alias-hacks.mal Normal file
View File

@ -0,0 +1,55 @@
;; Testing alias-hacks.mal
(load-file "../../lib/alias-hacks.mal")
;=>nil
;; Testing let
(macroexpand (let binds a b))
;=>(let* binds (do a b))
(let [x 2] 3 x)
;=>2
;; Testing when
(macroexpand (when condition a b))
;=>(if condition (do a b))
(when false (nth () 0) a)
;=>nil
(when true 3 2)
;=>2
;; Testing name
(macroexpand (def name a b))
;=>(def! name (do a b))
(def x 1 2 3)
;=>3
x
;=>3
;; Testing fn
(macroexpand (fn args a b))
;=>(fn* args (do a b))
((fn [x] 1 2) 3)
;=>2
;; Testing defn
(macroexpand (defn name args b))
;=>(def! name (fn args b))
(defn f [x] 1 2 x)
(f 3)
;=>3
;; Testing partial
((partial +) 1 2)
;=>3
((partial + 1) 2)
;=>3
((partial + 1 2))
;=>3
((partial not) false)
;=>true
((partial not false))
;=>true
((partial (fn* [x y] (+ x y)) 1) 2)
;=>3
((partial str 1 2) 3 4)
;=>"1234"