Skip to content

Commit 92fee51

Browse files
authored
Merge pull request #3698 from Imperatorn/patch-10
Update index.dd Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents f4b4e99 + e21f1b2 commit 92fee51

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

index.dd

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,17 @@ void main()
234234
{
235235
import std.stdio, std.string, std.algorithm, std.conv;
236236

237-
// Reduce the RPN expression using a stack
238-
readln.split.fold!((stack, op)
237+
// arr is real[] and sym is the current symbol
238+
readln.split.fold!((arr, sym)
239239
{
240-
switch (op)
241-
{
242-
// Generate operator switch cases statically
243-
static foreach (c; "+-*/")
244-
case [c]:
245-
return stack[0 .. $ - 2] ~
246-
mixin("stack[$ - 2] " ~ c ~
247-
" stack[$ - 1]");
248-
default: return stack ~ op.to!real;
249-
}
240+
static foreach (c; "+-*/")
241+
if (sym == [c])
242+
// replace the last 2 elements with the binary op
243+
return arr[0 .. $-2] ~
244+
mixin("arr[$-2] " ~ c ~ " arr[$-1]");
245+
246+
// sym must be a number
247+
return arr ~ sym.to!real;
250248
})((real[]).init).writeln;
251249
}
252250
----

0 commit comments

Comments
 (0)