Questions tagged [pattern-matching]
Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.
1,615 questions
100
votes
4
answers
4k
views
What best practices or performance considerations are there for choosing between Cases, Position, Pick and Select?
Cases, Select,Pick and Position each have different ...
84
votes
4
answers
7k
views
Using a PatternTest versus a Condition for pattern matching
My last question to the site resulted in several answers that involve using pattern matching in Mathematica, a feature I wasn't very familiar with at the time. I am currently reading Mathematica ...
60
votes
1
answer
3k
views
How to generally match, unify and merge patterns?
This question was split from this one. While that question is now about how to match two particular patterns (mostly using Verbatim or ...
58
votes
3
answers
2k
views
How is pattern specificity decided?
Mathematica has a notion of pattern specificity, which is a partial ordering on patterns.
The rules (e.g. DownValues, SubValues,...
46
votes
10
answers
7k
views
Find zero crossing in a list
I'm looking for a function that finds the index of the zero-crossing points of a list. Before I go making my own subroutine to do this, I was wondering if anyone knows of any built-in Mathematica ...
39
votes
1
answer
2k
views
Why is StringExpression faster than RegularExpression?
Edit: as noted by Albert Retey the performance difference is only seen when sub expression extraction is performed. If this test is used below the timings are similar:
...
38
votes
4
answers
6k
views
Looking for "Longest Common Substring" solution
I'm looking for robust code to solve the "Longest Common Substring" problem:
Find the longest string (or strings) that is a substring (or are substrings) of two or more strings.
I can just code it ...
38
votes
3
answers
3k
views
How to Combine Pattern Constraints and Default Values for Function Arguments?
EDIT: As several respondents have noted in the answers and comments below, the original example had a default value that would never be used because of the way patterns and default values are ...
34
votes
12
answers
7k
views
Find subsequences of consecutive integers inside a list
I have a list which is something like this:
{3,4,5,6,7,10,11,12,15,16,17,19,20,21,22,23,24,42,43,44,45,46}
What I'd like to to is get the intervals which are in ...
34
votes
2
answers
4k
views
How do I perform string matching and replacements?
What are, and how do I use Mathematica's string matching and replacement tools?
34
votes
2
answers
919
views
Why (and when) does pattern matching with f[__] perform MUCH more quickly than _f?
Consider this expression in Mathematica 10.3 (and above) on MacOS X:
expr = a b c d e f g h i j k l m n o p q r s t myG[u] myF[a, b]
Now compare the time it ...
34
votes
1
answer
832
views
Make my data-structure atomic
Lately, and by lately I mean since version 7 or so, the number of atomic expressions in Mathematica constantly grew. In former times only the native types like integers and optimised arrays were ...
32
votes
5
answers
3k
views
Fetching data from HTML source
I want to generate a couple of plots/graphs with Area 51 statistics. Since Area 51 doesn't work with the SE API, I'm forced to find another way to get the information I want.
That other way is with <...
31
votes
9
answers
3k
views
Using patterns in pure functions
Pure functions may be handy if you don't want to assign your function a name. For example I would calculate $x(x-1)$ for some numbers $x$ by
...
30
votes
2
answers
1k
views
How can I ensure that I am constructing patterns in the most efficient way possible?
Inspired by this answer, I am interested to know if there are best practices or rules of thumb for constructing patterns, for example for use in function definitions (...