From ccd874fe4ee42d2bd7037261f6bc43e1c7b2f6ff Mon Sep 17 00:00:00 2001 From: Ian Henry Date: Sun, 23 Apr 2023 21:11:56 -0700 Subject: [PATCH] add catseq --- src/boot/boot.janet | 7 +++++++ test/suite0001.janet | 3 +++ 2 files changed, 10 insertions(+) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index ae8477f0..894097b5 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -611,6 +611,13 @@ (def $accum (gensym)) ~(do (def ,$accum @[]) (loop ,head (,array/push ,$accum (do ,;body))) ,$accum)) +(defmacro catseq + ``Similar to `loop`, but concatenates each element from the loop body into an array and returns that. + See `loop` for details.`` + [head & body] + (def $accum (gensym)) + ~(do (def ,$accum @[]) (loop ,head (,array/concat ,$accum (do ,;body))) ,$accum)) + (defmacro tabseq ``Similar to `loop`, but accumulates key value pairs into a table. See `loop` for details.`` diff --git a/test/suite0001.janet b/test/suite0001.janet index 3dca5fb6..f26b873f 100644 --- a/test/suite0001.janet +++ b/test/suite0001.janet @@ -252,6 +252,9 @@ (def xs (apply tuple (seq [x :down [8 -2] :when (even? x)] (tuple (/ x 2) x)))) (assert (= xs '((4 8) (3 6) (2 4) (1 2) (0 0))) "seq macro 2") +(def xs (catseq [x :range [0 3]] [x x])) +(assert (deep= xs @[0 0 1 1 2 2]) "catseq") + # :range-to and :down-to (assert (deep= (seq [x :range-to [0 10]] x) (seq [x :range [0 11]] x)) "loop :range-to") (assert (deep= (seq [x :down-to [10 0]] x) (seq [x :down [10 -1]] x)) "loop :down-to")