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
2
votes
2 answers

Pattern combining type test and literal

The active pattern in this question fails to compile after upgrading to VS 2012 RTM. It provides a way to do a type test and match a literal within a single pattern. For example: let (|Value|_|) value = match box value with | :? 'T as x -> Some…
Daniel
  • 47,404
  • 11
  • 101
  • 179
1
vote
2 answers

Why do active patterns require special syntax?

If ordinary functions could be used as patterns it would save having to write trivial active patterns like let (|NotEmpty|_|) s = Seq.tryPick Some s and would, hypothetically, allow let s = seq [] match s with | Seq.tryPick Some -> ... | _ ->…
Daniel
  • 47,404
  • 11
  • 101
  • 179
1
vote
0 answers

Type mismatch on active pattern with ReadOnlySpan parameters and (expected) implicit argument conversion

I am confused as to why this bit of F# code fails to compile: open System [] module Al = [] let inline (|EquivalentName|_|) (name1: string) (name2: string) : unit voption = ValueNone //…
Bent Rasmussen
  • 5,538
  • 9
  • 44
  • 63
1
vote
1 answer

How to partially apply an active pattern

The Fsharpx.Extras NuGet package exposes an active pattern for regular expression matching, qualified as Fsharpx.Text.Regex.Match. The first parameter is a RegexOptions value from the BCL. Rather than having to write: let someFunc = | Match…
RMills330
  • 319
  • 1
  • 12
1
vote
1 answer

Active Pattern on FSharpPlus trySscanf

I am trying to create an active pattern Scan around FSharpPlus's trySscanf, so that the following works: let res = // res = 42 match "Hello 42 World" with | Scan "Hello %i World" n -> n The way I understand incomplete active patterns to work, I…
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1
vote
1 answer

overloading F# active patterns

I am fairly new to F# and active patterns, and I ran across an anomoly that I can't explain. module Eval = let (|Bet|Pass|) (test:BetChoice) = match test with | BetChoice.Bet -> Bet | BetChoice.Pass -> Pass let…
Snark
  • 1,664
  • 14
  • 27
1
vote
0 answers

Calling multicase active pattern directly

Consider a single case active pattern : let (|ToUpper|) (input : string) = input.ToUpper();; I can call the above single case active pattern outside an explicit match: let g ( ToUpper x ) =x ;; > val g : string -> string g ("hello");; …
shing
  • 75
  • 1
  • 5
1
vote
1 answer

F# match active pattern as expanded tuple

I get the following error in diff with a red squiggle under Subset. Type mismatch. Expecting a Range -> Choice but given a Range * Range -> Choice Is there some sort of type annotation I can add to the SubSet match so I don't have to use fst and…
gradbot
  • 13,732
  • 5
  • 36
  • 69
1
vote
1 answer

F# Partial Active Pattern Matching "Rule Will Never Be Matched"

Given the following active pattern: let (| HasMatch |) (x:string) = if x.Contains("0") then Some() else None;; And the following pattern matching func: let testFn x = function | HasMatch i -> printfn "%A" i | _ -> printf "nope";; The last…
Micah
  • 10,295
  • 13
  • 66
  • 95
0
votes
1 answer

how c# pattern matching do things like f#'s active pattern

F# active pattern can do let (|Even|Odd|) input = if input % 2 = 0 then Even else Odd let TestNumber input = match input with | Even -> printfn "%d is even" input | Odd -> printfn "%d is odd" input TestNumber 7 a poor C# implementation…
Rm558
  • 4,621
  • 3
  • 38
  • 43
0
votes
1 answer

Deserialization of Xml with F#

I get the following xml from a web service which I want to convert into .net objects: let xmlString = " OK
Beaker
  • 2,804
  • 5
  • 33
  • 54
1 2
3