sdg*_*sdh 1 syntax f# pattern-matching pointfree
考虑以下代码:
type Fruit = Apple | Banana
let totalCost fruits =
fruits
|> Seq.map (fun fruit ->
match fruit with
| Apple -> 0.50
| Banana -> 0.70
)
|> Seq.sum
Run Code Online (Sandbox Code Playgroud)
我totalCost可以用fruit删除标识符的方式重写得更简洁吗?
像这样:
// Not real code
let totalCost fruits =
fruits
|> Seq.map (
match
| Apple -> 0.50
| Banana -> 0.70
)
|> Seq.sum
Run Code Online (Sandbox Code Playgroud)
您要寻找的关键字是function:
|> Seq.map (
function
| Apple -> 0.50
| Banana -> 0.70
)
Run Code Online (Sandbox Code Playgroud)
function 屈服于 fun x -> match x with
| 归档时间: |
|
| 查看次数: |
60 次 |
| 最近记录: |