我想在不使用临时变量或 lambda 的情况下将变量通过管道转发到匹配情况。这个想法:
let temp =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
let result =
match temp with
| Case1 -> "Output 1"
| Case2 -> "Output 2"
| _ -> "Other Output"
我希望写一些类似于以下内容的东西:
// 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"
我拥有的最接近的东西是使用 lambda 的以下内容。但我认为下面的代码也不是那么好,因为我仍在"命名"临时变量。
let result =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
|> fun temp ->
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的代码?还是我必须选择代码 #3 或 #4?谢谢。
let result =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
|> function
| Case1 -> "Output 1"
| Case2 -> "Output 2"
| _ -> "Other Output"