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
22
votes
1 answer

F# Mapping Regular Expression Matches with Active Patterns

I found this useful article on using Active Patterns with Regular Expressions: http://www.markhneedham.com/blog/2009/05/10/f-regular-expressionsactive-patterns/ The original code snippet used in the article was this: open…
Beaker
  • 2,804
  • 5
  • 33
  • 54
13
votes
2 answers

Active patterns and member constraint

For an inline function one could create a constraint like: let inline implicit arg = ( ^a : (static member op_Implicit : ^b -> ^a) arg) requiring the given operator or member on the arguments. Is there a way to match based on somthing similar? I…
Rune FS
  • 21,497
  • 7
  • 62
  • 96
9
votes
1 answer

Passing partial active patterns as arguments?

I'm learning F# by writing a recursive descent parser using active patterns. Since all my rules or partial active patterns I need to combine them in different manners, but I'm getting really frustrated with the syntax of passing active patterns as…
monoceres
  • 4,722
  • 4
  • 38
  • 63
9
votes
2 answers

Trying to understand F# active patterns, why can I do this:

I have a Dictionary over which I initially iterated thusly: myDictionary |> Seq.iter (fun kvp -> doSomething kvp.Key kvp.Value) Later, I discovered that I could make use of the KeyValue active pattern, and do this: myDictionary |> Seq.iter (fun…
MiloDC
  • 2,373
  • 1
  • 16
  • 25
8
votes
2 answers

Using boolean functions as pattern discriminators in F#

I tried googling this but I couldn't find a collection of words that guided me to what I am trying to do. I am trying to solve Project Euler Problem 54, and I have this rather ridiculous function: let evaluate hand = if isRoyalFlush hand then 9 …
asibahi
  • 857
  • 8
  • 14
8
votes
4 answers

Active pattern broken in F# 3.0

This active pattern compiles with F# 2.0: let (|Value|_|) value = // 'a -> 'T option match box value with | :? 'T as x -> Some x | _ -> None but, in F# 3.0, emits the error: Active pattern '|Value|_|' has a result type containing type…
Daniel
  • 47,404
  • 11
  • 101
  • 179
5
votes
2 answers

Use of typeof<_> in active pattern

Given the following contrived active pattern: let (|TypeDef|_|) (typeDef:Type) (value:obj) = if obj.ReferenceEquals(value, null) then None else let typ = value.GetType() if typ.IsGenericType && typ.GetGenericTypeDefinition() = typeDef…
Daniel
  • 47,404
  • 11
  • 101
  • 179
5
votes
3 answers

Incomplete match with AND patterns

I've defined an expression tree structure in F# as follows: type Num = int type Name = string type Expr = | Con of Num | Var of Name | Add of Expr * Expr | Sub of Expr * Expr | Mult of Expr * Expr | Div of Expr * Expr |…
luksan
  • 7,661
  • 3
  • 36
  • 38
5
votes
3 answers

Using active patterns within discrimated union type declarations

Is it possible to use active patterns within discrimated union type declarations? To be more precise, consider the following toy example: type T = | A of int | B let (|Negative|_|) t = match t with | A n when n < 0 -> Some () …
Mo B.
  • 5,307
  • 3
  • 25
  • 42
4
votes
1 answer

How to compose F# Active Pattern Matching functions inside a Function Expression?

We have this small helper functions open System let (|NonEmptyString|) value = if String.IsNullOrEmpty value then failwith "String must be non-empty." else value let (|StartsWithX|) (value:string) = if value.StartsWith("X") |> not …
Functional_S
  • 1,159
  • 6
  • 9
4
votes
1 answer

Active Pattern Matching with Discriminated Unions

Is there any way to use a discriminated union of the following form with active pattern matching? I haven't been able to find any examples. This is what I'm trying to do: type c = a | b type foo = | bar1 | bar2 of c //allowed let…
Magg G.
  • 229
  • 3
  • 10
4
votes
1 answer

Multi-case parameterized active patterns returning error FS0722 Only active patterns returning exactly one result may accept arguments

Since I only found Japanese pages about this error, I figured, let's log it and ask here, since my Japanese is kinda rusty. If I have the following FSharp active pattern (simplified example): let (|InRange|OutOfRange|) from too = function |…
Abel
  • 56,041
  • 24
  • 146
  • 247
4
votes
2 answers

Match List Items in Any Order

I'm still a novice when it comes to many areas of F#. I'm asking this question more out of curiosity than out of an actual business need. Is there any way to match the first n items in a list regardless of what order they appear? To clarify,…
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
4
votes
2 answers

F# Active Pattern List.filter or equivalent

I have a records of types type tradeLeg = { id : int ; tradeId : int ; legActivity : LegActivityType ; actedOn : DateTime ; estimates : legComponents ; entryType : ShareOrDollarBased ; confirmedPrice: DollarsPerShare…
akaphenom
  • 6,728
  • 10
  • 59
  • 109
4
votes
3 answers

F# active pattern as non-static member

I'm not sure if non-static public member active patterns are allowed but you can define them without the compiler complaining. If they are allowed what's the syntax for matching against one? The compiler is giving me a type mismatch for Foo in…
gradbot
  • 13,732
  • 5
  • 36
  • 69
1
2 3