我想在不使用临时变量或 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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)