Skip to content

Instantly share code, notes, and snippets.

@pragdave
Created February 21, 2018 19:57
Show Gist options
  • Save pragdave/e11c9ed58fdee8b0e59916af17175f93 to your computer and use it in GitHub Desktop.
Save pragdave/e11c9ed58fdee8b0e59916af17175f93 to your computer and use it in GitHub Desktop.
Code Extract: diet :"r" command
defp command("r", args, state) do
args = if String.starts_with?(args, ":") && String.contains?(args, " ") do
String.replace(args, " ", ", ", global: false)
else
args
end
args = if String.contains?(args, ",") && !String.starts_with?(args, "{") do
"{ #{args} }"
else
args
end
try do
{arg_val, _} = Code.eval_string(args)
{ result, stepper } = Diet.Stepper.run(stepper(state), arg_val)
IO.puts "\nResult: #{inspect result, pretty: true}"
put_in(state.steppers[state.current], stepper)
|> interact
rescue
e in [ CompileError, SyntaxError ] ->
IO.puts "error: #{e.description}\n"
interact(state)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment