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