我想在不使用临时变量或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"
是否可以编写类似于Code#2的代码?或者我必须选择Code#3还是#4?谢谢。
答案 0 :(得分:16)
let result =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
|> function
| Case1 -> "Output 1"
| Case2 -> "Output 2"
| _ -> "Other Output"