Live data from Hacker News

More functional C#

news.ycombinator.com

1–10 of 98 posts

More functional C#

#1
I added this code as a comment to a thread that was almost all the way off the front page by the time I got to it. It was a response to some 'functional folks' criticizing the verbosity of c#. While you can certainly do c# the java way, you can also do it in a near functional way.

I decided to post the code as a full submission because I think c# often gets dismissed as Java copy-cat, which hasn't been true in a decade.

The code (non-destructive, no side effects) calculates square roots :

    public static void Main()
    {
        const int n = 21;

        Observable.Generate(1, x=>true, x=> 0.5 * (x + (n/x)), x=>x )
        .Scan(new {prv = 0d, cur = 0d}, (prev, curr) => new {prv = prev.cur, cur = curr})
        .FirstAsync(_ => Math.Abs(_.cur - _.prv)  _.cur)
        .Subscribe(Console.WriteLine);

        // this is just to compare values, so is not part of the solution
        Console.WriteLine(Math.Sqrt(n)); 
        Console.ReadLine();
    }
Read the more OO c# implementation that inspired me here: https://news.ycombinator.com/item?id=7044497

Read the Haskell code that inspired both here: https://news.ycombinator.com/item?id=7043943

Re: More functional C#

#2
Maybe it's strange for them to see something that is both functional and highly practical?

PS: once Roslyn get's finally released, Java will never catch up again.

Re: More functional C#

#4

I'm guessing Observable, Scan, FirstAsync and Subscribe are all from the Rx reactive/observable framework? I haven't seen them before.

Scan and Observable are part of System.Reactive the other two are I think part of System or System.Data.Entity

Re: More functional C#

#5
You can write Java code which works similarly to your C# code by using RxJava: https://github.com/Netflix/RxJava

Or Python, via RxPy: https://github.com/Reactive-Extensions/RxPy

Or JavaScript, via RxJs: https://github.com/Reactive-Extensions/RxJS

Or Ruby, via Rx.rb: https://github.com/Reactive-Extensions/Rx.rb

Or Objective-C, via Rx.ObjC: https://github.com/Reactive-Extensions/Rx.ObjC

Your code is a good demonstration of how Rx works though, and it's nice to know it's becoming more popular.

Re: More functional C#

#6

You can write Java code which works similarly to your C# code by using RxJava: https://github.com/Netflix/RxJava Or Python, via RxPy: https://github.com/Reactive-Extensions/RxPy Or JavaScript, via RxJs: https://github.com/Reactive-Extensions/RxJS Or Ruby, via Rx.rb: https://github.com/Reactive-Extensions/Rx.rb Or Objective-C, via Rx.ObjC: https://github.com/Reactive-Extensions/Rx.ObjC Your code is a good demonstratio…

Rx was invented by Microsoft, so C# is a rightful first choice for Rx examples :). That code doesn't demonstrate Rx per se, it demonstrates how lambda expressions, extension methods, anonymous types, type inference enable a succinct backwards-compatible (to some extent) functional syntax in C#.

Re: More functional C#

#8
C#'s functional side is why I actually really enjoy working with it. I get to use a nice blend of OO and functional, where each makes sense. OO more on a macro scale, organising code.

Re: More functional C#

#9

I'm guessing Observable, Scan, FirstAsync and Subscribe are all from the Rx reactive/observable framework? I haven't seen them before.

Observable, Scan and FirstAsync are from Rx. (FirstAsync is roughly equivalent to LINQ First(), except that it doesn't block.) This particular Subscribe method is also part of Rx, but it's basically a helper method around the BCL System.IObservable.Subscribe(IObserver) that saves you having to implement IObserver.
Post reply on HN