English 中文(简体)
方法链条
原标题:Method Chaining vs |> Pipe Operator

因此,我有以下法典:

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

let a = [1; 2; 3; 4; 54; 9]

let c = a |> List.map(fun(x) -> x*3) |> List.filter(fun(x) -> x > 10)
let d = a.Select(fun(x) -> x*3).Where(fun(x) -> x > 10)

for i in c do
    Console.WriteLine(i)

for i in d do
    Console.WriteLine(i)

这两种情况似乎都是一样的,但大多数F#例见我使用“The >;管道运营商,而I m则更多地用于计算链条(a.l.a. C# Linq)。 后者也稍有缩短,不过,它们又增加了共同点。 现在使用C# Linq syntax的i m,但这一数值比任何真正的设计决定都多。

我是否知道或基本相同的考虑?

Edit: The other consideration is that the Pipe syntax is significantly more "noisy" than the Linq syntax: the operation I am doing (e.g. "map") is really short and in lowercase, while each one is preceded by this Huge "|> List" that, apart from making it longer distracts the eye away from the tiny, lowercase method name. Even StackOverflow s syntax highlighter highlights the wrong (irrelevant) thing. Either that or I m just not used to it.

最佳回答

管道支持 F# 左对右型推论。 <代码>a.GroupBy要求a的类型已众所周知为seq<_>,而a ́ >;Seq.groupBy。 载于<代码>a 至seq<_>。 以下职能:

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

要求使用LINQ书写字句:

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

由于你对功能风格感到欣慰,你设法使你的守则更加简明扼要。 例如,以前的职能可以缩短到:

let increment = Seq.map ((+) 1)
问题回答

其他人已经解释了两种风格之间的大多数差异。 我认为,最重要的是类型的推论(丹尼尔提到),这种推论与基于管道和诸如<代码>List.map等功能的正统法体格比照。

另一种不同之处是,在使用F#风格时,你可以比较容易地看到,部分计算是在评价被迫时进行的。

let foo input =
  input 
  |> Array.map (fun a -> a) // Takes array and returns array (more efficient)
  |> Seq.windowed 2         // Create lazy sliding window
  |> Seq.take 10            // Take sequence of first 10 elements
  |> Array.ofSeq            // Convert back to array

I also find the |> operator more syntactically convenient, because I never know how to correctly indent code that uses .Foo - especially where to place the dot. On the other hand, |> has quite established coding style in F#.

In general, I recommend using the |> style because it is "more standard". There is nothing wrong with using the C# style in F#, but you may find that writing code in a more idiomatic style makes it easier to use some interesting functional programming concepts that work better in F# than in C#.

Actually the pipe operator does nothing but swap the function and argument around, to my knowledge there s no difference between f1 (f2 3) and 3 |> f2 |> f1 besides that the latter is easier to read when you re chaining a lot together.

edit: it s actually defined just as that: let inline (|>) x f = f x.

I guess the reason you tend to see the List.map approach more than Linq is because in OCaml (the predecessor to F#), these operators have always been there, so this style of coding is really entrenched in the way functional programmers think. A List is a very basic concept in F#, it is slightly different from a IEnumerable (that s closer to a Seq).

Linq is largely an undertaking to bring these functional programming concepts to C# and VB. So they re in the .Net platform and therefor available, but in F# they re kind of redundant.

清单:地图是一项非常简单的操作,,因为林克办法在整个框架中带来了一些间接费用的zy评价。 But,我认为,在你真正使用之前,这不会产生重大影响。 我在一些人的谈话中听到,C#汇编者之所以使用Linq,是因为这一原因,但在正常情况下,你不会听说过。

因此,从总体上来说,你最能做的是,没有权利或错误。 在个人方面,我会与名单操作者接触,因为他们在F部分中重新采用了更高的标准。

GJ

很可能最终会发生的一个事情是轻率问题。 例如:

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<string, (string * int)>) -> x.Key, x.Count())

for i in c do
    Console.WriteLine(i)

for i in d2 do
    Console.WriteLine(i)

To my understanding, the F# |> operator was introduced to make sequence operations look like LINQ queries, or better to make it look similar to C# extension method chaining. List.map and filter, in fact, are functions in a "functional" way: get a sequence and a f as input, return a sequence. Without pipe, the F# variant will be

filter(fun(x) -> x > 10, map(fun(x) -> x*3, a))

Notice that visually the order of the functions is reversed (application is still in the same order) : with |> they look more "natural", or better, they look more similar to the C# variant. C# achieves the same goal through extension methods: remember that the C# one is actually

Enumerable.Where(Enumerable.Select(a, f1), f2)

Enumerable.Select is a function where the first parameter is a "this IEnumerable", which is used by the compiler to transform it to a.Select... In the end, they are language facilities (realized through a compiler transformations in C#, and using operators and partial application in F#) to make nested function calls look more like a chain of transformations.





相关问题
Which name for a "smart" dictionary (hashtable)?

I m looking for a good name for a custom dictionary which automatically initializes the value for a requested key if it doesn t exist, using a delegate. The indexer implementation should help ...

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Python dictionary simple way to add a new key value pair

Say you have, foo = bar d = { a-key : a-value } And you want d = { a-key : a-value , foo : bar } e = { foo :foo} I know you can do, d[ foo ] = foo #Either of the following for e e = { foo :foo}...

Storing ints in a Dictionary

As I understand, in Objective-C you can only put Objects into dictionaries. So if I was to create a dictionary, it would have to have all objects. This means I need to put my ints in as NSNumber, ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签