Questions tagged [active-pattern]

Active pattern is a technique in F# programming language which enable you to define named partitions that subdivide input data, so that you can use these names in a pattern matching expression just as you would for a discriminated union.

Active pattern is a technique in F# programming language which enable you to define named partitions that subdivide input data, so that you can use these names in a pattern matching expression just as you would for a discriminated union.

You can use active patterns to decompose data in a customized manner for each partition. For example, its possible wrap objects with an active pattern, so that you can use objects in pattern matching as easily as any other union type.

41 questions
4
votes
3 answers

FizzBuzz with Active Patterns

I'm trying to understand Active Patterns, so I'm playing around with FizzBuzz: let (|Fizz|_|) i = if i % 3 = 0 then Some Fizz else None let (|Buzz|_|) i = if i % 5 = 0 then Some Buzz else None let (|FizzBuzz|_|) i = if i % 5 = 0 && i % 3 = 0 then…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
3
votes
1 answer

How can I pass complex expression to parametrized active pattern?

I defined the active pattern "Expression" as follows: let (|Expression|_|) expression _ = Some(expression) Now I'm trying to use it in this way: match () with | Expression((totalWidth - wLeft - wRight) / (float model.Columns.Count - 0.5)) cw …
LOST
  • 2,956
  • 3
  • 25
  • 40
3
votes
2 answers

How to pattern-match when something is NOT of a particular type

We're all used to pattern-matching for cases when something is a particular type, e.g., match x with | Y(x) :: tail -> ... // assumes List.head(x) is of type Y(x) But how can can I match the case when something is not of a particular type?…
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
3
votes
2 answers

Splitting out blocks of code in F# pattern matching for readability

// Standard pattern matching. let Foo x = match x with | 1 -> // ... lots of code, only evaluated if x == 1 | 2 -> // ... lots of code, only evaluated if x == 2 // Standard pattern matching separated out, causing exception. let…
3
votes
1 answer

Why active patterns behave this way?

I found this code in the excellent book F# Design Patterns by Gene Belitski: let (| `` I'm active pattern `` |) x = x + 2 let (`` I'm active pattern `` y) = 40 (* val ( |`` I'm active pattern ``| ) : x:int -> int val y : int = 42 *) The author…
Soldalma
  • 4,636
  • 3
  • 25
  • 38
3
votes
1 answer

Subset of Union members as "parameter" in Pattern matching

Let us have a type definition for a tree with several types of binary nodes, among other types of nodes, i.e. type Tree = | BinaryNodeA of Tree * Tree | BinaryNodeB of Tree * Tree | [Other stuff...] I want to manipulate this tree using a recursive…
Serendip
  • 63
  • 1
  • 3
3
votes
2 answers

Appropriate use of active patterns in F#

I'm using an active pattern to parse usage events in a csv formatted usage log. The active pattern part is listed below. Parsing the whole file in works well and the sequence that is produced is filled with all sorts of UsageEvents. type SystemName…
Wouter
  • 2,170
  • 1
  • 28
  • 58
3
votes
2 answers

F# Avoid active pattern overwriting

I have noticed I cannot create two active patterns with the same options, but I can have two with similar ones without any warning: let (|A|B|C|) c = if (c = 'a') then A else if (c = 'b') then B else C let (|A|B|D|) c = if (c = '1')…
vtortola
  • 34,709
  • 29
  • 161
  • 263
3
votes
1 answer

Trouble with FSharp active patterns

I'm parsing code quotations in FSharp and am building up pattern helpers. All was going well till I tried let (|BinaryFn|_|) fn (input:Expr) = function | SpecificCall fn (_,_,l::r::[]) -> Some(l,r) | _ -> None let (|Multiply|_|) x = …
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
2
votes
1 answer

Resuse F# Active Pattern result

In the following code I have to reuse the Active Pattern result three times for each iteration. i.e. match tree.Parent, postion with I found out that I could save the Active Pattern result. i.e. let pos =…
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
2
votes
2 answers

What's wrong with ActivePattern matching against System.Type?

module Reflection = [] module Type = let isType<'a> = Unchecked.defaultof<'a> let (|IsEqual|Isnt|) (_:'a) (t:Type):Choice = let t' = typeof<'a> if t = t' then…
Maslow
  • 18,464
  • 20
  • 106
  • 193
2
votes
2 answers

F#: Type matching and active patterns

Given the following: type IFruit = interface end type Avocado = { color : string; age : int } interface IFruit let (|AvocadoTexture|) (a : Avocado) = if a.age < 7 then "firm" else "mushy" ... Why does this work: let texture (f : IFruit) = …
MiloDC
  • 2,373
  • 1
  • 16
  • 25
2
votes
3 answers

Curried Arguments in Discriminated Unions

I have a discriminated union like this: type A = |B | C of int*A I have to pattern match like this (the parenthesis appear to be needed): match x with | B -> printfn "B" | C (i,a) -> printfn "%A, %A" i a Is there a way to instead match like this…
Ringil
  • 6,277
  • 2
  • 23
  • 37
2
votes
1 answer

Why would I use the keyword "inline" for an Active Pattern?

I still cannot understand why I would use the keyword inline for a function. What does it give me that I don't already have? let inline (|Positive|Neutral|Negative|) x = match sign x with | 1 -> Positive | -1 -> Negative | _ -> Neutral
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
2
votes
1 answer

Is is possible to pattern match on the underlying shape of a discriminated union?

Does F# support pattern matching of a discriminated union member instance by criteria other than the Identifier pattern? For example, imagine that I want to match on the underlying shape of the data and I want to consider anything with an int * int…