random: Get the right name without run-time overhead.

This commit is contained in:
Vincent St-Amour 2016-01-03 13:53:31 -06:00
parent 4509430063
commit cc79e2241c

View File

@ -112,36 +112,36 @@
y)))) y))))
(begin-encourage-inline (begin-encourage-inline
(define-values (-random) ; more featureful than #%kernel's `random` (define-values (-random) ; more featureful than #%kernel's `random`
(procedure-rename (let ([random ; to get the right name
(case-lambda (case-lambda
[() (random)] ; no args, random float [() (random)] ; no args, random float
[(x) ; one arg, either random float with prng, or random integer [(x) ; one arg, either random float with prng, or random integer
;; can just pass through to #%kernel's `random`, which will do the ;; can just pass through to #%kernel's `random`, which will do the
;; necessary checking ;; necessary checking
(random x)] (random x)]
[(x y) [(x y)
;; two args, either max and prng, or min and max ;; two args, either max and prng, or min and max
(cond [(exact-positive-integer? y) ; min and max case (cond [(exact-positive-integer? y) ; min and max case
(enforce-random-int-range x) (enforce-random-int-range x)
(enforce-random-int-range y) (enforce-random-int-range y)
(enforce-greater x y) (enforce-greater x y)
(+ x (random (- y x)))] (+ x (random (- y x)))]
[(pseudo-random-generator? y) ; int and prng case [(pseudo-random-generator? y) ; int and prng case
(enforce-random-int-range x) (enforce-random-int-range x)
(random x y)] (random x y)]
[else [else
(raise-argument-error (raise-argument-error
'random 'random
"(or/c (integer-in 1 4294967087) pseudo-random-generator?)" "(or/c (integer-in 1 4294967087) pseudo-random-generator?)"
y)])] y)])]
[(min max prng) ; three args: min, max, and prng [(min max prng) ; three args: min, max, and prng
(enforce-random-int-range min) (enforce-random-int-range min)
(enforce-random-int-range max) (enforce-random-int-range max)
(enforce-greater min max) (enforce-greater min max)
(unless (pseudo-random-generator? prng) (unless (pseudo-random-generator? prng)
(raise-argument-error 'random "pseudo-random-generator?" prng)) (raise-argument-error 'random "pseudo-random-generator?" prng))
(+ min (random (- max min) prng))]) (+ min (random (- max min) prng))])])
'random))) random)))
(define-values (new:collection-path) (define-values (new:collection-path)
(let ([collection-path (new-lambda (collection (let ([collection-path (new-lambda (collection