Recap
Pattern matching
Fโฏ core building block
Combines "data structure matching" and "deconstruction"
Used almost everywhere:
match
andfunction
expressionstry/with
blocklet
binding, including function parameter
Can be abstracted into a
fold
function associated with a union type
Patterns
Constant โข Identifier โข Wilcard
1
, Color.Red
โข Some 1
โข _
Collection : Cons โข List โข Array
head :: tail
โข [1; 2]
Product type : Record โข Tuple
{ A = a }
โข a, b
Type Test
:? Subtype
Logical : OR, AND
1 | 2
, P1 & P2
Variables โข Alias
head :: _
โข (0, 0) as origin
+ The when
guards in match
expressions
Active Patterns
Extending pattern matching
Based on function + metadata โ 1st-class citizens
4 types: total simple/multiple, partial (simple), parametric
At 1st little tricky to understand, but we get used to it quickly
Use for:
Add semantics without relying on union types
Simplify / factorize guards
Wrapping BCL methods
Extract a data set from an object
...
Addendum
Last updated
Was this helpful?