我想在不使用临时变量或lambda的情况下将变量传递给匹配大小写.想法:
let temp =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
let result =
match temp with
| Case1 -> "Output 1"
| Case2 -> "Output 2"
| _ -> "Other Output"
Run Code Online (Sandbox Code Playgroud)
我希望写一些类似于以下内容:
// 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"
Run Code Online (Sandbox Code Playgroud)
我最接近的是使用lambda的以下内容.但我认为下面的代码也不是那么好,因为我仍在"命名"临时变量.
let result =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
|> fun temp ->
match temp with
| Case1 -> "Output 1"
| Case2 -> "Output 2"
| _ -> "Other Output"
Run Code Online (Sandbox Code Playgroud)
另一方面,我可以直接用大块代码替换"temp"变量:
let result =
match x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN with
| Case1 -> "Output 1"
| Case2 -> "Output 2"
| _ -> "Other Output"
Run Code Online (Sandbox Code Playgroud)
是否可以编写类似于Code#2的代码?或者我必须选择Code#3还是#4?谢谢.
hve*_*ter 19
let result =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
|> function
| Case1 -> "Output 1"
| Case2 -> "Output 2"
| _ -> "Other Output"
Run Code Online (Sandbox Code Playgroud)