Skip to content

Commit

Permalink
Add delay (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Nov 8, 2024
1 parent 0c3207c commit 5841dd8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

[Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect

## v0.8.124 (2024-11-08)

- Add `delay`

## v0.8.123 (2024-11-05)

- Fix [#572](https://github.com/squint-cljs/squint/issues/572): prevent vite page reload
Expand Down
1 change: 1 addition & 0 deletions resources/squint/core.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#{Atom
Cons
Delay
IIterable
IIterable__iterator
LazySeq
Expand Down
3 changes: 2 additions & 1 deletion src/squint/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
'or macros/core-or
'and macros/core-and
'assert macros/core-assert
'simple-benchmark macros/simple-benchmark}
'simple-benchmark macros/simple-benchmark
'delay macros/delay}
cc/common-macros))

(def core-config {:vars (edn-resource "squint/core.edn")})
Expand Down
15 changes: 15 additions & 0 deletions src/squint/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2707,3 +2707,18 @@ export function update_vals(m, f) {
}, m2, m);
return m2;
}

export class Delay {
constructor(f) {
this.f = f;
}
_deref() {
if (this.realized) {
return this.v;
} else {
this.v = this.f();
this.realized = true;
return this.v;
}
}
}
10 changes: 9 additions & 1 deletion src/squint/internal/macros.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
bit-and-not bit-clear bit-flip bit-test
bit-shift-left bit-shift-right bit-shift-right-zero-fill
unsigned-bit-shift-right bit-set undefined?
simple-benchmark])
simple-benchmark delay])
(:require [clojure.string :as str]
[squint.compiler-common :as-alias ana]
[clojure.core :as cc]
Expand Down Expand Up @@ -595,3 +595,11 @@
elapsed# (- end# start#)]
(~print-fn (str ~bs-str ", " ~expr-str ", "
~iterations " runs, " elapsed# " msecs"))))))

(core/defmacro delay
"Takes a body of expressions and yields a Delay object that will
invoke the body only the first time it is forced (with force or deref/@), and
will cache the result and return it on all subsequent force
calls."
[& body]
`(new cljs.core/Delay (fn [] ~@body) nil))
4 changes: 4 additions & 0 deletions test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2293,5 +2293,9 @@ new Foo();")
(deftest issue-537-test
(is (true? (jsv! "(not (= 1 2))"))))

(deftest delay-test
(is (eq [0 2 1 2 1]
(jsv! "(def a (atom 0)) (def x (delay (do (swap! a inc) 2))) [@a @x @a @x @a]"))))

(defn init []
(t/run-tests 'squint.compiler-test 'squint.jsx-test 'squint.string-test 'squint.html-test))

0 comments on commit 5841dd8

Please sign in to comment.