在 F# 中以无点样式匹配?
Matching in a point-free style in F#?
考虑这段代码:
type Fruit = Apple | Banana
let totalCost fruits =
fruits
|> Seq.map (fun fruit ->
match fruit with
| Apple -> 0.50
| Banana -> 0.70
)
|> Seq.sum
我可以重写 totalCost
以使其更简洁,从而删除 fruit
标识符吗?
像这样:
// Not real code
let totalCost fruits =
fruits
|> Seq.map (
match
| Apple -> 0.50
| Banana -> 0.70
)
|> Seq.sum
您要找的关键词是function
:
|> Seq.map (
function
| Apple -> 0.50
| Banana -> 0.70
)
function
脱糖为 fun x -> match x with
考虑这段代码:
type Fruit = Apple | Banana
let totalCost fruits =
fruits
|> Seq.map (fun fruit ->
match fruit with
| Apple -> 0.50
| Banana -> 0.70
)
|> Seq.sum
我可以重写 totalCost
以使其更简洁,从而删除 fruit
标识符吗?
像这样:
// Not real code
let totalCost fruits =
fruits
|> Seq.map (
match
| Apple -> 0.50
| Banana -> 0.70
)
|> Seq.sum
您要找的关键词是function
:
|> Seq.map (
function
| Apple -> 0.50
| Banana -> 0.70
)
function
脱糖为 fun x -> match x with