Last active
July 7, 2020 08:19
-
-
Save jwscook/ab17ae4298b64aad44a338c444d3d153 to your computer and use it in GitHub Desktop.
@replacerand macro to replace calls to rand() by a given function e.g. x->0.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A macro `@replacerand` that replaces the value returned by a call to `rand` with something else. | |
""" | |
using Cassette | |
Cassette.@context ReplaceRand | |
Cassette.overdub(ctx::ReplaceRand, fn::typeof(rand), args...) = ctx.metadata(args...) | |
replacerand(f, replacement=(x...) -> 0.5) = Cassette.overdub(ReplaceRand(metadata=replacement), f) | |
macro replacerand(replacement, ex) | |
:(replacerand(() -> $(esc(ex)), $(esc(replacement)))) | |
end | |
# define what rand should be replaced by | |
replacement() = 0.5 | |
replacement(n) = zeros(n) .+ 0.5 | |
replacement(_, n) = zeros(n) .+ 0.5 | |
@replacerand replacement rand() | |
function foo() | |
return rand() | |
end | |
@replacerand replacement foo() | |
module Bar | |
baz() = rand() | |
end | |
using .Bar | |
@replacerand replacement Bar.baz() | |
@replacerand replacement rand(2) | |
@replacerand replacement rand((2, 3)) | |
using Random | |
rng = Random.MersenneTwister() | |
@replacerand replacement rand(rng, 2) | |
@replacerand replacement rand(rng, (2, 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a simple way to do this:
then you can call whatever code you want
If you want to customize the value, the metadata can be just a
Float64
with
After you declare the context, you have to instantiate it and pass your instance into
overdub
. The metadata field can be anything you want, it doesn't have to be declared as a struct (unless you want it to be).