Move list-set and list-update to unstable/list.

This commit is contained in:
Eric Dobson 2012-08-11 23:10:31 -07:00 committed by Sam Tobin-Hochstadt
parent dc3d06c7ae
commit 6e2e84664d
5 changed files with 56 additions and 24 deletions

View File

@ -360,14 +360,4 @@
(provide/cond-contract (struct Rep ([seq exact-nonnegative-integer?] (provide/cond-contract (struct Rep ([seq exact-nonnegative-integer?]
[free-vars (hash/c symbol? variance?)] [free-vars (hash/c symbol? variance?)]
[free-idxs (hash/c symbol? variance?)] [free-idxs (hash/c symbol? variance?)]
[stx (or/c #f syntax?)])) [stx (or/c #f syntax?)])))
[replace-syntax (Rep? syntax? . -> . Rep?)])
(define (replace-field val new-val idx)
(define-values (type skipped) (struct-info val))
(define maker (struct-type-make-constructor type))
(define flds (struct->list val))
(apply maker (list-set flds idx new-val)))
(define (replace-syntax rep stx)
(replace-field rep stx 3))

View File

@ -13,6 +13,7 @@
env? update-type/lexical env-map env-props replace-props) env? update-type/lexical env-map env-props replace-props)
racket/contract racket/match racket/contract racket/match
unstable/struct unstable/struct
unstable/list
"tc-metafunctions.rkt" "tc-metafunctions.rkt"
(for-syntax racket/base)) (for-syntax racket/base))

View File

@ -9,9 +9,6 @@ at least theoretically.
racket/require-syntax racket/provide-syntax racket/require-syntax racket/provide-syntax
racket/struct-info "timing.rkt") racket/struct-info "timing.rkt")
;; to move to unstable
(provide list-update list-set)
(provide (provide
;; optimization ;; optimization
optimize? optimize?
@ -157,16 +154,6 @@ at least theoretically.
[(_ hd ([i c] ...) . opts) [(_ hd ([i c] ...) . opts)
(define-struct hd (i ...) . opts)]))) (define-struct hd (i ...) . opts)])))
(define (list-update l i f)
(cond [(null? l) (error 'list-update "list not long enough" l i f)]
[(zero? i) (cons (f (car l)) (cdr l))]
[else (cons (car l) (list-update (cdr l) (sub1 i) f))]))
(define (list-set l k v)
(if (zero? k)
(cons v (cdr l))
(cons (car l) (list-set (cdr l) (sub1 k) v))))
(provide make-struct-info-self-ctor) (provide make-struct-info-self-ctor)
;Copied from racket/private/define-struct ;Copied from racket/private/define-struct

View File

@ -182,3 +182,23 @@
(provide/contract (provide/contract
[group-by (->* (procedure? list?) (#:key procedure?) [group-by (->* (procedure? list?) (#:key procedure?)
list?)]) list?)])
;; endobson added:
(define (list-update l i f)
(cond
[(zero? i) (cons (f (car l)) (cdr l))]
[else (cons (car l) (list-update (cdr l) (sub1 i) f))]))
(define (list-set l k v)
(list-update l k (lambda (_) v)))
(provide/contract
[list-update (->i [(list list?)
(index (list) (and/c (>=/c 0) (</c (length list))))
(updater (-> any/c any/c))]
[_ list?])]
[list-set (->i [(list list?)
(index (list) (and/c (>=/c 0) (</c (length list))))
(value any/c)]
[_ list?])])

View File

@ -175,4 +175,38 @@ determined by @racket[=?].
} }
@addition{Eric Dobson}
@defproc[(list-update [lst list?]
[index (and/c (>=/c 0) (</c (length lst)))]
[updater (-> any/c any/c)])
list?]{
Returns a list that is the same as @racket[lst] except at the specified index.
The element at the specified index is @racket[(updater (list-ref lst index))].
@examples[#:eval the-eval
(list-update '(zero one two) 1 symbol->string)
]
}
@defproc[(list-set [lst list?]
[index (and/c (>=/c 0) (</c (length lst)))]
[value any/c])
list?]{
Returns a list that is the same as @racket[lst] except at the specified index.
The element at the specified index is @racket[value].
@examples[#:eval the-eval
(list-set '(zero one two) 2 "two")
]
}
@close-eval[the-eval] @close-eval[the-eval]