Pattern Matching - shindakioku/fpfantasy GitHub Wiki

Pattern Matching

matching :: List -> Function(Data) OR matching :: Any(Pattern) -> Any(Handler) -> ... -> Function(Data)

var a = matching([
  [0, () => 0],
  [1, 1],
  [2, () => () => 2],
  [default_, 'default'] // default_ - from matching (fpfantasy) = 'default'
]);

print('0 = ${a(0)}, 1 = ${a(1)}, 2 = ${a(2)()}, 3 = ${a(3)}');
// 0 = 0, 1 = 1, 2 = 2, 3 = 'default'

And the same with functions

var a = matching(0, () => 0)
                (1, 1)
                (2, () => () => 2)
                (default_, 'default');

print('0 = ${a(0)}, 1 = ${a(1)}, 2 = ${a(2)()}, 3 = ${a(3)}');
// 0 = 0, 1 = 1, 2 = 2, 3 = 'default'