Advertisement
vedranvinko

pattern_matching

Jul 13th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. // -> http://tomasp.net/blog/2015/csharp-pattern-matching/
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace PatternMatching
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             var expr = new Multiply
  16.             {
  17.                 Left = new Variable { Name = "x"},
  18.                 Right = new Add
  19.                 {
  20.                     Left = new Constant { Value = 1 },
  21.                     Right = new Constant { Value = 2}
  22.                 }
  23.             };
  24.  
  25.             Console.WriteLine(Format(expr));
  26.  
  27.             Console.WriteLine(Evaluate(expr, new Dictionary<string, int> { { "x", 4 } }));
  28.             Console.ReadLine();
  29.         }
  30.  
  31.         public static string Format(Exception e)
  32.         {
  33.             try
  34.             {
  35.                 throw e;
  36.             }
  37.             catch (Constant n)
  38.             {
  39.                 return n.Value.ToString();
  40.             }
  41.             catch (Variable v)
  42.             {
  43.                 return v.Name;
  44.             }
  45.             catch (Add a)
  46.             {
  47.                 return $"({Format(a.Left)} + {Format(a.Right)})";
  48.             }
  49.             catch (Multiply a)
  50.             {
  51.                 return $"{Format(a.Left)} * {Format(a.Right)}";
  52.             }
  53.         }
  54.  
  55.         public static int Evaluate(Exception e, IDictionary<string, int> vars)
  56.         {
  57.             int res;
  58.             try { throw e; }
  59.             catch (Constant n) { return n.Value; }
  60.             catch (Variable v) when (vars.TryGetValue(v.Name, out res)) { return res; }
  61.             catch (Variable _) { throw new ArgumentException("Variable not found!"); }
  62.             catch (Add a) { return Evaluate(a.Left, vars) + Evaluate(a.Right, vars); }
  63.             catch (Multiply a) { return Evaluate(a.Left, vars) + Evaluate(a.Right, vars); }
  64.         }
  65.     }
  66.  
  67.     public class Variable : Exception
  68.     {
  69.         public string Name { get; set; }
  70.     }
  71.  
  72.     public class Constant : Exception
  73.     {
  74.         public int Value { get; set; }
  75.     }
  76.  
  77.     public class Add : Exception
  78.     {
  79.         public Exception Left { get; set; }
  80.         public Exception Right { get; set; }
  81.     }
  82.  
  83.     public class Multiply : Exception
  84.     {
  85.         public Exception Left { get; set; }
  86.         public Exception Right { get; set; }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement