在F#中以无点样式进行匹配?

时间:2019-07-07 13:05:49

标签: 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

我可以用删除totalCost标识符的方式重写fruit来使其更简洁吗?

类似这样的东西:

// Not real code
let totalCost fruits = 
  fruits
  |> Seq.map (
    match
    | Apple -> 0.50
    | Banana -> 0.70
  )
  |> Seq.sum

1 个答案:

答案 0 :(得分:7)

您要查找的关键字是function

|> Seq.map ( 
    function
    | Apple -> 0.50 
    | Banana -> 0.70
)

function降为fun x -> match x with