Created
July 7, 2020 12:27
-
-
Save vasilkosturski/4a6a527c35083024a69c33fdb8505fb3 to your computer and use it in GitHub Desktop.
clash-of-styles-operations-matrix-via-oop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace OOPvsFP | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var expression = new Addition( | |
new MyInt(3), | |
new Addition(new MyInt(4), new MyInt(5))); | |
var asString = expression.Stringify(); | |
var result = expression.Eval(); | |
Console.WriteLine($"{asString} = {result}"); // 3 + 4 + 5 = 12 | |
} | |
} | |
public interface IExpression | |
{ | |
int Eval(); | |
string Stringify(); | |
} | |
public class MyInt : IExpression | |
{ | |
private int Val { get; } | |
public MyInt(int val) | |
{ | |
Val = val; | |
} | |
public int Eval() => Val; | |
public string Stringify() => Val.ToString(); | |
} | |
public class Addition : IExpression | |
{ | |
private readonly IExpression _operand1; | |
private readonly IExpression _operand2; | |
public Addition(IExpression operand1, IExpression operand2) | |
{ | |
_operand1 = operand1; | |
_operand2 = operand2; | |
} | |
public int Eval() => _operand1.Eval() + _operand2.Eval(); | |
public string Stringify() => $"{_operand1.Stringify()} + {_operand2.Stringify()}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment