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:
parent
eacb46df65
commit
d0efab8745
24
lib/alias-hacks.mal
Normal file
24
lib/alias-hacks.mal
Normal 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
55
tests/lib/alias-hacks.mal
Normal 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"
|
||||
|
Loading…
Reference in New Issue
Block a user