26

So I have the following code:

// 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)

Both seem to do the same thing, but most F# examples I see use the |> pipe operator, while I'm more used to method chaining (a.l.a. C# Linq). The latter is also somewhat shorter, albeit somewhat more crunched together. For now i'm using the C# Linq syntax, but that's more of habit/inertia rather than any real design decision.

Are there any considerations that I should know about, or are they basically identical?

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.

5 Answers 5

23

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)
6
  • Good example @Daniel. That's why I wrote "look more "natural", or better, they look more similar to the C# variant". I prefer a more functional style when working with F# (probably due to my previous experience with ML), so I prefer your last function. It is a matter of taste and "team style" IMO Oct 8, 2011 at 17:04
  • @Daniel: Interesting tradeoff. I read your post earlier, but only just grokked exactly what the implications are. Essentially Seq.map is like declaring items to be a seq before using, while items.map doesn't tell you anything about items. Does this mean that even if you later put a known seq into the increment function, the type inference engine won't go back up to the definition of increment and decide it takes seq?
    – Li Haoyi
    Oct 10, 2011 at 8:40
  • @LiHaoyi: Although later uses can restrict the type of a function, your function may only interact with the type as it is known at the point of your function declaration. Type inference works top-to-bottom, left-to-right, so the compiler can only use type information available prior to the point of use.
    – Daniel
    Oct 10, 2011 at 14:04
  • @Daniel: Any chance you could unpack your last version a bit? I have a vague idea of what's going on, but I'm not clear on why that would work and I suspect knowing why would make inroads on other concepts; e.g. currying...
    – Phil H
    Apr 24, 2014 at 11:33
  • 1
    @Daniel: Yes, thank you. So the function (+) is being curried to create a 1:1 mapping function, and that is being passed to map, which takes it as the first argument. By constructing this using the let, we curry the first argument, leaving the second argument (namely, the sequence to map), so this is now a function taking 1 argument (a sequence) and returning the mapped sequence, I think.
    – Phil H
    Apr 28, 2014 at 10:44
15

Others already explained most of the differences between the two styles. In my point of view, the most important is type inference (mentioned by Daniel) which works more nicely with the idiomatic F# style based on pipelining and functions like List.map.

Another difference is that when using the F# style, you can more easily see which part of the computation evaluates lazily, when the evaluation is forced etc., because you can combine functions for IEnumerable<_> (called Seq) and functions for lists or arrays:

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#.

2
  • 1
    I know this is a "dummy" example, but would there ever be a reason to do an Array.map function like the one you show?
    – Mike K
    Oct 10, 2011 at 22:25
  • 1
    off subject, but I really wish we get method chaining like this in C++ someday. Aug 20, 2013 at 21:13
8

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.

Also List.map is a very simple operation, whereas the Linq approach brings in the whole framework with lazy evaluation etc that brings some overhead. But I don't think that will make a significant difference until you really use it a lot. I heard in some talk that the reason the C# compiler doesn't use Linq more is because of this reason, but in normal life you're not likely to notice.

So all in all, do what you feel best with, there's no right or wrong. Personally I would go with the List operators because they're more standard in 'idiomatic' F#.

GJ

6
  • 5
    seq<T> and IEnumerable<T> aren't just close, they are the same. seq<T> is just an alias for IEnumerable<T>.
    – svick
    Oct 8, 2011 at 16:41
  • 4
    LINQ approach does not really bring in "the whole framework with lazy evaluation". It is just a syntactic difference and it relies on the implementation of Select (which does not necessarily have to be the one for IEnumerable). Aside, you can get lazy evaluation with F# using Seq.map. Oct 8, 2011 at 17:52
  • You're right, I checked with ILSpy and they're just basic enumerators. I was always under the impression that the chained functions were kept as an expression tree and only optimized compiled and executed when the result was asked for, pretty sure I got that from a Linq talk on C9 when it was first introduced. Maybe I'm confused with Linq to SQL.
    – gjvdkamp
    Oct 8, 2011 at 18:38
  • @gjvdkamp Yes - that happens only in implementations for accessing databases (LINQ to SQL or Entities). The trick there is that the Select method expects argument of type Expression<Func<...>> instead of just Func<...>. Aside - using these methods from F# won't work, because F# quotations work differently than C# expression trees. Oct 8, 2011 at 19:13
  • While the syntax is similar I would not call OCaml the predecessor - OCaml is in many ways more powerful than F# (for example it's module system)
    – Random Dev
    Oct 13, 2014 at 5:07
3

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

for i in c do
    Console.WriteLine(i)

for i in d2 do
    Console.WriteLine(i)
2

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.

3
  • But given that I can use the extension-method-chaining style in F# as well, should I still stick with the F# piping style, or does it not matter? I'm thinking there are probably some funny edge cases that i'm not thinking of where their behaviour does differ.
    – Li Haoyi
    Oct 8, 2011 at 16:36
  • The |> operator isn't really a compiler transformation. It's just normal F# operator. It can work this way thanks to partial function application.
    – svick
    Oct 8, 2011 at 16:48
  • @LiHaoyi that depends on your codebase. Probably it is wiser to use the F# way, as other code you will use (written by others, in external libraries, etc.) will probably follow the F# pattern. Otherwise, if you work in a team that follows the C# conventions, stick with that. Oct 8, 2011 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.