Method Chaining vs |> Pipe Operator

后端 未结 5 1862
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 00:54

So I have the following code:

// Learn more about F# at http://fsharp.net
open System
open System.Linq
open Microsoft.         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 01:58

    Pipelining supports F#'s left-to-right type inference. a.GroupBy requires that the type of a is already known to be seq<_>, whereas a |> Seq.groupBy itself infers a to be seq<_>. The following function:

    let increment items = items |> Seq.map (fun i -> i + 1)
    

    requires a type annotation to be written using LINQ:

    let increment (items:seq<_>) = items.Select(fun x -> x + 1)
    

    As you get comfortable with the functional style you'll find ways to make your code more concise. For example, the previous function can be shortened to:

    let increment = Seq.map ((+) 1)
    

提交回复
热议问题