3 ms·
> In functional languages this is called "destructuring assignment". I thought it's just called pattern matching. Most functional languages don't have assignme
by bmitc 5y ago
> In functional languages this is called "destructuring assignment".
I thought it's just called pattern matching. Most functional languages don't have assignment and have bindings instead.
- nerdponx 5y agoI would argue that destructuring is a special case of pattern matching.
- raymondh 5y agoIOW, the match/case tool can pattern match and destructure at the same time. Som uninterestingly, it is possible to do only one of those things: Iterator unpacking: first, *middle, last = t is similar to a match/case with a sequence pattern: match t: case first, *middle, last: ... The former works for any iterable and the latter requires a sequence. The former raises an error for a non-iterable and the latter just skips the case for a non-sequence. Otherwise, they are similar. However, arguing that one is a special case of the other misses the entire point of structural pattern matching. We would almost never substitute the latter for the former. They mostly live in different worlds because we use them and think about them much differently: # Something that would work but they we don't do for item in somedict.items(): match item: case [key, value]: print(key.upper()) print(value) print() case _: raise RuntimeError('unexpected kind of item') # What we actually do for key, value in somedict.items(): print(key.upper()) print(value) print() Sorry for the small rant, but I think this pedantic point isn't a useful line of thinking. That concepts and implementation have some overlap but should occupy a different concept maps in our brains — very much like the idea that dicts can do set-like things but that we're better off thinking about dicts as doing lookups and sets as doing uniquification, membership-testing, and set-to-set operations.
- greggyb 5y ago"Pattern matching", in the context of functional programming languages, most often refers to the complex combination of destructuring bind and conditional control flow based on which binding succeeded. This article describing the Python feature of multiple assignment demonstrates only the destructuring assignment (assignment, because it is Python and these variables can be reassigned). There is no control flow tied to this operation, except for the exceptions that can be thrown when the LHS and RHS have incompatible structure. Clojure is another example of a language with destructuring binding that is not tied to a control flow construct. Clojure functions and `let` bindings can use a destructuring syntax to bind to elements in a data structure. There is no tightly-linked control flow structure that goes along with this binding mechanism. It is my understanding that there are several libraries available that implement pattern matching, but I do not use these. Most often, when I have seen pattern matching described to an audience with a background in imperative languages, it is compared to a super-powered switch statement. This gives the impression that the important part is the control flow. Languages such as Python and Clojure show that there is inherent value in the destructuring assignment/bind, even out of the context of control flow.