Skip to content

Instantly share code, notes, and snippets.

@vasilkosturski
Created July 7, 2020 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasilkosturski/4a6a527c35083024a69c33fdb8505fb3 to your computer and use it in GitHub Desktop.
Save vasilkosturski/4a6a527c35083024a69c33fdb8505fb3 to your computer and use it in GitHub Desktop.
clash-of-styles-operations-matrix-via-oop
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