NickL
NickL

Reputation: 1960

Why are the .NET LINQ methods re-written in the F# core library?

I've seen on github that the F# core library has a re-written set of LINQ methods. What is the reason for this? And why are they different to the C# methods?

Edit

I'm not really sure what opinion has to do with this, but perhaps the question of why they're duplicated and why they're slightly different should be separated. An example is with the take method: in C# a call to take 5 elements of a 4-element sequence will return that 4-element sequence, whereas in F# it will throw an exception.

Upvotes: 0

Views: 230

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243061

There is a couple of reasons why the Seq module contains similar functionality to LINQ:

  • F# had it first :-) The Seq module in F# existed long before LINQ was added to C#. I guess the C# team might have decided to just use the Seq module as an implementation of LINQ, but it is not hard to see why that did not happen...

  • C# uses extension methods while F# uses functions - in principle, F# functions are compiled as static methods, so it would not be impossible to imagine that they could be marked as extension methods accessible from C# - but it is not simple.

  • F# functions are curried - this is tricky - in C#, the this IEnumerable<T> parameter is usually the first one, while in F# it is the last one (to allow partial function application)

  • F# functions take FSharpFunc or Func - another technical difference - F# functions are compiled differently (for performance reasons)

  • Naming is also different. For F#, this is important to keep partial OCaml compatibility.

Upvotes: 10

Related Questions