Table of contents

Macros

Show example of custom pattern matching.

const { match } = require("assert");

match(val)(
  string >> (left + right),
  [a, ...b] >> (a + b),
  [head | tail] >> (a + b),
  "test" >> (left / right),
  (val >= 10) >> "tset",
  Test >>
    match(Test)(
      BinaryExpression(left, "+", right) >> (evaluate(left) + evaluate(right)),
      BinaryExpression(left, "-", right) >>
        match(right)(
          (BinaryExpression(left, "+", right) >> (left + right),
          BinaryExpression(left, "-", right) >> (evaluate(left) - evaluate(right)),
          UnaryExpression(left, "/", right) >> (evaluate(left) / evaluate(right)),
          Test >> ok)
        ),
      UnaryExpression(left, "/", right) >> (left / right),
      Test >> ok
    ),
  Test >> ok,
  (a, b, "test", 65) >> (a + b + c + d),
  { type: "Program", operator: "+" } >> rest
);

const quickSort = createMatcher(
  (_, quickSort) => (
    [] >> [], [x, ...xs] >> [...quickSort(xs.filter((a) => a <= x)), x, ...quickSort(xs.filter((a) => a > x))]
  )
);

quickSort([3, 34534, 4, 5, 6, 6]);

(async function f() {
  const response = await fetch(something);
  const messageOk = "Data successfully loaded";

  createMatcher(
    (response) => (
      { status: 200, headers: { "Content-Length": contentLength } } >> handleSuccess(response, contentLength),
      { status: 400 } >> "Endpoint not found",
      (response.status > 400) >> "Error!",
      _ >> "Otherwise"
    ),
    { messageOk, handleSuccess }
  )(response);
})();

// quicksort1 :: (Ord a) => [a] -> [a]
// quicksort1 [] = []
// quicksort1 (x:xs) =
//   let smallerSorted = quicksort1 [a | a <- xs, a <= x]
//       biggerSorted = quicksort1 [a | a <- xs, a > x]
//   in  smallerSorted ++ [x] ++ biggerSorted