Skip to content

Commit

Permalink
as-vec, for-by - impl + docstring + test
Browse files Browse the repository at this point in the history
  • Loading branch information
leonoel committed Jan 26, 2024
1 parent 772dc49 commit 9cccd8c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/hyperfiddle/electric_de.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,32 @@ For each tuple in the cartesian product of `table1 table2 ,,, tableN`, calls bod
`(call (r/bind-args (fn ~args ~@body)
~@(map (clojure.core/fn [expr]
`(r/fixed-signals (join (i/items (pure ~expr)))))
exprs))))))
exprs))))))

(defmacro as-vec "
Syntax :
```clojure
(as-vec table)
```
Returns a single vector containing elements of `table`.
" [expr] `(input (m/reductions i/patch-vec (pure ~expr))))

(defmacro for-by "
Syntax :
```clojure
(for-by kf [sym1 coll1
sym2 coll2
,,, ,,,
symN collN]
& body)
```
Stabilizes successives states of `coll1 coll2 ,,, collN` with function `kf`. For each tuple in the cartesian product of
resulting tables, calls body in an implicit `do` with symbols `sym1 sym2 ,,, symN` bound to the singleton tables for
this tuple. Returns the concatenation of all body results as a single vector.
" [kf bindings & body]
`(as-vec
~((clojure.core/fn rec [bindings]
(if-some [[sym expr & bindings] bindings]
`(cursor [~sym (diff-by ~kf ~expr)]
~(rec bindings)) `(do ~@body)))
(seq bindings))))
21 changes: 20 additions & 1 deletion test/hyperfiddle/electric/impl/runtime_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,23 @@
% := {:degree 4, :permutation {}, :grow 1, :shrink 0, :freeze #{},
:change {3 {:name "bob", :owner "jack", :kind "horse", :personality "skittish"}}}
(swap! !animals pop)
% := {:degree 4, :permutation {}, :grow 0, :shrink 1, :change {}, :freeze #{}})
% := {:degree 4, :permutation {}, :grow 0, :shrink 1, :change {}, :freeze #{}})

(tests
(def !x (atom "hello"))
(def !y (atom "electric"))
(on-diff! rcf/tap
(root-frame (e/as-vec (e/amb (e/watch !x) (e/watch !y)))))
% := {:degree 1, :permutation {}, :grow 1, :shrink 0, :change {0 ["hello" "electric"]}, :freeze #{}}
(reset! !y "world")
% := {:degree 1, :permutation {}, :grow 0, :shrink 0, :change {0 ["hello" "world"]}, :freeze #{}})

(tests
(def !n (atom 3))
(on-diff! rcf/tap
(root-frame (e/for-by identity [x (range (e/watch !n))
y (range x)]
[x y])))
% := {:degree 1, :permutation {}, :grow 1, :shrink 0, :change {0 [[1 0] [2 0] [2 1]]}, :freeze #{}}
(swap! !n inc)
% := {:degree 1, :permutation {}, :grow 0, :shrink 0, :change {0 [[1 0] [2 0] [2 1] [3 0] [3 1] [3 2]]}, :freeze #{}})

0 comments on commit 9cccd8c

Please sign in to comment.