Some F # functions that I would like to see in C #, any help?

After the mess with F #, there are some really nice features that I think I'll skip when I want to return to C #, any tips on how I can refuse the following or duplicate their functionality even better:

  • Matching patterns (especially with discriminatory unions)
  • Discriminative Unions
  • Recursive functions (chapters and tails in lists)

Last but not least, message processing with Erlang.

+4
source share
4 answers

I am not sure how much this is really a question. However, here are some typical templates that I use to encode these functional constructs in C # (some of them are taken from my book, which has source code ).

Discriminative unions - there really is no good way to implement discriminatory unions in C # - the only thing you can do is implement them as a class hierarchy (with a base class representing the DU type and derived class for each DU case). You can also add the Tag property (from some type of enum ) to the base class to make it easier to check in which case the class represents. As far as I know, this is used in LINQ expression trees (which really should be discriminated against by the union).

Matching a pattern - you will probably never get this in a completely general way (e.g. with nested patterns), but you can simulate pattern matching on discriminatory joins like this (using the Option<int> which is either Some of int or None ):

 Option<int> value = GetOption(); int val; if (value.TryMatchSome(out val)) Console.WriteLine("Some {0}", val); else if (value.TryMatchNone()) Console.WriteLine("None"); 

Not ideal, but at least you get a relatively good way to extract values ​​from these cases.

Messaging - there is Concurrency and coordination lead time , which is in some way also based on messaging and can be used in C #. I bet you can also use the F # mailbox processor from C # using the iterator-based method that I described in this article and is also used in the Wintellect PowerThreading Library. However, I do not think that anyone has implemented a simple messaging library based on this idea.

This way you can simulate many of the functionality in C #, at least to some extent, and use some others without any problems (lambda functions and higher order functions). However, if you need full F # power, you just need to convince your company to start using F # :-).

+2
source

Use F # to make multiple libraries that you can call from C # .

One very good thing about F # is that it is still a .NET language. You can mix and match languages ​​in the CLR as much as you want ...

+6
source

Discriminative unions and pattern matching can be modeled in C #, although type definitions are a bit detailed (see How can I duplicate a F # type union in C # for some ideas). Here's the approach I advocated in this matter: type F # type T = ACase of A | BCase of B | CCase of C type T = ACase of A | BCase of B | CCase of C type T = ACase of A | BCase of B | CCase of C can be represented by an abstract C # class with some static helper methods.

 public abstract class T { public abstract X Match<X>(Func<A,X> aCase, Func<B,X> bCase, Func<C,X> cCase); private class ACase : T { private A a; public ACase(A a) { this.a = a; } public override X Match<X>(Func<A,X> aCase, Func<B,X> bCase, Func<C,X> cCase) { return aCase(a); } } private class BCase : T { private B b; public BCase(B b) { this.b = b; } public override X Match<X>(Func<A,X> aCase, Func<B,X> bCase, Func<C,X> cCase) { return bCase(b); } } private class CCase : T { private C c; public CCase(C c) { this.c = c; } public override X Match<X>(Func<A,X> aCase, Func<B,X> bCase, Func<C,X> cCase) { return cCase(c); } } public static T MakeACase(A a) { return new ACase(a); } public static T MakeBCase(B b) { return new BCase(b); } public static T MakeCCase(C c) { return new CCase(c); } } 

The match now looks like F #, but without labels. Equivalent to this F # code:

 function | A a -> 1 | B b -> 2 | C c -> 3 

Is this C # code:

 public static int MatchDemo(T t) { return t.Match( a => 1, b => 2, c => 3); } 
+2
source

Departure

match / template idea

for some crazy ways to try pattern matching in C #.

0
source

Source: https://habr.com/ru/post/1304326/


All Articles