racket/collects/math/private/array/array-comprehension.rkt
Neil Toronto f2dc2027f6 Initial math library commit. The history for these changes is preserved
in the original GitHub fork:

  https://github.com/ntoronto/racket

Some things about this are known to be broken (most egregious is that the
array tests DO NOT RUN because of a problem in typed/rackunit), about half
has no coverage in the tests, and half has no documentation. Fixes and
docs are coming. This is committed now to allow others to find errors and
inconsistency in the things that appear to be working, and to give the
author a (rather incomplete) sense of closure.
2012-11-16 11:39:51 -07:00

65 lines
2.6 KiB
Racket

#lang racket/base
(require (for-syntax racket/base
syntax/parse)
typed/racket/base
"mutable-array.rkt"
"utils.rkt")
(provide for/array:
for*/array:
for/array
for*/array)
(define-syntax (base-for/array: stx)
(syntax-parse stx #:literals (:)
[(_ name:id for/vector:id #:shape ds-expr:expr (~optional (~seq #:fill fill-expr:expr))
(clause ...) (~optional (~seq : A:expr)) body:expr ...+)
(with-syntax ([(maybe-fill ...) (if (attribute fill-expr) #'(#:fill fill-expr) #'())]
[(maybe-type ...) (if (attribute A) #'(: A) #'())])
(syntax/loc stx
(let*: ([ds : User-Indexes ds-expr]
[ds : Indexes (check-array-shape
ds (λ () (raise-argument-error 'name "Indexes" ds)))])
(define vs (for/vector #:length (array-shape-size ds) maybe-fill ...
(clause ...) maybe-type ... body ...))
(unsafe-mutable-array ds vs))))]
[(_ name:id for/vector:id (clause ...) (~optional (~seq : A:expr)) body:expr ...+)
(with-syntax ([(maybe-type ...) (if (attribute A) #'(: A) #'())])
(syntax/loc stx
(let ()
(define vs (for/vector (clause ...) maybe-type ... body ...))
(define ds ((inst vector Index) (vector-length vs)))
(unsafe-mutable-array ds vs))))]))
(define-syntax-rule (for/array: e ...)
(base-for/array: for/array: for/vector: e ...))
(define-syntax-rule (for*/array: e ...)
(base-for/array: for*/array: for*/vector: e ...))
(define-syntax (base-for/array stx)
(syntax-parse stx
[(_ name:id for/vector:id #:shape ds-expr:expr (~optional (~seq #:fill fill-expr:expr))
(clause ...) body:expr ...+)
(with-syntax ([(maybe-fill ...) (if (attribute fill-expr) #'(#:fill fill-expr) #'())])
(syntax/loc stx
(let* ([ds ds-expr]
[ds (check-array-shape
ds (λ () (raise-argument-error 'name "Indexes" ds)))])
(define vs (for/vector #:length (array-shape-size ds) maybe-fill ...
(clause ...) body ...))
(unsafe-mutable-array ds vs))))]
[(_ name:id for/vector:id (clause ...) body:expr ...+)
(syntax/loc stx
(let ()
(define vs (for/vector (clause ...) body ...))
(define ds ((inst vector Index) (vector-length vs)))
(unsafe-mutable-array ds vs)))]))
(define-syntax-rule (for/array e ...)
(base-for/array for/array for/vector e ...))
(define-syntax-rule (for*/array e ...)
(base-for/array for*/array for*/vector e ...))