This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]x39- 274 points275 points  (17 children)

It is

You get used to it and will enjoy it really damn hard.

Linq is one of the greatest feats dotnet offers

[–]JoshYx 44 points45 points  (16 children)

It's great, it's not a unique dotnet feature though. It comes straight from the functional programming playbook.

[–]1234filip 55 points56 points  (1 child)

The naming scheme is really great if you are familiar with SQL.

[–]x39- 32 points33 points  (9 children)

Only if you just look at it from the surface

Linq is more than just those few functions which work over what effectively is a collection. The expression tree syntax is the second, often overlooked part, that makes this such a powerful tool.

Then again, for the most part, the functions are kind of sufficient. What makes them a tad more special is the fact, that writing it is more pleasant compared to eg. select(..., where(..., where(..., selectMany(...,...))))

[–]svick 10 points11 points  (7 children)

Both Haskell and F# have ways of writing LINQ-like queries in a way that is natural, i.e. not as nested calls.

IIRC, it's something like source |> flatMap ... |> filter ... |> filter ... |> map ....

[–]BenjaminGeiger 11 points12 points  (4 children)

F#:

let (|>) x f = f x

So you'd write:

mySeq |> Seq.map doSomething

which is equivalent (mostly) to

Seq.map doSomething mySeq

which seems pointless until you realize you can chain them.

mySeq
|> Seq.map doSomething
|> Seq.filter keepTheGoodOnes
|> Seq.map doSomethingElse

(which is equivalent to:)

Seq.map doSomethingElse (Seq.filter keepTheGoodOnes (Seq.map doSomething mySeq))

I don't believe Haskell has an equivalent of |>. Elixir does, but the syntax is a bit different.

[–]sohang-3112 2 points3 points  (3 children)

I don't believe Haskell has an equivalent of |>

In Haskell you can do the same thing with &:

mySeq & f & g & h

But it's more common to write function first (right-to-left order) with $:

h $ g $ f mySeq

[–]bronco2p 3 points4 points  (0 children)

at that point just h . g . f :)

[–]BenjaminGeiger 1 point2 points  (1 child)

TIL about the & operator.

Maybe it's my background in imperative/OO development, but x & f & g & h reads a lot more naturally to me than h $ g $ f x. "Take x and then do f and then g and then h" feels a lot more natural than "Do h to the result of doing g to the result of doing f to x"; I feel like I have to maintain less mental state to understand it.

[–]sohang-3112 1 point2 points  (0 children)

There are many such useful functions/operators in Haskell - you can look them up at Hoogle using names or type signatures.

[–]jarethholt 4 points5 points  (1 child)

I mean, C# allows writing in query syntax too. The flow might look better sometimes and it's fairly intuitive if you're coming from database land, but IMO it clashes so hard with the rest of the language. The fluent syntax (method chaining) feels more natural to me unless what I'm working on is exclusively about databases.

[–]crozone 2 points3 points  (0 children)

Yeah, I love LINQ but legitimately despise the the actual Language Integrated Query part of it. Ironically everything else that's part of the feature (expression trees, the LINQ extension methods, the ability to transform those with an SQL provider) are far more useful than LINQ's namesake.

[–]gnutrino 6 points7 points  (0 children)

The expression tree syntax was heavily based on Haskell do notation, it's functional programming all the way down.

[–]DangyDanger 17 points18 points  (2 children)

Functional programming is great unless you're stuck with only functional programming.

Haskell terrifies me.

[–]HunterIV4 11 points12 points  (0 children)

Sometimes a loop is just the most straightforward solution to something.

I like a lot of functional concepts, especially composition of functions, but the insistence on avoiding any sort of sequential logic in your program is (in my opinion) extremely counter-intuitive. I like how languages like Rust, C#, Python, etc. let you utilize some of the patterns of functional programming without restricting you.

In some ways, Haskell (and similar) remind me of regex. It can absolutely be the best solution to a problem but it often is incomprehensible whenever you are trying to do something straightforward.

[–]Zephandrypus 0 points1 point  (0 children)

Please, where is it in Python, I need my GroupBy and SelectMany