2017-06-25 10 views
0

は、私は、彼らがアクティブパターンに「_」を使用していることがわかります。"_"がアクティブなパターンで使用されているのはなぜですか?この<a href="https://fsharpforfunandprofit.com/posts/convenience-active-patterns/" rel="nofollow noreferrer">link</a>から

let (|Int|_|) str = 
    match System.Int32.TryParse(str) with 
    | (true,int) -> Some(int) 
    | _ -> None 

私はアンダースコアが表示されていない別のlinkからの例を見てみましょう。

let (|Even|Odd|) input = if input % 2 = 0 then Even else Odd 

これら2つの実装の違いは何ですか?

+7

一つのパターンが完了し、他の部分です。両方の名前、両方の例、両方の完全な説明は_you_リンクのページにあります。あなたの質問は何ですか? – ildjarn

+1

この程度SOドキュメントの記事もありますhttps://stackoverflow.com/documentation/f%23/962/active-patterns/23523/complete-and-partial-active-patterns#t=201706260814429684968 –

答えて

4

これはthis doc pageに書かれています。
(|Int|_|) str =...部分アクティブパターンです。 let (|Even|Odd|) input =...はアクティブパターンです。下記参照。

// From doc link 
module ActivePatternSample = 
    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 

// Rewrite to partial Active Pattern 
module PartialActivePatternSample = 
    let (|Even|_|) input = 
    match (input%2) with 
    | 0 -> Some input 
    | _ -> None 

    let (|Odd|_|) input = 
    match input%2 with 
    | 1 -> Some input 
    | _ -> None 

    let TestNumber input = 
    match input with 
    | Even input -> printfn "%A is even" input 
    | Odd input -> printfn "%A is odd" input 
    | _ -> printfn "can not come here" 
+1

いただきありがとうございますあなた編集。私は英語で編集するように注意しようとします。日本のF#に関する質問はほとんどありません。だから私はここに来た。 –

+0

ご理解いただきありがとうございます。スタックオーバーフローで幸運! = D –

関連する問題

 関連する問題