// sequence can contain operators (+, -, *, /), Doubles or string-encoded doubles srecord CalcRPN(L sequence) is Steppable { int i; new DoubleList stack; run { stepAll(this); } bool done() { ret i >= l(sequence); } public bool step() { if (done()) false; O o = sequence.get(i++); if (eq(o, "*")) perform((a, b) -> a * b); else if (eq(o, "/")) perform((a, b) -> doubleRatio(a, b)); else if (eq(o, "+")) perform((a, b) -> a + b); else if (eq(o, "-")) perform((a, b) -> a - b); else if (eq(o, "u-")) perform(a -> -a); else if (o cast S) handleUnknownString(o); else stack.add(toDouble(o)); true; } void handleUnknownString(S s) { stack.add(toDouble(trim(s))); } void perform(IF2 f) { var arg2 = popLast(stack); var arg1 = popLast(stack); stack.add(f.get(arg1, arg2)); } void perform(IF1 f) { stack.add(f.get(popLast(stack))); } double get() { run(); assertEquals(1, l(stack)); ret last(stack); } }