1
0
mirror of https://github.com/janet-lang/janet synced 2025-12-11 19:18:07 +00:00

Allow matching tuples and arrays exactly via a dollar suffix.

This commit is contained in:
Calvin Rose
2025-11-25 19:36:56 -06:00
parent 8efeeaec95
commit d9105299f1
3 changed files with 12 additions and 42 deletions

View File

@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.
## Unreleased - ???
- Add `thaw-keep-keys` as a variant of thaw
- The `repl` function now respects the `*repl-prompt*` dynamic binding.
## 1.40.1 - 2025-11-16
- Fix `JANET_REDUCED_OS` build regression caused by `os/posix-chroot`.
- Code formatting

View File

@@ -1480,52 +1480,12 @@
``Do a post-order traversal of a data structure and call `(f x)`
on every visitation.``
[f form]
(f (walk (fn :visit [x] (postwalk f x)) form)))
(f (walk (fn [x] (postwalk f x)) form)))
(defn prewalk
"Similar to `postwalk`, but do pre-order traversal."
[f form]
(walk (fn :visit [x] (prewalk f x)) (f form)))
(defn- walk-dict-pairs [f form]
(def ret @{})
(eachp p1 form
(def p2 (f p1))
(when p2
(def [k v] p2)
(put ret k v)))
ret)
(defn walk2
``Iterate over the values in ast and apply `f`
to them. Collect the results in a data structure. Differs from `walk` in 2 ways:
* Will iterate fibers and gather results into an array.
* Tables and structs are traversed by visiting each key-value as a tuple instead
of iterating them in an interleaved fashion.
If ast is not a
table, struct, array, tuple, or fiber,
returns form.``
[f form]
(case (type form)
:table (walk-dict-pairs f form)
:struct (table/to-struct (walk-dict-pairs f form))
:array (walk-ind f form)
:tuple (keep-syntax! form (walk-ind f form))
:fiber (walk-ind f form)
form))
(defn postwalk2
``Do a post-order traversal of a data structure and call `(f x)`
on every visitation.``
[f form]
(f (walk2 (fn :visit [x] (postwalk2 f x)) form)))
(defn prewalk2
"Similar to `postwalk`, but do pre-order traversal."
[f form]
(walk2 (fn :visit [x] (prewalk2 f x)) (f form)))
(walk (fn [x] (prewalk f x)) (f form)))
(defmacro as->
``Thread forms together, replacing `as` in `forms` with the value

View File

@@ -185,5 +185,11 @@
(assert-no-error "iterate over coro 2" (keys (generate [x :range [0 10]] x)))
(assert-no-error "iterate over coro 3" (pairs (generate [x :range [0 10]] x)))
# thaw
(def ds1 [1 2 3 {:a 2} {:b 3} 4 5 6])
(def ds2 [1 2 3 {:a 2 {:c :d} {:e :f}} {:b 3} 4 5 6])
(assert (deep= (thaw ds1) (thaw-keep-keys ds1)) "thaw vs. thaw-keep-keys 1")
(assert (deep-not= (thaw ds2) (thaw-keep-keys ds2)) "thaw vs. thaw-keep-keys 2")
(end-suite)