Skip to content

Instantly share code, notes, and snippets.

@jwscook
Created July 6, 2020 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwscook/0886a04f5cedd176fe31b9d30b90d8e8 to your computer and use it in GitHub Desktop.
Save jwscook/0886a04f5cedd176fe31b9d30b90d8e8 to your computer and use it in GitHub Desktop.
Use Cassette and ReplMaker to create REPL that has deterministic rand()
# 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