F# Training
F# Training
F# Training
  • Presentation
  • Fundamentals
    • Introduction
    • Syntax
      • Bases
      • Functions
      • Rules
      • Exceptions
    • First concepts
    • ๐Ÿ”Quiz
  • Functions
    • Signature
    • Concepts
    • Syntax
    • Standard functions
    • Operators
    • Addendum
    • ๐Ÿ”Quiz
    • ๐Ÿ“œSummary
  • Types
    • Overview
    • Tuples
    • Records
    • Unions
    • Enums
    • Anonymous records
    • Value types
    • ๐Ÿ“œRecap
    • Addendum
  • Monadic types
    • Intro
    • Option type
    • Result type
    • Smart constructor
    • ๐Ÿš€Computation expression (CE)
    • ๐Ÿš€CE theoretical basements
    • ๐Ÿ“œRecap
  • Pattern matching
    • Patterns
    • Match expression
    • Active patterns
    • ๐Ÿš€Fold function
    • ๐Ÿ“œRecap
    • ๐Ÿ•น๏ธExercises
  • Collections
    • Overview
    • Types
    • Common functions
    • Dedicated functions
    • ๐Ÿ”Quiz
    • ๐Ÿ“œRecap
  • Asynchronous programming
    • Asynchronous workflow
    • Interop with .NET TPL
    • ๐Ÿ“œRecap
  • Module and Namespace
    • Overview
    • Namespace
    • Module
    • ๐Ÿ”Quiz
    • ๐Ÿ“œRecap
  • Object-oriented
    • Introduction
    • Members
    • Type extensions
    • Class, Struct
    • Interface
    • Object expression
    • Recommendations
Powered by GitBook
On this page
  • Pattern matching
  • Patterns
  • Active Patterns
  • Addendum

Was this helpful?

Edit on GitHub
  1. Pattern matching

Recap

Pattern matching

  • Fโ™ฏ core building block

  • Combines "data structure matching" and "deconstruction"

  • Used almost everywhere:

    • match and function expressions

    • try/with block

    • let binding, including function parameter

  • Can be abstracted into a fold function associated with a union type

Patterns

Pattern
Example

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

PreviousFold functionNextExercises

Last updated 1 month ago

Was this helpful?

๐Ÿ“œ , F# for fun and profit, Juin 2012

๐Ÿ“œ , F# for fun and profit, Juin 2012

๐Ÿ“œ (6 articles), F# for fun and profit, Aoรปt 2015

๐Ÿ“น -

๐Ÿ“œ
Match expressions
Domain modelling et pattern matching : Roman numeral
Recursive types and folds
A Deep Dive into Active Patterns
Repo github