Patterns
Patterns are a way to create complex guard routines that can be applied directly to expressions or more commonly they are used as parameter patterns for sequences (the two facets combined are known as routines).
Conceptually, they are used for creating interfaces, classes of objects, and for routine parameters. To do this, you can declare and thus name these patterns. This is known as a contract. There are two types of contracts: investigative and transformative. An investigative contract returns a truth value if the pattern is matched. A transformative contract will replace the value by applying some sequential transformation.
Transformative contracts use the ^
suffix and they are either applied like routines or placed in front of parameter names inside of parameter patterns. The contract will be invoked with the given value and they run this value through a sequence which theoretically will transform the value into what the contract author intends.
Investigative contracts use the ?
suffix and they are applied in the same fashion as transformative contracts however they fail if the pattern doesn't match the given value.
Despite being mostly a symbolic language, named/categorical values are both numerous and, for lack of a term, "exist in the realm of the named" meaning that they should have names because they are named things and not syntax. Therefore, Quarrel provides some named contracts and builtins. Although not being a true tenet, Quarrel does intend to be capable of modeling programs without excessive boilerplate to create a platform that is usable.
Patterns not only provide a means to understand incoming data, they also indicate the structure of your data and have the ability to generate data according to the specification. When you pass an expression through a pattern, you get a context where the names provided in the pattern are patched to the things you provided when you invoked the pattern.
The base expression type of the pattern becomes the aggregate that is formed when the data is
#
How to...#
create a patternPatterns are first-class and can be passed around and defined anonymously
#
create a contractInvestigative contracts are just patterns that return truth values when given arguments. Transformative sequences can be as minimal as a simple expression (like above) or full-fledged routines.
#
apply a patternPatterns are applied within parameter patterns or structural patterns. You then apply a parameter pattern by...
- ...combining it with a sequence via the
=>
operator to become a functional routine. - ...combining it with a table via the
->
operator to become an iterative routine. - ...supplying it to a structural sequence of elements to become a record.
#
add an additional investigative contract pattern#
Pattern Sub-LanguageInside of patterns, many declarative operators describe the same features they would outside of patterns. There are some operators specific to this sub-language.
&
This operator creates a multi-faceted pattern where two connected patterns will now be subsequently tested against some datum and only approves the datum as fulfilling those patterns if both patterns are valid for the datum.
|
#
This operator creates a multi-faceted pattern where two connected patterns will be tested against some datum and unlike &
, if only one pattern matches the data, it's a pass.
->
#
This operator takes a datum (with or without a preceding contractual obligation) and (when the contract passes) applies an additional check on the datum via a sequence. Keep in mind that Quarrel allows patterns to directly compare literal values in addition to just other patterns but when you need to do some calculations or check some internal parameter, then you reach for @
. You can use the _
operator to indicate a single parameter routine which improves readability:
<-
#
This operator applies the sequence to the argument first and then performs the contractual check. Here, the return value is the new parameter, replacing the argument provided.
...
#
When fixed to a container name, this operator creates a list of values that encompasses a slice of the arguments provided. It allows parameter sequences to be variable length. When given no container name, it implies that this slice of values is not needed.
_
#
Signifies an element of the pattern that is discarded (though you can still test it against contracts). Interestingly, the value is not truly discarded but merely left unnamed. Inside of sequences, you can access a default parameter list; a single parameter is concidentally accessed with the same operator, _
. However, when multiple parameters are involved, you must use \1
, \2
, and so on.
?
#
The ?
operator takes some pattern or contract and requires it to pass ("obligates" it) to some container or value. This is the main way of using contracts and also can be used with pattern literals.
#
^
#
#
OperatorsBinary and unary operations that act specifically on Patterns.
->
#
This operator will do several difference types of transformations depending on the receiver. In the case of being received by a sequence, the supplying sequence is provided as arguments to the receiving sequence. In the case of being received by a pattern, the supplying sequence is tested against the pattern to determine if it matches.
#
ExamplesThe |-|
builtin container is for input and output and in this case, it prints whatever value is provided to it to the STDOUT.
<-
#
Take the receiving values as arguments to an invocation of the sequence.
#
Examples+
#
Invokes any sequence provided as an argument to the +
operator.
#
Examples==
#
Invokes any sequence provided as an argument to the ==
operator.
#
Examples<>
#
Invokes any sequence provided as an argument to the <>
operator.
#
Examples>>
#
Invokes any sequence provided as an argument to the >>
operator.
#
Examples<<
#
Invokes any sequence provided as an argument to the <<
operator.
#
Examples>=
#
Invokes any sequence provided as an argument to the >=
operator.
#
Examples<=
#
Invokes any sequence provided as an argument to the <=
operator.
#
Examples***
#
Invoke the sequence as many times as indicated by the other operand.
#
Examples#
Method OperatorsMethod operations that are performed on Sequence containers.
[+]
#
Add a new routine to the container
#
Examples[:]
#
Clone the container of sequences
#
Examples[_:]
#
Cloned list of all parameter patterns
#
Examples[?]
#
Check for a matching parameter pattern
#
Examples[-]
#
Change a routine by looking up it's parameter pattern
#
Examples[@]
#
Count of how many routines are contained