create account

The expression tree challenge follow up in C# by woz.software

View this thread on: hive.blogpeakd.comecency.com
· @woz.software · (edited)
$0.13
The expression tree challenge follow up in C#
![](https://i.imgur.com/rbI6ChF.jpg)

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
👍  , , , , , , , , ,
properties (23)
authorwoz.software
permlinkthe-expression-tree-challenge-follow-up-in-c
categoryprogramming
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"}
created2017-09-08 08:22:21
last_update2017-09-08 08:54:09
depth0
children6
last_payout2017-09-15 08:22:21
cashout_time1969-12-31 23:59:59
total_payout_value0.115 HBD
curator_payout_value0.016 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,466
author_reputation2,321,910,395,519
root_title"The expression tree challenge follow up in C#"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,230,309
net_rshares44,863,165,878
author_curate_reward""
vote details (10)
@jonas160 ·
great feed
👍  ,
properties (23)
authorjonas160
permlinkre-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t153452636z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-08 08:26:39
last_update2017-09-08 08:26:39
depth1
children2
last_payout2017-09-15 08:26:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation1,402,607,389,289
root_title"The expression tree challenge follow up in C#"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,230,546
net_rshares5,227,646,094
author_curate_reward""
vote details (2)
@woz.software ·
Thanks :)

Just seen your coffee ring drawings. You have another follower :)
properties (22)
authorwoz.software
permlinkre-jonas160-re-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t084219603z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-08 08:42:21
last_update2017-09-08 08:42:21
depth2
children1
last_payout2017-09-15 08:42:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length76
author_reputation2,321,910,395,519
root_title"The expression tree challenge follow up in C#"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,231,572
net_rshares0
@jonas160 ·
yeah, thanks man
properties (22)
authorjonas160
permlinkre-wozsoftware-re-jonas160-re-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t170749256z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-08 09:59:36
last_update2017-09-08 09:59:36
depth3
children0
last_payout2017-09-15 09:59:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length16
author_reputation1,402,607,389,289
root_title"The expression tree challenge follow up in C#"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,236,931
net_rshares0
@phil-coding ·
This is really interesting Thank you! Your work is really add up to the community of programming
👍  ,
properties (23)
authorphil-coding
permlinkre-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t091011849z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-08 09:11:48
last_update2017-09-08 09:11:48
depth1
children0
last_payout2017-09-15 09:11:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length96
author_reputation4,773,758,818
root_title"The expression tree challenge follow up in C#"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,233,639
net_rshares5,816,682,257
author_curate_reward""
vote details (2)
@steemitboard ·
Congratulations @woz.software! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/voted.png)](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)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-wozsoftware-20170908t113117000z
categoryprogramming
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-09-08 11:31:15
last_update2017-09-08 11:31:15
depth1
children0
last_payout2017-09-15 11:31:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length703
author_reputation38,975,615,169,260
root_title"The expression tree challenge follow up in C#"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,243,270
net_rshares0
@woz.software · (edited)
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;
}

...
```
properties (22)
authorwoz.software
permlinkre-wozsoftware-the-expression-tree-challenge-follow-up-in-c-20170908t084141169z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-09-08 08:41:42
last_update2017-09-08 08:44:33
depth1
children0
last_payout2017-09-15 08:41:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length904
author_reputation2,321,910,395,519
root_title"The expression tree challenge follow up in C#"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id14,231,517
net_rshares0