現用模式

現用模式可讓您定義細分輸入資料的具名分割區,讓您可以在模式比對運算式中使用這些名稱,就像您對差別聯集一樣的處理方式。 您可以使用作用中的模式,以自訂方式分解每個部分的資料。

語法

// Active pattern of one choice.
let (|identifier|) [arguments] valueToMatch = expression

// Active Pattern with multiple choices.
// Uses a FSharp.Core.Choice<_,...,_> based on the number of case names. In F#, the limitation n <= 7 applies.
let (|identifier1|identifier2|...|) valueToMatch = expression

// Partial active pattern definition.
// Uses a FSharp.Core.option<_> to represent if the type is satisfied at the call site.
let (|identifier|_|) [arguments] valueToMatch = expression

備註

在先前的語法中,識別碼是輸入資料分割區的名稱,這些分割區是由引數表示,換句話說,是引數集合之所有值的子集名稱。 現用模式定義中最多可以有七個分割區。 運算式描述要分解資料的表單。 您可以使用現用模式定義來定義規則,以判斷指定為引數的值所屬的具名分割區。 符號 (| 和 |) 稱為香蕉夾,而這種型別的 let 繫結所建立的函式,稱為主動式識別器

例如,請考慮使用引數的下列現用模式。

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
TestNumber 11
TestNumber 32

此程式的輸出為:

7 is odd
11 is odd
32 is even

現用模式的另一個用法是以多種方式分解資料類型,例如,當相同的基礎資料具有各種可能標記法時。 例如,Color 物件可能會分解成 RGB 標記法或 HSB 標記法。

open System.Drawing

let (|RGB|) (col : System.Drawing.Color) =
     ( col.R, col.G, col.B )

let (|HSB|) (col : System.Drawing.Color) =
   ( col.GetHue(), col.GetSaturation(), col.GetBrightness() )

let printRGB (col: System.Drawing.Color) =
   match col with
   | RGB(r, g, b) -> printfn " Red: %d Green: %d Blue: %d" r g b

let printHSB (col: System.Drawing.Color) =
   match col with
   | HSB(h, s, b) -> printfn " Hue: %f Saturation: %f Brightness: %f" h s b

let printAll col colorString =
  printfn "%s" colorString
  printRGB col
  printHSB col

printAll Color.Red "Red"
printAll Color.Black "Black"
printAll Color.White "White"
printAll Color.Gray "Gray"
printAll Color.BlanchedAlmond "BlanchedAlmond"

上述程式的輸出為:

Red
 Red: 255 Green: 0 Blue: 0
 Hue: 360.000000 Saturation: 1.000000 Brightness: 0.500000
Black
 Red: 0 Green: 0 Blue: 0
 Hue: 0.000000 Saturation: 0.000000 Brightness: 0.000000
White
 Red: 255 Green: 255 Blue: 255
 Hue: 0.000000 Saturation: 0.000000 Brightness: 1.000000
Gray
 Red: 128 Green: 128 Blue: 128
 Hue: 0.000000 Saturation: 0.000000 Brightness: 0.501961
BlanchedAlmond
 Red: 255 Green: 235 Blue: 205
 Hue: 36.000000 Saturation: 1.000000 Brightness: 0.901961

結合使用現用模式的這兩種方式,可讓您將資料分割並分解成適當的表單,並在最方便計算的表單中,對適當資料執行適當的計算。

產生的模式比對運算式可讓資料以非常易讀的方式撰寫,大幅簡化可能的複雜分支和資料分析程式碼。

部分現用模式

有時候,您只需要分割輸入空間的一部分。 在此情況下,您會撰寫一組部分模式,每個模式都符合某些輸入,但無法符合其他輸入。 不一定會產生值的現用模式稱為部分現用模式;它們具有屬於選項型別的傳回值。 若要定義部分現用模式,請在香蕉夾內的模式清單結尾使用萬用字元 (_)。 下列程式碼說明如何使用部分現用模式。

let (|Integer|_|) (str: string) =
   let mutable intvalue = 0
   if System.Int32.TryParse(str, &intvalue) then Some(intvalue)
   else None

let (|Float|_|) (str: string) =
   let mutable floatvalue = 0.0
   if System.Double.TryParse(str, &floatvalue) then Some(floatvalue)
   else None

let parseNumeric str =
   match str with
   | Integer i -> printfn "%d : Integer" i
   | Float f -> printfn "%f : Floating point" f
   | _ -> printfn "%s : Not matched." str

parseNumeric "1.1"
parseNumeric "0"
parseNumeric "0.0"
parseNumeric "10"
parseNumeric "Something else"

上述範例的輸出如下:

1.100000 : Floating point
0 : Integer
0.000000 : Floating point
10 : Integer
Something else : Not matched.

使用部分使用模式時,有時個別的選擇可能會斷續或互斥,但可以不必如此。 在下列範例中,模式 Square 和模式 Cube 不會斷續,因為某些數字同時是正方形和立方體,例如 64。 下列程式會使用 AND 模式來結合 Square 和 Cube 模式。 它會列印所有整數,最多 1000 個同時是正方形和立方體的整數,以及僅為立方體的整數。

let err = 1.e-10

let isNearlyIntegral (x:float) = abs (x - round(x)) < err

let (|Square|_|) (x : int) =
  if isNearlyIntegral (sqrt (float x)) then Some(x)
  else None

let (|Cube|_|) (x : int) =
  if isNearlyIntegral ((float x) ** ( 1.0 / 3.0)) then Some(x)
  else None

let findSquareCubes x =
   match x with
   | Cube x & Square _ -> printfn "%d is a cube and a square" x
   | Cube x -> printfn "%d is a cube" x
   | _ -> ()
   

[ 1 .. 1000 ] |> List.iter (fun elem -> findSquareCubes elem)

輸出如下所示:

1 is a cube and a square
8 is a cube
27 is a cube
64 is a cube and a square
125 is a cube
216 is a cube
343 is a cube
512 is a cube
729 is a cube and a square
1000 is a cube

參數化現用模式

現用模式一律會針對符合的項目至少採用一個引數,但也可能會採用其他引數,在此情況下,會套用參數化現用模式這個名稱。 其他引數允許特殊化一般模式。 例如,使用規則運算式剖析字串的現用模式,通常會包含規則運算式作為額外的參數,也會使用先前程式碼範例中定義的部分現用模式 Integer,如下列程式碼所示。 在此範例中,會提供針對各種日期格式使用規則運算式的字串,以自訂一般 ParseRegex 現用模式。 整數現用模式可用來將相符字串轉換成可傳遞至 DateTime 建構函式的整數。

open System.Text.RegularExpressions

// ParseRegex parses a regular expression and returns a list of the strings that match each group in
// the regular expression.
// List.tail is called to eliminate the first element in the list, which is the full matched expression,
// since only the matches for each group are wanted.
let (|ParseRegex|_|) regex str =
   let m = Regex(regex).Match(str)
   if m.Success
   then Some (List.tail [ for x in m.Groups -> x.Value ])
   else None

// Three different date formats are demonstrated here. The first matches two-
// digit dates and the second matches full dates. This code assumes that if a two-digit
// date is provided, it is an abbreviation, not a year in the first century.
let parseDate str =
   match str with
   | ParseRegex "(\d{1,2})/(\d{1,2})/(\d{1,2})$" [Integer m; Integer d; Integer y]
          -> new System.DateTime(y + 2000, m, d)
   | ParseRegex "(\d{1,2})/(\d{1,2})/(\d{3,4})" [Integer m; Integer d; Integer y]
          -> new System.DateTime(y, m, d)
   | ParseRegex "(\d{1,4})-(\d{1,2})-(\d{1,2})" [Integer y; Integer m; Integer d]
          -> new System.DateTime(y, m, d)
   | _ -> new System.DateTime()

let dt1 = parseDate "12/22/08"
let dt2 = parseDate "1/1/2009"
let dt3 = parseDate "2008-1-15"
let dt4 = parseDate "1995-12-28"

printfn "%s %s %s %s" (dt1.ToString()) (dt2.ToString()) (dt3.ToString()) (dt4.ToString())

上述程式碼的輸出如下:

12/22/2008 12:00:00 AM 1/1/2009 12:00:00 AM 1/15/2008 12:00:00 AM 12/28/1995 12:00:00 AM

現用模式不限於模式比對運算式,您也可以在 let 繫結上使用它們。

let (|Default|) onNone value =
    match value with
    | None -> onNone
    | Some e -> e

let greet (Default "random citizen" name) =
    printfn "Hello, %s!" name

greet None
greet (Some "George")

上述程式碼的輸出如下:

Hello, random citizen!
Hello, George!

不過請注意,只有單一案例的現用模式可以參數化。

// A single-case partial active pattern can be parameterized
let (| Foo|_|) s x = if x = s then Some Foo else None
// A multi-case active patterns cannot be parameterized
// let (| Even|Odd|Special |) (s: int) (x: int) = if x = s then Special elif x % 2 = 0 then Even else Odd

部分現用模式的結構標記法

根據預設,部分現用模式會傳回 option 值,這會牽涉到成功比對上 Some 值的配置。 或者,您可以透過使用 Struct 屬性,使用值選項作為傳回值:

open System

[<return: Struct>]
let (|Int|_|) str =
   match Int32.TryParse(str) with
   | (true, n) -> ValueSome n
   | _ -> ValueNone

必須指定屬性,因為不會簡單地將傳回型別變更為 ValueOption,來推斷結構傳回的用法。 如需詳細資訊,請參閱 RFC FS-1039

另請參閱