Questions tagged [pattern-matching]

Finding sequences of data that have a particular structure or matching specific criteria or rules. For character pattern defined by a regular expression, use the tag regular-expressions instead.

Pattern matching is about finding sequences of data having a particular structure or matching specific criteria or rules.

The pattern can be defined by enumerating an explicit list of sequences, by defining expected combinations of sub-sequences, or by defining other criteria or rules, which if true characterize a sequence belonging to the pattern. These criteria/rules can also express some tolerances, which makes pattern-matching also suitable for finding similarity.

Pattern matching also relates to the extraction of data embedded in the patterns that are matched. Some programming language use this feature in language construct, sometimes using the term pattern matching as synonym for unification .

Pattern matching applies to any kind of data, such as binary data, DNA, character strings, graphs, etc...

Further reading:

Disambiguation:

34 questions
27
votes
4 answers

Is pattern-matching against types idiomatic or poor design?

It seems like F# code often pattern matches against types. Certainly match opt with | Some val -> Something(val) | None -> Different() seems common. But from an OOP perspective, that looks an awful lot like control-flow based on a runtime type…
12
votes
1 answer

Pattern matching in Clojure vs Scala

What are the key differences between pattern matching in these two languages? I am not referring to syntax, but capability, implementation details, range of use cases and necessity. Scala applications (eg. Lift and Play) speak proudly about the…
10
votes
5 answers

Idiomatic pattern matching equivalent in Java

I'm building a simulator which parses some events from STDIN and "runs" them. My background is mostly functional programming these days, so it seemed natural to do something like this: data Event = Thing1 String Int | Thing2 Int | Thing3 String…
closeparen
  • 646
  • 4
  • 9
10
votes
2 answers

Why can't the Scala compiler give pattern matching warning for nonsealed classes/traits?

If I use an unsealed trait or abstract class in Scala and then use pattern matching, I wonder, does the compiler not know at compile time for this particular patternmatch what possible implementations of this trait/class are available? So, if it…
valenterry
  • 2,429
  • 16
  • 21
7
votes
1 answer

How do you make decorators as powerful as macros?

Quick background: I am designing a Pythonic language that I want to be as powerful as Lisp while remaining easy to use. And by "powerful", I mean "flexible and expressive". I've just been introduced to Python function decorators, and I like them. I…
Gavin D. Howard
  • 1,020
  • 1
  • 10
  • 11
6
votes
3 answers

Search for similar vectors, based on elementwise difference

I understand that there is a thread discussing a similar issue here: How to efficiently search a set of vectors for a vector which is the closest match But my problem is slightly different - and hopefully easier. Given a set of vectors of the same…
Ziqi
  • 163
  • 4
6
votes
2 answers

Data structure for pattern matching

Let's say you have an input file with many entries like these: date, ticker, open, high, low, close, And you want to execute a pattern matching routine on the entries(rows) in that file, using a candlestick pattern, for…
alvonellos
  • 432
  • 1
  • 4
  • 16
5
votes
4 answers

Are there known constructs for two-way string pattern matching?

I've often come across situations where pattern matching in a string is formalized, but the reverse is not. Say I invent a new string pattern that can be expressed by regex /([0-9a-z])*:([0-9a-z])*@([0-9a-z])/. Expressing that regex as data to your…
Jonathan Allard
  • 215
  • 1
  • 6
5
votes
2 answers

Avoiding instanceof for recursive data types

I have written a simple class hierarchy to represent terms in Scala. Terms are recursive data types, e.g., a Sum and a Multiplication consist of the left-hand-side (lhs), which is a Term, and the right-hand-side (rhs), which is also a Term. Scala's…
helios35
  • 159
  • 2
5
votes
3 answers

How to report multiple errors as a result of validation?

I have a class that transforms a complex model, for example an abstract syntax tree or intermediate model. The model can be either valid, invalid or partially invalid, i.e. it contains errors but some parts of it are valid and can be processed…
user3998276
  • 187
  • 1
  • 2
  • 7
5
votes
2 answers

Is it possible (and practical) to search a string for arbitrary-length repeating patterns?

I've recently developed a huge interest in cryptography, and I'm exploring some of the weaknesses of ECB-mode block ciphers. A common attack scenario involves encrypted cookies, whose fields can be represented as (relatively) short hex strings. Up…
Louis Thibault
  • 2,178
  • 3
  • 16
  • 17
4
votes
1 answer

Pattern matching against two similar types

What is the best way to handle pattern matching in the following situation? sealed trait Metadata final case class Metadata1() extends Metadata final case class Metadata2() extends Metadata final case class Metadata3() extends Metadata sealed trait…
MI3Guy
  • 250
  • 1
  • 6
3
votes
1 answer

Best method for Pattern Matching on Binary String?

I need to search a long string of binary string data (couple of megabytes worth of binary digits) for patterns of binary digits. There are about 100 000 different patterns to search for. Each is a maximum of 10 bits/character length patterns of 1's…
user289944
  • 39
  • 1
  • 3
3
votes
1 answer

Pattern matching events with arbitrary structure

My team implemented an alert broker system. The way it works is this: Event sources generate events, with mostly arbitrary structure, and the broker listens to them. Users go to the broker and use an expression language to create alerts for the…
user21123
3
votes
0 answers

String Pattern Matching from Lookup Table - Non-Exponential Solution?

Given the problem... Given a String comprising of non-alhpabetical symbols, and a Lookup Table where a subset of those symbols may equate to a single alphabetical character, output all possible Strings. ...is it possible to compute in…
1
2 3