Use of Partial-Case with Parameters

Depicts use of one kind of active pattern matching; partial-case with parameters.

Copy Source
Copy Link
Tools:
 1: // A record type to hold name and age
 2: type Person = 
 3:     { Name: string; 
 4:       Age: int }
 5: 
 6: // Partial active pattern with two argus
 7: let (|Young|_|) age (lst: list<Person>) =
 8:     lst |> List.mapi(fun i _ -> (lst.Item i).Age) 
 9:         |> List.tryFind(fun i -> i <= age)
10: 
11: // A matching funtion
12: let findYoungAge = function
13:     | Young 25 yr -> printfn "Found a young age of %d" yr
14:     | _ -> printfn "Couldn't find any ages younger than 25"
15: 
16: // Construct a list of record values
17: let listOfPerson = 
18:     [ {Name = "John";    Age = 29}; 
19:       {Name = "Simon";   Age = 24}; 
20:       {Name = "Shirley"; Age = 50} ]
21: 
22: // "Found a young age of 24"
23: findYoungAge listOfPerson
24: 
Person.Name: string
Multiple items
val string : 'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------

type string = System.String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
Person.Age: int
Multiple items
val int : 'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------

type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>

  type: int<'Measure>
  implements: System.IComparable
  implements: System.IConvertible
  implements: System.IFormattable
  implements: System.IComparable<int<'Measure>>
  implements: System.IEquatable<int<'Measure>>
  inherits: System.ValueType


--------------------

type int = int32

Full name: Microsoft.FSharp.Core.int

  type: int
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
val age : int

  type: int
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
val lst : Person list

  type: Person list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<Person>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<Person>
  implements: System.Collections.IEnumerable
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>

  type: 'T list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
type Person =
  {Name: string;
   Age: int;}

Full name: Snippet.Person

  type: Person
  implements: System.IEquatable<Person>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Person>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------

type List<'T> =
  | ( [] )
  | ( :: ) of 'T * 'T list
  with
    interface System.Collections.IEnumerable
    interface System.Collections.Generic.IEnumerable<'T>
    member Head : 'T
    member IsEmpty : bool
    member Item : index:int -> 'T with get
    member Length : int
    member Tail : 'T list
    static member Cons : head:'T * tail:'T list -> 'T list
    static member Empty : 'T list
  end

Full name: Microsoft.FSharp.Collections.List<_>

  type: List<'T>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
val mapi : (int -> 'T -> 'U) -> 'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.mapi
val i : int

  type: int
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
property List.Item: int -> Person
val tryFind : ('T -> bool) -> 'T list -> 'T option

Full name: Microsoft.FSharp.Collections.List.tryFind
val findYoungAge : Person list -> unit

Full name: Snippet.findYoungAge
active recognizer Young: int -> Person list -> int option

Full name: Snippet.( |Young|_| )
val yr : int

  type: int
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
val printfn : Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val listOfPerson : Person list

Full name: Snippet.listOfPerson

  type: Person list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<Person>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<Person>
  implements: System.Collections.IEnumerable

More information

Link: http://fssnip.net/46
Posted: 3 years ago
Author: Joel Huang
Tags: active pattern