forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib/load-file-once: basic support for multiple imports
- Loading branch information
1 parent
70305b6
commit 13e679c
Showing
17 changed files
with
98 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(swap! counter (fn* [x] (+ 1 x))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters