55

In F# interactive, I can use String.Join("+", ["a"; "b"]) successfully, but

["a"; "b"] |> String.Join "+"

produces an error:

Script1.fsx(79,15): error FS0001: This expression was expected to have type
    string list -> 'a    
but here has type
    string

How do I use String.Join passing a collection using pipeline?

P.S. The same problem is with lines |> File.WriteAllLines "filename.txt"

2 Answers 2

86

String.Join is a .NET method. When using a .NET method, F# views it as a function that takes a tuple as an argument (when calling it you write parameters as f(a, b)). The |> operator can be used with functions that use the curried form of parameters (and can be called by writing f a b).

You can use a function String.concat from the F# library (which does the same thing) instead:

["a"; "b"] |> String.concat "+"

EDIT File.WriteAllLines is the same case. If you want to use it as part of a pipeline, you can write an F# function that wraps the call:

let writeAllLines file (lines:seq<string>) =
  System.IO.File.WriteAllLines(file, lines)

In general, you can use |> with .NET methods only if you want to write all arguments on the left side of the operator. You can for example write:

("+", ["a"; "b"]) |> System.String.Join

... but that doesn't fit with the usual use of pipeline operator. When working with .NET API, it is usually better to use a C#-like style of programming (without pipelining), because pipeline works well only with functional libraries.

7
  • Thanks! By the way, any luck with File.WriteAllLines? Nov 25, 2010 at 15:39
  • @modosansreves: I added answers to your other questions. Nov 25, 2010 at 15:48
  • I think it has to be ("+",[|"a","b"|]) |> System.String.Join, taking an array of strings. Doesn't really matter though, it still makes my eyes bleed.
    – cfern
    Nov 25, 2010 at 16:20
  • 1
    @cfern: I think the original works too, because there is an overload taking seq<string>. Nov 25, 2010 at 17:11
  • 1
    @Tomas Petricek: the IEnumerable overload is only present in .Net 4 (which explains why it didn't work for me in VS2008). I learned something new today. :)
    – cfern
    Nov 26, 2010 at 8:35
24

I thought I would weigh in with an alternative. The OP specifically asked about String.Join, and Tomas Petricek's answer is absolutely correct for that method (use String.concat from the F# library).

If you're talking about another method that takes tupled arguments but you want to use them as curried, you can use this helper function:

let partial f x y = f(x, y)

This allows you to pass a method to 'partial' and get back a curried function. Use it like this:

let partial f x y = f(x, y)
myCollection |> partial String.Join "&"
1
  • A good name for this function might be "curry"; then you could also have "uncurry"
    – Jwosty
    Sep 24, 2021 at 17:26

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.