Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pattern matching in D

Tags:

I recently stumbled over the D programming Language and i really like it. You can programm really high-level while having full Hardware access like in C.

coming from a rather functional background (Haskell,scala) I´m searching for a way to pattern match in D, but i found nothing on http://www.digitalmars.com/d/. In Haskell pattern matching is supported by the language itself. In Scala it´s achieved by case classes or extractors(normal objects with an unapply method).

Is it possible to do this in D?

the receive method in std.concurrency which is used to do concurrency in an actor-style like in erlang and scala takes a bunch of functions and pattern matheses on these. But i think it´s not as flexible as in other languages. Can you uses guards ? Can you extract the Object´s contents like it´s possible in scala ?

like image 640
KIMA Avatar asked Jun 11 '11 12:06

KIMA


People also ask

What is pattern matching in DS?

Pattern matching in computer science is the checking and locating of specific sequences of data of some pattern among raw data or a sequence of tokens. Unlike pattern recognition, the match has to be exact in the case of pattern matching.

What is pattern matching give an example?

For example, x* matches any number of x characters, [0-9]* matches any number of digits, and . * matches any number of anything. A regular expression pattern match succeeds if the pattern matches anywhere in the value being tested.

What does ?= Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

What is pattern matching in SQL?

SQL pattern matching allows you to search for patterns in data if you don't know the exact word or phrase you are seeking. This kind of SQL query uses wildcard characters to match a pattern, rather than specifying it exactly. For example, you can use the wildcard "C%" to match any string beginning with a capital C.


1 Answers

No pattern matching as known from Haskell is built in into language, but D has very generic compile time and reflection capabilities that will allow you to match type and their structure in library. For matching runtime values you can use only ordinary if / switch ... constructs; lazy evaluation of function arguments might also help with implementation of some matching-at-runtime techniques.

Template Constraints will allow you to create function (template) overloads based on any expression evaluated at compile time (D allows you to execute almost all normal code during compilation). You can also use static if for similar effect. This will practically allow you to match type structure. This is also commonly used technique in D.

You might find code of std.algorithm interesting, look for isInputRange and similar functions - they perform matching on type structure - they restrict type of argument to be of certain typeclass

Some directions for compile time reflection:

  • Is Expression
  • Traits
  • std.traits
like image 169
Michal Minich Avatar answered Nov 03 '22 03:11

Michal Minich