Interface Motif<A,B>
- Type Parameters:
A- the subtarget type of objects to match the hole againstB- the target type of objects to match the entire pattern against
- All Superinterfaces:
Function<Pattern<? super A>,,Pattern<B>> Serializable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A motif is a function object that can be applied to a subpattern
p, resulting in a pattern where the hole is filled with p. In
contrast to an arbitrary function on patterns, a motif should implement a
pattern template: Any pattern structure it creates when applied to a
subpattern p should not depend on observable properties of p;
the subpattern should be passed around, and possibly incorporated one or more
times, as a completely opaque reference.
Note that holes are characterized semantically, in that they can be filled by applying the motif. Syntactically, a hole is not necessarily a single reference in an object graph: The hole of a motif can be referred to by zero, one or several fields of other pattern objects.
Patterns are contravariant: A function on patterns on target type
A to patterns on target type B corresponds to a
nondeterministic function from B to A.
-
Method Summary
Modifier and TypeMethodDescriptionReturns a composed motif that first applies this motif to its input pattern, and then applies a given motif to the result.Wrap the given subpattern in a template.default booleanMatch this pattern once against a target, with the hole occupied by a given consumer.static <A,B> Motif <A, B> Combine two motifs conjunctively with a shared hole.Returns a composed motif that first applies a given motif to its input pattern, and then applies this motif to the result.static <A,B> Motif <A, B> Returns a constant motif where the hole is never used.eagerBindings(B target) Finds all values corresponding to the hole of this motif by the matches for a given target.static <A,B> Motif <A, B> Combine two motifs disjunctively with a shared hole.Creates an eta-expanded variant of this motif.default voidMatch this pattern exhaustively against a target, with the hole occupied by a given consumer.static <A> Motif<A, A> identity()Returns an identity motif.lazyBindings(B target) Finds all values corresponding to the hole of this motif by the matches for a given target.static <A,B> Motif <A, B> multiTransform(Function<? super B, ? extends Iterable<A>> fun) Returns a motif that transforms the target with the given multi-valued function and then passes the results to the hole.Deprecated.static <A> Motif<A, A> Returns a motif that is equivalent to one or more iterations of the given step motif.static <A> Motif<A, A> Returns a motif that is equivalent to zero or more iterations of the given step motif.Deprecated.static <A,B> Motif <A, B> Returns a motif that transforms the target with the given function and then passes the result to the hole.unlambda()Returns a pattern where the hole has been filled with a fresh variable.
-
Method Details
-
apply
Wrap the given subpattern in a template.The shape of the returned pattern should not depend on observable properties of the given subpattern: The result should be a fixed pattern-with-a-hole template, where the hole is filled with the given subpattern.
Unless specified explicitly otherwise, implementations of this interface are not expected to tolerate null references, but rather throw an exception for early detection of caller errors.
- Specified by:
applyin interfaceFunction<A,B> - Parameters:
subpattern- the pattern to fill the hole with- Returns:
- a pattern where the hole has been filled with
subpattern - Throws:
NullPointerException- ifsubpatternis null
-
star
Returns a motif that is equivalent to zero or more iterations of the given step motif.- Type Parameters:
A- the target type of objects to match against- Parameters:
step- a step motif to be iterated- Returns:
- a motif that is equivalent to the infinite disjunction of
identity(),step,root.compose(root),root.compose(root).compose(root), etc. - Throws:
NullPointerException- ifstepis null- See Also:
-
plus
Returns a motif that is equivalent to one or more iterations of the given step motif.- Type Parameters:
A- the target type of objects to match against- Parameters:
step- a step motif to be iterated- Returns:
- a motif that is equivalent to the infinite disjunction of
step,root.compose(root),root.compose(root).compose(root), etc. - Throws:
NullPointerException- ifstepis null- See Also:
-
identity
Returns an identity motif. Such a motif consists only of a hole: When applied to a subpatternp, it returnsp.The returned motif is hardly useful by itself, but rather as a building block in motif algebra.
- Type Parameters:
A- the target type of objects to match against- Returns:
- a motif that consists only of a hole
-
constant
Returns a constant motif where the hole is never used.- Type Parameters:
A- the subtarget type of objects to match the hole againstB- the target type of objects to match the entire pattern against- Parameters:
pattern- the constant pattern- Returns:
- a constant motif that always returns
patternwhen applied - Throws:
NullPointerException- ifpatternis null
-
transform
Returns a motif that transforms the target with the given function and then passes the result to the hole.- Type Parameters:
A- the subtarget type of objects to match the hole againstB- the target type of objects to match the entire pattern against- Parameters:
fun- the function to transform targets with- Returns:
- a motif that transforms the target with
funand then passes the result to the hole - Throws:
NullPointerException- iffunis null- See Also:
-
multiTransform
Returns a motif that transforms the target with the given multi-valued function and then passes the results to the hole.- Type Parameters:
A- the subtarget type of objects to match the hole againstB- the target type of objects to match the entire pattern against- Parameters:
fun- the function to transform targets with- Returns:
- a motif that transforms the target with
funand then passes the results to the hole, combining matches disjunctively - Throws:
NullPointerException- iffunis null- See Also:
-
either
static <A,B> Motif<A,B> either(Motif<? extends A, ? super B> left, Motif<? extends A, ? super B> right) Combine two motifs disjunctively with a shared hole.- Type Parameters:
A- the subtarget type of objects to match the hole againstB- the target type of objects to match the entire pattern against- Parameters:
left- an argument motifright- another argument motif- Returns:
- a motif that fills the holes of
leftandrightwith the same subpattern, and combines the resulting patterns disjunctively - Throws:
NullPointerException- ifleftorrightis null- See Also:
-
both
static <A,B> Motif<A,B> both(Motif<? extends A, ? super B> left, Motif<? extends A, ? super B> right) Combine two motifs conjunctively with a shared hole.- Type Parameters:
A- the subtarget type of objects to match the hole againstB- the target type of objects to match the entire pattern against- Parameters:
left- an argument motifright- another argument motif- Returns:
- a motif that fills the holes of
leftandrightwith the same subpattern, and combines the resulting patterns conjunctively - Throws:
NullPointerException- ifleftorrightis null- See Also:
-
on
Deprecated.Usecompose(eu.bandm.tools.paisley.Motif<C, ? super A>)instead.Returns a composed motif that first applies a given motif to its input pattern, and then applies this motif to the result.- Type Parameters:
C- the subtarget type of objects to match the hole of the combined motif against- Parameters:
before- the motif to wrap a given subpattern with before filling the hole of this motif with the result- Returns:
- a composed motif that first applies
beforeto its input pattern, and then appliesthisto the result - See Also:
-
compose
Returns a composed motif that first applies a given motif to its input pattern, and then applies this motif to the result.- Type Parameters:
C- the subtarget type of objects to match the hole of the combined motif against- Parameters:
before- the motif to wrap a given subpattern with before filling the hole of this motif with the result- Returns:
- a composed motif that first applies
beforeto its input pattern, and then appliesthisto the result - See Also:
-
then
Deprecated.Returns a composed motif that first applies this motif to its input pattern, and then applies a given motif to the result.- Type Parameters:
C- the target type of objects to match the combined motif against- Parameters:
after- the motif to wrap the result of filling the hole of this motif with a given subpattern- Returns:
- a composed motif that first applies
thisto its input pattern, and then appliesafterto the result - See Also:
-
andThen
Returns a composed motif that first applies this motif to its input pattern, and then applies a given motif to the result.- Type Parameters:
C- the target type of objects to match the combined motif against- Parameters:
after- the motif to wrap the result of filling the hole of this motif with a given subpattern- Returns:
- a composed motif that first applies
thisto its input pattern, and then appliesafterto the result - See Also:
-
lazyBindings
Finds all values corresponding to the hole of this motif by the matches for a given target.In contrast to
eagerBindings(B), the alternative successful matches are effected lazily on demand.- Parameters:
target- the object to match against- Returns:
- the values bound by this extractor on all successive matching
attempts of
rootagainsttarget, in order. - See Also:
-
eagerBindings
Finds all values corresponding to the hole of this motif by the matches for a given target.In contrast to
lazyBindings(B), the alternative successful matches are are exhausted before this method returns.- Parameters:
target- the object to match against- Returns:
- the values bound by this extractor on all successive matching
attempts of
rootagainsttarget, in order. - See Also:
-
stream
-
unlambda
Returns a pattern where the hole has been filled with a fresh variable.The variable can be retrieved from the result with
Theme.getArgument().- Returns:
- a pattern where the hole has been filled with a fresh variable
-
attempt
Match this pattern once against a target, with the hole occupied by a given consumer.At most one match is performed; nondeterministic extra matches are ignored. For multiple matches, use
exhaust(B, java.util.function.Consumer<? super A>)instead.If a match is successful up to the hole, then the subobject to match the hole against is passed to
consumer.- Parameters:
target- the target object to match againstconsumer- a consumer for the extracted subobject to match the hole against- Returns:
trueif a match has been successful;falseotherwise- Throws:
NullPointerException- ifconsumeris null- See Also:
-
exhaust
Match this pattern exhaustively against a target, with the hole occupied by a given consumer.Matching is iterated until failure. For a single match, use
attempt(B, java.util.function.Consumer<? super A>)instead.If a match is successful up to the hole, then the subobject to match the hole against is passed to
consumer.- Parameters:
target- the target object to match againstconsumer- a consumer for each extracted subobject to match the hole against- Throws:
NullPointerException- ifconsumeris null- See Also:
-
etaExpand
Creates an eta-expanded variant of this motif.The returned motif has the same observable behavior as this motif, but creates patterns with a particular structure: The original hole is filled with a fresh variable, and a new hole is made that consumes the lazy bindings of that variable.
The resulting form can improve efficiency if a motif is reused many times and/or compiled: The hole in an eta-expanded motif occurs exactly once right at the top; the rest of the template is unchanged and can be shared.
- Returns:
- an eta-expanded variant of this motif
- See Also:
-
compose(eu.bandm.tools.paisley.Motif<C, ? super A>)instead.