[2020][ruby][18] refactor

pull/2/head
Alpha Chen 4 years ago
parent 5ecce65bb5
commit 5bcd40bef6

@ -35,18 +35,18 @@ def evaluate(input)
ss = StringScanner.new(input)
until ss.eos?
ss.scan(/\s*/)
case
stack << case
when n = ss.scan(/\d+/)
stack << stack.pop[n.to_i]
stack.pop[n.to_i]
when ss.scan(/\+/)
stack << ->(x) { stack.pop + x }
->(x) { stack.pop + x }
when ss.scan(/\*/)
stack << ->(x) { stack.pop * x }
->(x) { stack.pop * x }
when ss.scan(/\(/)
stack << ->(x) { x }
->(x) { x }
when ss.scan(/\)/)
n = stack.pop
stack << stack.pop[n]
stack.pop[n]
else
fail
end

Loading…
Cancel
Save