Method Chaining vs |> Pipe Operator

后端 未结 5 1820
爱一瞬间的悲伤
爱一瞬间的悲伤 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

    Well one thing that you will probably run into eventually is problems with type inference. Look at this example for instance:

    open System
    open System.Linq
    open Microsoft.FSharp.Collections
    
    let a = ["a", 2; "b", 1; "a", 42; ]
    
    let c = a |> Seq.groupBy (fst) |> Seq.map (fun (x,y) -> x, Seq.length y)
    
    //Type inference will not work here
    //let d1 = a.GroupBy(fun x -> fst x).Select(fun x -> x.Key, x.Count())
    
    //So we need this instead
    let d2 = a.GroupBy(fun x -> fst x).Select(fun (x : IGrouping) -> x.Key, x.Count())
    
    for i in c do
        Console.WriteLine(i)
    
    for i in d2 do
        Console.WriteLine(i)
    

提交回复
热议问题