 In my earlier post "[Some coding fun - The expression tree challenge :)](https://steemit.com/programming/@woz.software/some-coding-fun-the-expression-tree-challenge)" I showed an expression tree structure in F#, a functional first .NET language. Nobody picked up the gauntlet and gave an implementation in another language so I thought I would show the same functionality from an OO perspective instead of a functional one. Again would be interesting to see this in another language like Python etc In C# it made more sense to use inheritance of an abstract base. The parseTree function from the F# version has been replaced with virtual dispatch via the run method. I did ponder if it was worth adding an extra abstract base for the compuation style expressions but it gained nothing and just added more noise to the code. ``` public abstract class Expression { public abstract float Run(); } public class Value : Expression { private float _value; public Value(float value){_value = value;} public override float Run() => _value; } public class Add : Expression { private Expression _lhs; private Expression _rhs; public Add(Expression lhs, Expression rhs) { _lhs = lhs; _rhs = rhs; } public override float Run() => _lhs.Run() + _rhs.Run(); } public class Subtract : Expression { private Expression _lhs; private Expression _rhs; public Subtract(Expression lhs, Expression rhs) { _lhs = lhs; _rhs = rhs; } public override float Run() => _lhs.Run() - _rhs.Run(); } public class Multiply : Expression { private Expression _lhs; private Expression _rhs; public Multiply(Expression lhs, Expression rhs) { _lhs = lhs; _rhs = rhs; } public override float Run() => _lhs.Run() * _rhs.Run(); } public class Divide : Expression { private Expression _lhs; private Expression _rhs; public Divide(Expression lhs, Expression rhs) { _lhs = lhs; _rhs = rhs; } public override float Run() => _lhs.Run() / _rhs.Run(); } var tree = new Add( new Value(5.0), new Divide(new Value(3.0), new Value(1.5))); var result = tree.Run(); ``` For OO code it is not too bad but still many more lines of noise compared to the F# version. Happy coding, hope I get some other examples this time :) Woz
author | woz.software |
---|---|
permlink | the-expression-tree-challenge-follow-up-in-c |
category | programming |
json_metadata | {"tags":["programming","computer","tutorial","technology","challenge"],"image":["https://i.imgur.com/rbI6ChF.jpg"],"links":["https://steemit.com/programming/@woz.software/some-coding-fun-the-expression-tree-challenge"],"app":"steemit/0.1","format":"markdown"} |
created | 2017-09-08 08:22:21 |
last_update | 2017-09-08 08:54:09 |
depth | 0 |
children | 6 |
last_payout | 2017-09-15 08:22:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.115 HBD |
curator_payout_value | 0.016 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,466 |
author_reputation | 2,321,910,395,519 |
root_title | "The expression tree challenge follow up in C#" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 14,230,309 |
net_rshares | 44,863,165,878 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
drifter1 | 0 | 9,288,453,294 | 100% | ||
lloyddavis | 0 | 6,281,550,987 | 30% | ||
steemitboard | 0 | 155,657,624 | 1% | ||
shantamcbain | 0 | 665,254,833 | 100% | ||
head10 | 0 | 321,104,046 | 100% | ||
pilcrow | 0 | 22,524,455,482 | 50.51% | ||
qed | 0 | 2,883,582,552 | 100% | ||
olayinka | 0 | 619,179,824 | 100% | ||
phil-coding | 0 | 1,131,603,763 | 100% | ||
kbcndh6b | 0 | 992,323,473 | 100% |
great feed
author | jonas160 |
---|---|
permlink | re-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t153452636z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-09-08 08:26:39 |
last_update | 2017-09-08 08:26:39 |
depth | 1 |
children | 2 |
last_payout | 2017-09-15 08:26:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 10 |
author_reputation | 1,402,607,389,289 |
root_title | "The expression tree challenge follow up in C#" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 14,230,546 |
net_rshares | 5,227,646,094 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
woz.software | 0 | 5,051,961,746 | 100% | ||
jonas160 | 0 | 175,684,348 | 100% |
Thanks :) Just seen your coffee ring drawings. You have another follower :)
author | woz.software |
---|---|
permlink | re-jonas160-re-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t084219603z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-09-08 08:42:21 |
last_update | 2017-09-08 08:42:21 |
depth | 2 |
children | 1 |
last_payout | 2017-09-15 08:42:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 76 |
author_reputation | 2,321,910,395,519 |
root_title | "The expression tree challenge follow up in C#" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 14,231,572 |
net_rshares | 0 |
yeah, thanks man
author | jonas160 |
---|---|
permlink | re-wozsoftware-re-jonas160-re-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t170749256z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-09-08 09:59:36 |
last_update | 2017-09-08 09:59:36 |
depth | 3 |
children | 0 |
last_payout | 2017-09-15 09:59:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 16 |
author_reputation | 1,402,607,389,289 |
root_title | "The expression tree challenge follow up in C#" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 14,236,931 |
net_rshares | 0 |
This is really interesting Thank you! Your work is really add up to the community of programming
author | phil-coding |
---|---|
permlink | re-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t091011849z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-09-08 09:11:48 |
last_update | 2017-09-08 09:11:48 |
depth | 1 |
children | 0 |
last_payout | 2017-09-15 09:11:48 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 96 |
author_reputation | 4,773,758,818 |
root_title | "The expression tree challenge follow up in C#" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 14,233,639 |
net_rshares | 5,816,682,257 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
woz.software | 0 | 4,708,290,879 | 100% | ||
phil-coding | 0 | 1,108,391,378 | 100% |
Congratulations @woz.software! You have completed some achievement on Steemit and have been rewarded with new badge(s) : [](http://steemitboard.com/@woz.software) Award for the number of upvotes received Click on any badge to view your own Board of Honor on SteemitBoard. For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard) If you no longer want to receive notifications, reply to this comment with the word `STOP` > By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
author | steemitboard |
---|---|
permlink | steemitboard-notify-wozsoftware-20170908t113117000z |
category | programming |
json_metadata | {"image":["https://steemitboard.com/img/notifications.png"]} |
created | 2017-09-08 11:31:15 |
last_update | 2017-09-08 11:31:15 |
depth | 1 |
children | 0 |
last_payout | 2017-09-15 11:31:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 703 |
author_reputation | 38,975,615,169,260 |
root_title | "The expression tree challenge follow up in C#" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 14,243,270 |
net_rshares | 0 |
Actually the intermediate Operation abstract does clean it up some more. Basics of that refactor are... ``` public class Operation : Expression { private Expression _lhs; private Expression _rhs; public Operation(Expression lhs, Expression rhs) { _lhs = lhs; _rhs = rhs; } protected abstract float CalculateResult(float lhs, float rhs); public override float Run() => CalculateResult(_lhs.Run(), _rhs.Run()); } public class Subtract : Operation { public Subtract(Expression lhs, Expression rhs) : base(lhs, rhs) {} protected override float CalculateResult(float lhs, float rhs) => lhs + rhs; } public class Multiply : Operation { public Multiply(Expression lhs, Expression rhs) : base(lhs, rhs) {} protected override float CalculateResult(float lhs, float rhs) => lhs * rhs; } ... ```
author | woz.software |
---|---|
permlink | re-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t084141169z |
category | programming |
json_metadata | {"tags":["programming"],"app":"steemit/0.1"} |
created | 2017-09-08 08:41:42 |
last_update | 2017-09-08 08:44:33 |
depth | 1 |
children | 0 |
last_payout | 2017-09-15 08:41:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 904 |
author_reputation | 2,321,910,395,519 |
root_title | "The expression tree challenge follow up in C#" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 14,231,517 |
net_rshares | 0 |