Skip to main content
added 5 characters in body
Source Link
Arseni Mourzenko
  • 139.4k
  • 32
  • 359
  • 544

The difference between LinqLINQ and foreachforeach really boils down to two different programming styles: imperative and declarative.

Imparative - In this style you tell the computer "do this...now do this...now do this now do this". You feed it a program one step at a time.

Declarative - In this style you tell the computer what you want the result to be and let it figure out how to get there.

  • Imperative: in this style you tell the computer "do this... now do this... now do this now do this". You feed it a program one step at a time.

  • Declarative: in this style you tell the computer what you want the result to be and let it figure out how to get there.

A classic example of these two styles is comparing assembly code (or C) with SQL. In assembly you give instructions (literally) one at a time. In SQL you express how to join data together and what result you want from that data.

A nice side-effect of declarative programming is that it tends to be a bit higher level. This allows for the platform to evolve underneath you without you having to change your code. For instance:

var foo = bar.Distinct();

var foo = bar.Distinct(); 

What is happening here? Is Distinct using one core? Two? Fifty? We dontdon't know and we don't care. The .NET devs could rewrite it at any time, as long as it continues to perform the same purpose our code could just magically get faster after a code update.

This is the power of functional programming. And the reason that you'll find that code in languages like Clojure, F# and C# (written with a functional programming mindset) is often 3x-10x smaller than it's imperative counterparts.

Finally I like the declarative style because in C# most of the time this allows me to write code that does not mutate the data. In the above example, Distinct()Distinct() does not change bar, it returns a new copy of the data. This means that whatever bar is, and where ever it came from, it not suddenly changed.

So like the other posters are saying...learn FP (functional, learn functional programming)...it'll It'll change your life. And if you can, do it in a true functional programming language. I prefer Clojure, but F# and HaskelHaskell are also excellent choices.

The difference between Linq and foreach really boils down to two different programming styles: imperative and declarative.

Imparative - In this style you tell the computer "do this...now do this...now do this now do this". You feed it a program one step at a time.

Declarative - In this style you tell the computer what you want the result to be and let it figure out how to get there.

A classic example of these two styles is comparing assembly code (or C) with SQL. In assembly you give instructions (literally) one at a time. In SQL you express how to join data together and what result you want from that data.

A nice side-effect of declarative programming is that it tends to be a bit higher level. This allows for the platform to evolve underneath you without you having to change your code. For instance:

var foo = bar.Distinct();

What is happening here? Is Distinct using one core? Two? Fifty? We dont know and we don't care. The .NET devs could rewrite it at any time, as long as it continues to perform the same purpose our code could just magically get faster after a code update.

This is the power of functional programming. And the reason that you'll find that code in languages like Clojure, F# and C# (written with a functional programming mindset) is often 3x-10x smaller than it's imperative counterparts.

Finally I like the declarative style because in C# most of the time this allows me to write code that does not mutate the data. In the above example, Distinct() does not change bar, it returns a new copy of the data. This means that whatever bar is, and where ever it came from, it not suddenly changed.

So like the other posters are saying...learn FP (functional programming)...it'll change your life. And if you can, do it in a true functional programming language. I prefer Clojure, but F# and Haskel are also excellent choices.

The difference between LINQ and foreach really boils down to two different programming styles: imperative and declarative.

  • Imperative: in this style you tell the computer "do this... now do this... now do this now do this". You feed it a program one step at a time.

  • Declarative: in this style you tell the computer what you want the result to be and let it figure out how to get there.

A classic example of these two styles is comparing assembly code (or C) with SQL. In assembly you give instructions (literally) one at a time. In SQL you express how to join data together and what result you want from that data.

A nice side-effect of declarative programming is that it tends to be a bit higher level. This allows for the platform to evolve underneath you without you having to change your code. For instance:

var foo = bar.Distinct(); 

What is happening here? Is Distinct using one core? Two? Fifty? We don't know and we don't care. The .NET devs could rewrite it at any time, as long as it continues to perform the same purpose our code could just magically get faster after a code update.

This is the power of functional programming. And the reason that you'll find that code in languages like Clojure, F# and C# (written with a functional programming mindset) is often 3x-10x smaller than it's imperative counterparts.

Finally I like the declarative style because in C# most of the time this allows me to write code that does not mutate the data. In the above example, Distinct() does not change bar, it returns a new copy of the data. This means that whatever bar is, and where ever it came from, it not suddenly changed.

So like the other posters are saying, learn functional programming. It'll change your life. And if you can, do it in a true functional programming language. I prefer Clojure, but F# and Haskell are also excellent choices.

Source Link

The difference between Linq and foreach really boils down to two different programming styles: imperative and declarative.

Imparative - In this style you tell the computer "do this...now do this...now do this now do this". You feed it a program one step at a time.

Declarative - In this style you tell the computer what you want the result to be and let it figure out how to get there.

A classic example of these two styles is comparing assembly code (or C) with SQL. In assembly you give instructions (literally) one at a time. In SQL you express how to join data together and what result you want from that data.

A nice side-effect of declarative programming is that it tends to be a bit higher level. This allows for the platform to evolve underneath you without you having to change your code. For instance:

var foo = bar.Distinct();

What is happening here? Is Distinct using one core? Two? Fifty? We dont know and we don't care. The .NET devs could rewrite it at any time, as long as it continues to perform the same purpose our code could just magically get faster after a code update.

This is the power of functional programming. And the reason that you'll find that code in languages like Clojure, F# and C# (written with a functional programming mindset) is often 3x-10x smaller than it's imperative counterparts.

Finally I like the declarative style because in C# most of the time this allows me to write code that does not mutate the data. In the above example, Distinct() does not change bar, it returns a new copy of the data. This means that whatever bar is, and where ever it came from, it not suddenly changed.

So like the other posters are saying...learn FP (functional programming)...it'll change your life. And if you can, do it in a true functional programming language. I prefer Clojure, but F# and Haskel are also excellent choices.