2

More particularly, I really want an immutable/shared linked list, and I think having immutable maps and sets would be nice too. As long as I don't have to worry about the core implementation, I can easily add extension methods/subclass/wrap it to provide a reasonably slick external interface for myself to use.

Is there any reason I shouldn't do this? Performance, incompatibility, etc.?

Li Haoyi
  • 15,330
  • 17
  • 80
  • 137
  • 1
    This is a *very* general question and therefore virtually impossible to answer. Maybe you can confine it to one case with code snippets? An equally general answer would be: it depends. Immutability can be enforced in c# as well, but when specific F# features (like better type inference) or data types (like option) do a better job, well... – Gert Arnold Nov 23 '11 at 08:05
  • @GertArnold: Specifically I am talking about FSharpList, FSharpSet, FSharpMap, and the possibility of calling them from c# as an alternative to rolling my own immutable list/set/map in C#, and whether this is a good/bad idea. – Li Haoyi Nov 23 '11 at 14:01
  • I think Thomas gave a good answer to that. Oliver Sturm has a nice [book with free source code](http://www.wrox.com/WileyCDA/WroxTitle/Functional-Programming-in-C-Classic-Programming-Techniques-for-Modern-Projects.productCd-0470744588,descCd-DOWNLOAD.html) on doing functional stuff the C#-way. But I think you would want to make an F# library with a [CLS-compliant](http://msdn.microsoft.com/en-us/library/bhc3fa7f.aspx) API. – Gert Arnold Nov 23 '11 at 14:19

2 Answers2

7

FSharpx includes a couple of "adapters" so that F# collections can be used more comfortably in C#. Here's a short example:

var a = FSharpList.Create(1, 2, 3);
var b = a.Cons(0);
b.TryFind(x => x > 4)
 .Match(v => Console.WriteLine("I found a value {0}", v),
        () => Console.WriteLine("I didn't find anything"));

There's not much documentation right now, but you can use the tests for reference. It doesn't include absolutely every operation (I don't mind directly using things like MapModule in C# too much), but if you find anything you need missing, please fork the repository and add it!

I also blogged about this a few weeks ago.

Or you can try and use one of these implementations of persistent collections in C#.

Community
  • 1
  • 1
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
5

The types in the F# library (such as Set, Map and list) were not designed to be used from C#, so I wouldn't generally recommend using them directly. It can be done and some basic operations will work well (e.g. adding elements to an immutable map and checking if an element exists). However, there are some issues:

  • F# also has functionality in modules (MapModule for an immutable map) and as a C# user, you would expect to see these as members.

  • F# functions are not represented as Func<_, _> delegates, but using some special F#-specific way. This means that using higher-order functions will be difficult.

So, in summary, I think that a better approach is to wrap the F# data type into a class (implemented in F#) that exposes the methods you need to a C# developer in a friendly way. You can e.g. easily declare an F# method that takes Func<_, _> delegate and calls F# higher-order function in a module.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553