[your code here] Pure RPN calculator

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 26 02:45:07 PDT 2017


On 26.07.2017 04:37, Seb wrote:
> On Tuesday, 25 July 2017 at 21:13:54 UTC, Max Haughton wrote:
>> Semi-Functional/pure RPN calculator: https://run.dlang.io/is/JGkBZx
>>
>> This is probably too long, but it demonstrates the compiler enforced 
>> safety and purity (State is passed through the fold), while also 
>> showing off the higher level parts of Phobos (Use of fold).
> 
> Max, this is a great example!
> However, as you noticed it's a bit long and in fact in its current state 
> it wouldn't look good:
> 
> http://imgur.com/a/KwczM
> 
> If there's no chance to make it shorter,

import std.stdio,std.string,std.algorithm,std.conv;
void main(){
     readln.split.fold!((stack,op){
         switch(op){
             static foreach(c;"+-*/") case [c]:
                 return stack[0..$-2]~mixin("stack[$-2] "~c~" stack[$-1]");
             default: return stack~op.to!real;
         }
     })((real[]).init).writeln;
}



More information about the Digitalmars-d mailing list