Module forma.multipattern
A class contain a collection of pattern objects.
Many pattern operations generate a set of patterns. This class aims to provide a convenient collection with some common methods for handling them.
Functions
__len (mp) | Multipattern length. |
new (components) | Create a new multipattern from a list of patterns. |
clone (mp) | Clone the multipattern. |
insert (mp, ip) | Insert a pattern into the multipattern. |
n_components (mp) | Count the number of components in a multipattern. |
map (mp, fn) | Map a function over all patterns in this multipattern. |
filter (mp, fn) | Filter out sub-patterns according to a predicate. |
apply (mp, method, ...) | Apply a named method to each pattern, returning a new multipattern. |
union_all (mp) | Union all sub-patterns into a single pattern. |
Utilities
print (mp, chars, domain) | Print a multipattern. |
Functions
- __len (mp)
-
Multipattern length.
Returns the number of components in the multipattern.
Parameters:
- mp
- new (components)
-
Create a new multipattern from a list of patterns.
Parameters:
- components an array of pattern objects.
Returns:
-
a new multipattern containing those patterns.
- clone (mp)
-
Clone the multipattern.
Parameters:
- mp multipattern to clone.
Returns:
-
the cloned multipattern.
- insert (mp, ip)
-
Insert a pattern into the multipattern.
Parameters:
- mp multipattern to be operated upon.
- ip the new pattern to insert.
Returns:
-
the new multipattern.
- n_components (mp)
-
Count the number of components in a multipattern.
Parameters:
- mp the multipattern to count.
Returns:
-
the number of components.
- map (mp, fn)
-
Map a function over all patterns in this multipattern.
Calls
fn(pattern, index)
for each sub-pattern, returning a new multipattern of their results.Example: ``
local bigger = mp:map(function(p) return p:enlarge(2) end)
``Parameters:
- mp the multipattern upon which to map the function.
- fn
a function taking
(pattern, index)
and returning a new pattern.
Returns:
-
a new multipattern of the mapped results.
- filter (mp, fn)
-
Filter out sub-patterns according to a predicate.
Keeps only those patterns for which
predicate(pattern) == true
.Example: ``
local bigSegs = mp:filter(function(p) return p:size() >= 10 end)
``Parameters:
- mp the multipattern upon which to filter.
- fn
a function
(pattern) -> boolean
.
Returns:
-
a new multipattern containing only the sub-patterns passing the test.
- apply (mp, method, ...)
-
Apply a named method to each pattern, returning a new multipattern.
This is an alternative to
:map(...)
for calling an existing pattern method by name on all sub-patterns. You may also supply arguments to that method.Example: ``
local translated = mp:apply("translate", 10, 5) -- calls p:translate(10,5) on each pattern p
``Parameters:
- mp the multipattern upon which to apply the method.
- method the name of a function in pattern.
- ... additional arguments to pass to that method.
Returns:
-
a new multipattern of the method's results.
- union_all (mp)
-
Union all sub-patterns into a single pattern.
Folds over the sub-patterns with the union (
+
) operator, returning a single pattern.Example: ``
local combined = mp:union_all()
``Parameters:
- mp the multipattern to union over.
Returns:
-
a single pattern combining all sub-patterns.
Utilities
- print (mp, chars, domain)
-
Print a multipattern.
Prints a multipattern to io.output. If provided, a table of subpattern labels
can be used, with one entry per subpattern.
Parameters:
- mp the multipattern to be drawn.
- chars the characters to be printed for each subpattern (optional).
- domain the domain in which to print (optional).