mirror of
https://github.com/kanaka/mal.git
synced 2024-09-11 13:55:55 +03:00
lib/load-file-once: basic support for multiple imports
This commit is contained in:
parent
70305b673d
commit
13e679cdde
@ -17,9 +17,6 @@ However, here are some guidelines.
|
||||
is not possible, for example for macros, give them a name starting
|
||||
with an underscore.
|
||||
|
||||
- Support successive imports safely by giving the same definitions
|
||||
again.
|
||||
|
||||
If a module provides tests, you may run against an implementation IMPL
|
||||
with these commands.
|
||||
```
|
||||
@ -27,3 +24,12 @@ make IMPL^stepA
|
||||
cd tests
|
||||
python ../runtest.py lib/MODULE.mal ../IMPL/run
|
||||
```
|
||||
|
||||
Users and implementors should use the following syntax in order to
|
||||
ensure that the same file is only loaded once.
|
||||
|
||||
```
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/foo.mal")
|
||||
(load-file-once "../lib/bar.mal")
|
||||
```
|
||||
|
18
lib/load-file-once.mal
Normal file
18
lib/load-file-once.mal
Normal file
@ -0,0 +1,18 @@
|
||||
;; Like load-file, but will never load the same path twice.
|
||||
|
||||
;; This file is normally loaded with `load-file`, so it needs a
|
||||
;; different mechanism to neutralize multiple inclusions of
|
||||
;; itself. Moreover, the file list should never be reset.
|
||||
|
||||
(def! load-file-once
|
||||
(try*
|
||||
load-file-once
|
||||
(catch* _
|
||||
(let* [seen (atom {"../lib/load-file-once.mal" nil})]
|
||||
(fn* [filename]
|
||||
(if (not (contains? @seen filename))
|
||||
(do
|
||||
(swap! seen assoc filename nil)
|
||||
(load-file filename))))))))
|
||||
|
||||
nil
|
@ -1,6 +1,7 @@
|
||||
;; Mesure performances.
|
||||
|
||||
(load-file "../lib/trivial.mal") ; gensym inc
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/trivial.mal") ; gensym inc
|
||||
|
||||
;; Evaluate an expression, but report the time spent
|
||||
(defmacro! time
|
||||
|
@ -1,6 +1,7 @@
|
||||
;; Iteration on evaluations interpreted as boolean values.
|
||||
|
||||
(load-file "../lib/trivial.mal") ; gensym
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/trivial.mal") ; gensym
|
||||
|
||||
;; `(cond test1 result1 test2 result2 .. testn resultn)`
|
||||
;; is rewritten (in the step files) as
|
||||
|
@ -1,6 +1,7 @@
|
||||
;; Composition of partially applied functions.
|
||||
|
||||
(load-file "../lib/reducers.mal") ; reduce
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/reducers.mal") ; reduce
|
||||
|
||||
;; Rewrite x (a a1 a2) .. (b b1 b2) as
|
||||
;; (b (.. (a x a1 a2) ..) b1 b2)
|
||||
|
1
tests/lib/load-file-once-inc.mal
Normal file
1
tests/lib/load-file-once-inc.mal
Normal file
@ -0,0 +1 @@
|
||||
(swap! counter (fn* [x] (+ 1 x)))
|
38
tests/lib/load-file-once.mal
Normal file
38
tests/lib/load-file-once.mal
Normal file
@ -0,0 +1,38 @@
|
||||
(def! counter (atom 0))
|
||||
;=>(atom 0)
|
||||
|
||||
;; The counter is increased by each `load-file`.
|
||||
(load-file "../tests/lib/load-file-once-inc.mal")
|
||||
;=>1
|
||||
(load-file "../tests/lib/load-file-once-inc.mal")
|
||||
;=>2
|
||||
|
||||
;; load-file-once is available
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
;=>nil
|
||||
|
||||
;; First import actually calls `load-file`.
|
||||
(load-file-once "../tests/lib/load-file-once-inc.mal")
|
||||
;=>3
|
||||
|
||||
;; Later imports do nothing.
|
||||
(load-file-once "../tests/lib/load-file-once-inc.mal")
|
||||
;=>nil
|
||||
@counter
|
||||
;=>3
|
||||
|
||||
;; Loading the module twice does not reset its memory.
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
;=>nil
|
||||
(load-file-once "../tests/lib/load-file-once-inc.mal")
|
||||
;=>nil
|
||||
@counter
|
||||
;=>3
|
||||
|
||||
;; even if done with itself
|
||||
(load-file-once "../lib/load-file-once.mal")
|
||||
;=>nil
|
||||
(load-file-once "../tests/lib/load-file-once-inc.mal")
|
||||
;=>nil
|
||||
@counter
|
||||
;=>3
|
@ -1,6 +1,6 @@
|
||||
(load-file "../tests/computations.mal")
|
||||
;=>nil
|
||||
(load-file "../lib/memoize.mal")
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../tests/computations.mal")
|
||||
(load-file-once "../lib/memoize.mal")
|
||||
;=>nil
|
||||
|
||||
(def! N 32)
|
||||
|
@ -1,4 +1,5 @@
|
||||
(load-file "../lib/pprint.mal")
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/pprint.mal")
|
||||
;=>nil
|
||||
|
||||
(pprint '(7 8 9 "ten" [11 12 [13 14]] 15 16))
|
||||
|
@ -1,4 +1,5 @@
|
||||
(load-file "../lib/protocols.mal")
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/protocols.mal")
|
||||
;=>nil
|
||||
|
||||
;; Testing find-type for normal objects.
|
||||
|
@ -1,4 +1,5 @@
|
||||
(load-file "../lib/reducers.mal")
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/reducers.mal")
|
||||
;=>nil
|
||||
|
||||
;; Testing reduce
|
||||
|
@ -1,4 +1,5 @@
|
||||
(load-file "../lib/test_cascade.mal")
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/test_cascade.mal")
|
||||
;=>nil
|
||||
|
||||
;; Testing or
|
||||
|
@ -1,4 +1,5 @@
|
||||
(load-file "../lib/threading.mal")
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/threading.mal")
|
||||
;=>nil
|
||||
|
||||
;; Testing -> macro
|
||||
|
@ -1,4 +1,5 @@
|
||||
(load-file "../lib/trivial.mal")
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/trivial.mal")
|
||||
;=>nil
|
||||
|
||||
(inc 12)
|
||||
|
@ -1,6 +1,7 @@
|
||||
(load-file "../lib/threading.mal") ; ->
|
||||
(load-file "../lib/perf.mal") ; time
|
||||
(load-file "../lib/test_cascade.mal") ; or
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/threading.mal") ; ->
|
||||
(load-file-once "../lib/perf.mal") ; time
|
||||
(load-file-once "../lib/test_cascade.mal") ; or
|
||||
|
||||
;;(prn "Start: basic macros performance test")
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
(load-file "../tests/computations.mal") ; fib sumdown
|
||||
(load-file "../lib/perf.mal") ; time
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../tests/computations.mal") ; fib sumdown
|
||||
(load-file-once "../lib/perf.mal") ; time
|
||||
|
||||
;;(prn "Start: basic math/recursion test")
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
(load-file "../lib/threading.mal") ; ->
|
||||
(load-file "../lib/perf.mal") ; run-fn-for
|
||||
(load-file "../lib/test_cascade.mal") ; or
|
||||
(load-file "../lib/load-file-once.mal")
|
||||
(load-file-once "../lib/threading.mal") ; ->
|
||||
(load-file-once "../lib/perf.mal") ; run-fn-for
|
||||
(load-file-once "../lib/test_cascade.mal") ; or
|
||||
|
||||
;;(prn "Start: basic macros/atom test")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user