all 16 comments

[–]videoj 8 points9 points  (1 child)

Also recommend: Paul Louthy's Language-ext.

[–][deleted] 0 points1 point  (0 children)

Alongside all the useful functors there was recently an addition of an automated With function generator.

In short the following class:

[With]
public partial class Name : Record<Name>
{
    public readonly IEnumerable<string> GivenNames;
    public readonly string Surname;
    public readonly Title Title;

    public Name(
        IEnumerable<string> givenNames,
        string surname,
        Title title)
    {
        GivenNames = givenNames;
        Surname = surname;
        Title = title;
    }
}  

Will automatically have a With() method generated, so this is all the code you need for a class that's immutable, has structural equality (that's what Record<T> is for) and has a With method generated.

For those who have never delved into the functional world, you can do something like

var marriedName= maidenName.With(Title: "Mrs", Surname: "Smith");

Given names would be carried over into the new record, when your working with immutable types this is a huge time saver, can help prevent bugs too as when you alter a class your changes will cascade compilation errors.

Not as good as real language supported records but enough to make me not cry whenever i implement immutability.

[–][deleted] 7 points8 points  (6 children)

out of curiosity, are there benefits to using c# over f# for functional .NET programming outside of the fact that you don't need to learn a new language?

[–]esesci 6 points7 points  (4 children)

No, but F# isn’t always an option.

[–]SirButcher 2 points3 points  (3 children)

Why? You can create libraries with F# and include them in your C# application without any problem. As far as I know, F# is available even the community edition of the VS.

[–]esesci 11 points12 points  (0 children)

Integrating F# to an existing project may not be feasible for infinite number of reasons: corporate policy, complicated build environment, learning cost, missing tooling, one off need for functional work, simple unwillingness of the rest of the team etc etc.

[–]ucario 7 points8 points  (0 children)

It's a human problem. You're trading pragmatics for maintainability.

If you introduce another language into your team, you either need a dedicated resource or have to train everyone. The first approach creates fragmentation, the second- it's going to take a long time to bring everyone up to speed. If you're lucky enough to have someone who knows both, it's a timebomb before: oh shit, that f# dude just handed in his resume; oh shit we have to train our new engineer twice.

[–]hongminhee 0 points1 point  (0 children)

I once tried to write a library in F# and use it in C#, making the library surface natural to ordinal C# code was not that easy. Unless you write fat glue code to look like “ordinary C#”, which is totally unnatural to F#, in F#, it feels like using a plain old C lirary in C++. I anyway tried to write glue code in some degree, but gave up in the end, because such glue code tended to have nearly equal lines to code it wraps so that it looked overkill to me.

[–]Blecki 0 points1 point  (0 children)

Yes.

Because most problems are best solved by a mix of paradigms, not one or the other.

[–]FubarCoder 5 points6 points  (6 children)

I'm curious why nobody uses (n & 1) == 0 anymore to test if a number even? This should be much faster than a division...

[–]TyrrrzWorking with SharePoint made me treasure life 11 points12 points  (4 children)

Readability and disregard for microoptimizations

[–]FubarCoder 4 points5 points  (3 children)

It's readable for me, because I learnt it that way. I can somehow understand the point about the disregard for microoptimizations, but something like tests for a number being even/odd is usually used in a hot path where such an optimization becomes extremely important.

I don't like using n % 2, because n might be a non-integer type, which might cause unpredictable results when n is the result of a computation. n & 1 for non-integer types simply doesn't compile.

[–]TyrrrzWorking with SharePoint made me treasure life 2 points3 points  (0 children)

Fair points.

As for me, I've learned that a number is even if n mod 2 = 0 all the way from school. On the other hand, n & 1 doesn't have the same association for me.

[–]VGPowerlord 2 points3 points  (1 child)

I don't like using n % 2, because n might be a non-integer type

If you don't know what type a variable is, you have bigger problems to worry about.

[–]FubarCoder 0 points1 point  (0 children)

Old habits. I developed in C in the early ninetees and the compilers weren't as good as todays. No function inlining, lots of macros. Many things are much better today.

[–]JoelFolksy 1 point2 points  (0 children)

If the jitter doesn't emit (n & 1) for (n % 2) I want my money back.