DEV Community

Cover image for Introduction to functional programming with C#

Introduction to functional programming with C#

Naveen on April 22, 2017

It is no surprise that one of the biggest challenges in the enterprise software development is complexity. Change is inevitable. Especially when a ...
Collapse
 
fabriciofx profile image
Fabricio Cabral

Hi Naveen!

I'm sorry, but do you have any reference (papers, books, and so on) to support this affirmation: "Imperative programming styles like object oriented programming"?

Thanks for your attention!

Collapse
 
naveen profile image
Naveen

Hello Fabricio,

In my opinion, it means different things to different people. It is hard to make a definite statement to say that OOP does or doesn't belong to Imperative programming style.

Having said that, OOP is a way of managing state in an imperative program.

Collapse
 
bobbob profile image
bob

Hi Naveen.

"It is hard to make a definite statement to say that OOP does or doesn't belong to Imperative programming style."

And yet you made one for some reason. :)

OOP is no more imperative than Functional is imperative. If you write impure functions you're not really being functional you're being imperative. If you write mutable objects full of procedures ("sequences of statements") you're not really being OO you're being imperative or procedural.

I'm not sure what was in your blood stream, but it doesn't sound like it was OO. ;)

Collapse
 
vedrantrebovic profile image
vedrantrebovic

It seems like a blasphemy here if you mention some language, other than JS, can be used (or even worse: used for FP). Not sure why but many JS devs have this urge to show everyone JS is the best programming language out there (p.s. it isn't). Comments are mostly about the authors and code he wrote. I agree, if you publish some code online, even in blog post, it should be working, but it would be much better if we stick to the topic, rather than discussing the author. It's childish how people try to justify their language of choice by claiming their language is the best, and paradigm supported by that language is the only right one. If you catch yourself saying JS is better than C# (Java, C++...) or vice versa I have bad news for you: you're lousy software developer. If you catch yourself saying FP is better than OOP or vice versa I have bad news for you again: you don't understand neither of them. You're no different to any other extremist finding his holly grail.
First of all FP is around for so long, obsessing about it these days looks like obsessing about invention of wheel.
As a predominantly .Net developer I mostly rely on OOP. But that doesn't mean I don't use FP. Yes, C# supports FP programming style, probably not as good as some pure (pun intended) FP languages, but good enough to help you write better code. And it's been like that for years, with each new language spec supporting FP even more. If you want to criticize C# or any other programming language please write some code in it first. If you truly understand OOP and FP, you will find out you can use both of them. They both have pros and cons. If you manage to get pros column of both to your code, you have a winner.

Collapse
 
simonsabin profile image
Simon Sabin

Your code has a huge flaw in that you aren't outputting what is being calculated. You are outputting the index of the item being multiplied, not the actual value being multiplied.

public static List<string> Format(List<int> list)
    => list.AsParallel()
    .Select(Extensions.MultiplyBy2)
    .Zip(list.AsParallel(), (result, item) => $"{item} x 2 = {result}")
    .ToList();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dim0kq profile image
dim0kq

What is the point of writing FP in C#?

Collapse
 
bobbob profile image
bob

What is the point of writing OOP in C#?

Collapse
 
qjuanp profile image
Juan

What is the point of writing in C#?

--- oh! wait! I'm a c# developer!

Thread Thread
 
bobbob profile image
bob

You know what a rhetorical question is, right?

Collapse
 
bewithsahil002 profile image
Sahil Saif

">

Collapse
 
thecrimsonking92 profile image
Miles Grimes

Nice little article. Here to follow is a little nitpicking :)

You use the term "memorization". The correct term for what you reference is "memoization".