Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 25, 2017 [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
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). |
July 26, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Haughton | 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). Answer to this https://forum.dlang.org/post/dhuvztizmqysqsepmpgh@forum.dlang.org. This is exactly what the author looked for. |
July 26, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Haughton | 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, my suggestion considering that there are two other ideas in the queue with a similar length problem: https://github.com/dlang/dlang.org/pull/1759 https://github.com/dlang/dlang.org/pull/1762 would be to find a new home for such "one-page" D example. We plan to restructure the DTour (tour.dlang.org) anyways, so how about adding a new section like "D in action" to it? -> I have opened a discussion: https://github.com/dlang-tour/english/issues/194 |
July 26, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | 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; } |
July 26, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | 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; } |
July 26, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On 26.07.2017 11:45, Timon Gehr wrote:
> 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;
> }
>
(Sorry for the double post. Internet connection problem.)
|
July 26, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Wednesday, 26 July 2017 at 09:46:45 UTC, Timon Gehr wrote:
>> 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;
What does "case [c]:" mean?
|
July 26, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Iakh | On 26-07-17 16:40, Iakh wrote: > On Wednesday, 26 July 2017 at 09:46:45 UTC, Timon Gehr wrote: >>> 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; > > What does "case [c]:" mean? > In the static foreach c is a `immutable char` by putting it between [ and ] you create an array of immutable characters (string). -- Mike Wey |
July 26, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Wey | On Wednesday, 26 July 2017 at 17:12:00 UTC, Mike Wey wrote:
> On 26-07-17 16:40, Iakh wrote:
>> On Wednesday, 26 July 2017 at 09:46:45 UTC, Timon Gehr wrote:
>>>> 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;
>>
>> What does "case [c]:" mean?
>>
>
> In the static foreach c is a `immutable char` by putting it between [ and ] you create an array of immutable characters (string).
That's why some comments in the code would go a long way in lifting such issues.
|
July 28, 2017 Re: [your code here] Pure RPN calculator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Wednesday, 26 July 2017 at 09:45:07 UTC, Timon Gehr wrote: > 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; > } That's pretty great! Submitted: https://github.com/dlang/dlang.org/pull/1848 |
Copyright © 1999-2021 by the D Language Foundation