2017-11-07 9 views
5

का उपयोग किए बिना एक मैच केस को पाइप करें, मैं एक चर वैरिएबल या लैम्ब्डा का उपयोग किए बिना एक चर मामले में एक चर को आगे बढ़ाना चाहता हूं। विचार:एफ #, पाइप परिवर्तनीय

// IDEAL CODE (with syntax error) 
let result = 
    x 
    |> Function1 
    |> Function2 
    // ........ Many functions later. 
    |> FunctionN 
    |> match with // Syntax error here! Should use "match something with" 
     | Case1 -> "Output 1" 
     | Case2 -> "Output 2" 
     | _ -> "Other Output" 

निकटतम बात है कि मैं एक लैम्ब्डा का उपयोग करके निम्नलिखित है:

let temp = 
    x 
    |> Function1 
    |> Function2 
    // ........ Many functions later. 
    |> FunctionN 

let result = 
    match temp with 
    | Case1 -> "Output 1" 
    | Case2 -> "Output 2" 
    | _ -> "Other Output" 

मैं निम्न जैसा कुछ लिखने के लिए उम्मीद है। लेकिन मुझे लगता है कि नीचे दिया गया कोड वास्तव में बहुत अच्छा नहीं है, क्योंकि मैं अभी भी temp चर "नामकरण" कर रहा हूं।

let result = 
    match x 
      |> Function1 
      |> Function2 
      // ........ Many functions later. 
      |> FunctionN with 
    | Case1 -> "Output 1" 
    | Case2 -> "Output 2" 
    | _ -> "Other Output" 

क्या यह संभव है एक कोड संहिता # 2 के लिए इसी तरह लिखने के लिए:

let result = 
    x 
    |> Function1 
    |> Function2 
    // ........ Many functions later. 
    |> FunctionN 
    |> fun temp -> 
     match temp with 
     | Case1 -> "Output 1" 
     | Case2 -> "Output 2" 
     | _ -> "Other Output" 

दूसरी ओर, मैं सीधे "अस्थायी" चर कोड का एक बड़ा हिस्सा साथ की जगह ले सकता ? या मुझे कोड # 3 या # 4 चुनना है? धन्यवाद।

उत्तर

12
let result = 
    x 
    |> Function1 
    |> Function2 
    // ........ Many functions later. 
    |> FunctionN 
    |> function 
     | Case1 -> "Output 1" 
     | Case2 -> "Output 2" 
     | _ -> "Other Output"