Advanced logic

Create logic functions combining patterns to achieve a more granular matching.


Go back

It's only logical

Complex conditions are no match for Myra.

Apart from checking a single pattern each time, Myra allows to compose patterns with logic operators. In such a case, the action will only execute if the resulting logic function is true.

These are the available logic combinators:

And

Is true if both patterns are true.

Input A Input B Output
false false false
false true false
true false false
true true true

Or

Is true if one or both patterns are true.

Input A Input B Output
false false false
false true true
true false true
true true true

Not

Is true if the pattern is false.

Input Output
false true
true false

Xor

Is true if both patterns have a different value.

Input A Input B Output
false false false
false true true
true false true
true true false

Xnor

Is true if both patterns have the same value.

Input A Input B Output
false false true
false true false
true false false
true true true

These combinators can be used like this:

person.Age.Match(Patterns.IsGreaterOrEqual(18)
                .And(Patterns.IsLesserThan(30)),
            p => Console.WriteLine("Person is within age range."));
        
dog.Match(isHusky.Not(), d => Console.WriteLine($"{d.Name} isn't a husky."));

By combining the provided operations, it is possible to express any function, like:

  • P1.Nand(P2) = P1.And(P2).Not()
  • P1.Nor(P2) = P1.Or(P2).Not()

In fact, the default Xnor function is actually implemented combining basic operations:

  • P1.Xnor(P2) = P1.And(P2).Or(P1.Or(P2).Not())

You're all done!

Myra has no more secrets from you now.


Go back