masala-parser icon indicating copy to clipboard operation
masala-parser copied to clipboard

Infix operators using Sweet

Open d-plaindoux opened this issue 8 years ago • 0 comments

Expressiveness can be increased using infix operators.

p1 <*> p2    // == p1.then(p2)
p1 <|> p2    // == p1.or(p2)
p1 >>= p2    // == p1.flatmap(p2)
p1 || p2     // == p1.chain(p2)

This can be achieved using Sweet.JS meta language.

operator <*> left 1 = (left, right) => #`${left}.then(${right})`;
operator <|> left 1 = (left, right) => #`${left}.or(${right})`;
operator >>= left 1 = (left, right) => #`${left}.flatmap(${right})`;
operator || left 1  = (left, right) => #`${left}.chain(${right})`;

d-plaindoux avatar Apr 13 '17 08:04 d-plaindoux