Skip to content

Commit

Permalink
!253 improve rich-vector%zip-with-index
Browse files Browse the repository at this point in the history
  • Loading branch information
TREE37 authored and da-liii committed Feb 27, 2025
1 parent 4852eb8 commit ae0e6ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
22 changes: 14 additions & 8 deletions GoldfishLang.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -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-\<gtr\>vector save)) \
\ \ \ \ (let loop ((idx 0))

\ \ \ \ \ \ \ \ (loop (cdr lst)\
\ \ \ \ \ \ (if (\<gtr\>= 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-\<gtr\>list data) 0 '())))
\ \ \ \ \ \ \ \ \ \ \ \ (loop (+ idx 1)))))))

\;
</goldfish-chunk>
Expand Down
19 changes: 11 additions & 8 deletions goldfish/liii/lang.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit ae0e6ca

Please sign in to comment.