New answers tagged pattern-matching
5
votes
4
votes
Accepted
Apply pattern using a Slot (#) to each list element using a second slot
An alternative to the function that @Bill created for applying to more complex arrays is as follows:
...
- 7,989
6
votes
Apply pattern using a Slot (#) to each list element using a second slot
As requested, Use function definition to separate the two applications of #
...
- 11.1k
8
votes
Accepted
Test whether an expression contains a particular pattern
This is a bit more direct:
ContainsPlus = Not@*FreeQ[Plus]
Examples:
...
- 15.5k
0
votes
Confusing result when pattern matching a BlankSequence (__) to a list, then calling unary function on the list
It's because -> evaluates the right hand side of the rule eagerly when the rule is defined, not when it's later applied. In the case of ...
- 181
3
votes
How to combine multiple named patterns into one Cases?
ClearAll[foo]
foo = Function[,
alt : Alternatives @@ #[[All, 1]] :> ReplaceAll[#] @ alt,
HoldFirst] ;
Examples:
...
- 356k
3
votes
How to combine multiple named patterns into one Cases?
Thanks for all the input and comments.
I thought to share a solution I came up with. What I really wanted is to apply a set of patterns on input, and have those expressions in the input which match ...
- 127k
3
votes
Accepted
How to combine multiple named patterns into one Cases?
1. Why (I think) it doesn't work?
The problem with the proposed solution is (probably) that Cases needs its second argument to be a pattern (or a pattern ...
- 4,095
1
vote
Cases without repetition
I agree with everything that Nasser has said above.
However, if you don't mind the result as an association (rather than a list of rules), you could do something like the following? Not sure how ...
- 11.8k
4
votes
Cases without repetition
As someone said, premature optimization is the root of all evil.
I think you are trying to optimize something not needed. As mentioned in comments, you can simply remove duplicate items from the list ...
- 127k
1
vote
How to use Position to select based on absence of a pattern?
ps = Position[lis,x_/;Head[x]=!=Sin,{2},Heads->False]
(* {{1,1},{1,2},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3}} *)
- 11.8k
7
votes
Accepted
How to use Position to select based on absence of a pattern?
As far as I understand it, Except is a pattern object and it fits with pattern matching.
...
- 28.5k
10
votes
Accepted
RuleDelayed (:>) involving Piecewise leads to Piecewise::pairs warning in ReplaceAll (/.)
While creating the delayed rule Mathematica evaluates the left hand side of the rule by default. Evaluating Piecewise[a_, b___] returns the error message, but stays ...
- 18.3k
Top 50 recent answers are included
Related Tags
pattern-matching × 1423list-manipulation × 258
replacement × 213
string-manipulation × 163
function-construction × 106
filtering × 72
functions × 62
argument-patterns × 61
rule × 52
expression-manipulation × 50
evaluation × 41
symbolic × 37
simplifying-expressions × 36
bugs × 36
performance-tuning × 34
core-language × 34
programming × 33
associations × 28
hold × 24
sequence × 24
regular-expressions × 24
matrix × 23
attributes × 22
expression-test × 21
optional-arguments × 19