From 9c28e8b832003e190ee3e1c09992b54711877787 Mon Sep 17 00:00:00 2001 From: ben Date: Thu, 3 Mar 2016 02:29:21 -0500 Subject: [PATCH] [vector] mutation map --- test/vector-fail.rkt | 9 ++++++++- test/vector-pass.rkt | 4 ++++ vector.rkt | 29 ++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/test/vector-fail.rkt b/test/vector-fail.rkt index 0717b1a..e2be495 100644 --- a/test/vector-fail.rkt +++ b/test/vector-fail.rkt @@ -27,6 +27,13 @@ (vector-map: add1 (vector-map: add1 (vector-map: add1 (vector 0 0 0)))) 3) + (vector-ref: (vector-map!: (lambda (x) x) (vector #t #t)) 4) + (vector-ref: (vector-map!: symbol->string (vector 'a 'b)) 0) + + (vector-ref: + (vector-map!: add1 (vector-map!: add1 (vector-map!: add1 (vector 0 0 0)))) + 3) + ))) ;; ----------------------------------------------------------------------------- @@ -40,6 +47,6 @@ (compile-syntax stx))) (for ([rkt (in-list TEST-CASE*)]) - (check-exn #rx"vector::|Type Checker" + (check-exn #rx"out-of-bounds|Type Checker" (vector-eval rkt))) ) diff --git a/test/vector-pass.rkt b/test/vector-pass.rkt index c43b1c4..069a9be 100644 --- a/test/vector-pass.rkt +++ b/test/vector-pass.rkt @@ -159,6 +159,10 @@ (vector (vector 1) (vector 2 2) (vector 3 3 3) (vector 4 4 4 4))) (vector 1 2 3 4))) + (test-case "large vector" + (let-vector: ([v* (make-vector 200 #f)]) + (check-true (for/and ([v (in-vector (vector-map: not v*))]) v)))) + (test-case "higher-order map pass" (check-equal? ((lambda ([f : (-> (-> Symbol String) (Vectorof Symbol) (Vectorof String))]) diff --git a/vector.rkt b/vector.rkt index 06442f0..9d5cdb3 100644 --- a/vector.rkt +++ b/vector.rkt @@ -9,6 +9,7 @@ vector-ref: vector-set!: vector-map: + vector-map!: ;vector-append: ;vector->list ;vector->immutable-vector @@ -114,11 +115,16 @@ (syntax-parse stx [(_ f v:vector/length) #:with (i* ...) (for/list ([i (in-range (syntax-e #'v.length))]) i) + #:with f+ (gensym 'f) #:with v+ (syntax-property (if (small-vector-size? (syntax-e #'v.length)) - (syntax/loc stx (vector (f (unsafe-vector-ref v.expanded 'i*)) ...)) - (syntax/loc stx (build-vector 'v.length (lambda ([i : Integer]) - (unsafe-vector-ref v.expanded i))))) + (syntax/loc stx + (let ([f+ f]) + (vector (f+ (unsafe-vector-ref v.expanded 'i*)) ...))) + (syntax/loc stx + (let ([f+ f]) + (build-vector 'v.length (lambda ([i : Integer]) + (f+ (vector-ref: v.expanded i))))))) vector-length-key (syntax-e #'v.length)) (syntax/loc stx v+)] @@ -127,6 +133,23 @@ [(_ e* ...) (syntax/loc stx (vector-map e* ...))])) +(define-syntax (vector-map!: stx) + (syntax-parse stx + [(_ f v:vector/length) + #:with f+ (gensym 'f) + #:with v+ (syntax-property + #'(let ([f+ f]) + (for ([i (in-range 'v.length)]) + (unsafe-vector-set! v.expanded i (f+ (unsafe-vector-ref v.expanded i)))) + v.expanded) + vector-length-key + (syntax-e #'v.length)) + (syntax/loc stx v+)] + [_:id + (syntax/loc stx vector-map!)] + [(_ e* ...) + (syntax/loc stx (vector-map! e* ...))])) + ;; ----------------------------------------------------------------------------- (define-for-syntax (small-vector-size? n)