From ae0e6caa08a4f86b9c938e26c4dc6dea9ff4bd73 Mon Sep 17 00:00:00 2001 From: TREE_3 Date: Thu, 27 Feb 2025 03:09:18 +0000 Subject: [PATCH] !253 improve rich-vector%zip-with-index --- GoldfishLang.tmu | 22 ++++++++++++++-------- goldfish/liii/lang.scm | 19 +++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/GoldfishLang.tmu b/GoldfishLang.tmu index 2506ca19..f7b9437d 100644 --- a/GoldfishLang.tmu +++ b/GoldfishLang.tmu @@ -5388,21 +5388,27 @@ <\goldfish-chunk|goldfish/liii/lang.scm|true|true> (chained-define (%zip-with-index) - \ \ (chained-define (loop lst idx save) + \ \ (let* ((n (vector-length data)) - \ \ \ \ (if (null? lst) + \ \ \ \ \ \ \ \ \ (result (make-vector n))) - \ \ \ \ \ \ \ \ (reverse (list-\vector save)) \ + \ \ \ \ (let loop ((idx 0)) - \ \ \ \ \ \ \ \ (loop (cdr lst)\ + \ \ \ \ \ \ (if (\= idx n) - \ \ \ \ \ \ \ \ \ \ \ \ \ \ (+ idx 1)\ + \ \ \ \ \ \ \ \ \ \ (rich-vector result) - \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons (cons idx (car lst)) save)))) + \ \ \ \ \ \ \ \ \ \ (begin - \; + \ \ \ \ \ \ \ \ \ \ \ \ (vector-set!\ + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ result\ + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ idx\ + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (cons idx (vector-ref data idx))) - \ \ (rich-vector (loop (vector-\list data) 0 '()))) + \ \ \ \ \ \ \ \ \ \ \ \ (loop (+ idx 1))))))) \; diff --git a/goldfish/liii/lang.scm b/goldfish/liii/lang.scm index b9876b36..3152472c 100644 --- a/goldfish/liii/lang.scm +++ b/goldfish/liii/lang.scm @@ -1121,14 +1121,17 @@ (rich-hash-table group))) (chained-define (%zip-with-index) - (chained-define (loop lst idx save) - (if (null? lst) - (reverse (list->vector save)) - (loop (cdr lst) - (+ idx 1) - (cons (cons idx (car lst)) save)))) - - (rich-vector (loop (vector->list data) 0 '()))) + (let* ((n (vector-length data)) + (result (make-vector n))) + (let loop ((idx 0)) + (if (>= idx n) + (rich-vector result) + (begin + (vector-set! + result + idx + (cons idx (vector-ref data idx))) + (loop (+ idx 1))))))) (chained-define (%distinct) (let ((ht (make-hash-table))