Created
July 6, 2020 11:25
-
-
Save jwscook/0886a04f5cedd176fe31b9d30b90d8e8 to your computer and use it in GitHub Desktop.
Use Cassette and ReplMaker to create REPL that has deterministic rand()
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
# Based on https://gist.github.com/mbauman/d0c27f40e25393ef050b87b4bb6a69dc | |
using Cassette, ReplMaker | |
Cassette.@context RandAlwaysHalf | |
randreturnshalf(s, x) = x | |
function randreturnshalf(s, f, fx) | |
if f == rand | |
return randreturnshalf(s, one(typeof(fx)) / 2) | |
else | |
return fx | |
end | |
end | |
Cassette.overdub(::RandAlwaysHalf, f, args...) = randreturnshalf("$f$args", f, f(args...)) | |
function parse_to_expr(s) | |
p = Meta.parse(s) | |
isa(p, Expr) && p.head in (:using, :import) && return p | |
quote $Cassette.@overdub($(RandAlwaysHalf()), $p) end | |
end | |
initrepl(parse_to_expr, | |
prompt_text="RandAlwaysHalf> ", | |
prompt_color = :cyan, | |
start_key='£', | |
mode_name="not rand mode") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment