Functional programming for the OO developer (Intro)

in #programming7 years ago (edited)

I have been writing code since the days of the ZX81. Over the years I have worked in many languages, mostly of imperative style. Currently by day I mostly work in the C# OO space.

Since the release of C# 3.5, which introduced LINQ and Lambda, I have been exploring the wonderful, and at times mind bending world of functional programming. The benefits I have found exploring this space means some concepts have started to wash into my OO world, this has really simplified and cleaned up my code.

I thought I would start a series on my journey into functional code and the benefits I have seen using these techniques. Most of the examples will be C#, mostly because it is my go to language of choice and also as it's C style should feel familiar to anyone who has worked in one of the many C style languages.

So why should you care about Functional Programming?

I believe knowing about it could will make you a better programmer. You code will be easier to write, easier to detect bugs, easier to refactor and easier to reason about. You will also get a bag of new shiny tricks to make your programming life easier and more fun. (1)

The key topics I want to cover are:

  • Data, once created is immutable.
  • Types (These are NOT classes)
  • Functions are data (2)
  • Functions are pure (3)
  • Function composition
  • LINQ is NOT just about IEnumerable<T> (4)
  • Being lazy
  • Yes you can update an immutable database!!! (5)

I will flit between these topics as they are really so deeply intertwined, there is no other way :)

Let me know what you think of the series as it progresses, is moving fast enough etc. I will try publish one a week but I might sneak in more when possible. Hopefully it will spark your interest in this area that will become more important as CPU core count increases.

Hope you enjoy

Woz

Part 1 is now available

(1) Actual fun levels might vary from person to person.
(2) This will take a bit to build to it. Definitely one of those "Aaaargh I don't get th.... AH. It is so simple" moments
(3) They take arguments, operate on them and return a result. Nothing is changed, the same inputs ALWAYS return the same result and no "outside" state is looked at.
(4) This rabbit hole leads to the beautiful madness that is Monads and Functors, you will never look at LINQ the same way :)
(5) Lies all lies.... Sort of but not. New database copy each time but keeping everything everything that did not change.

Sort:  

Sound good, will read more when I'm at home. Now at the shop. 100% upvoted from @chanthasam

Yay, functional programming! You have my support :)

PS: Add the steemdev tag. Your post will show up in "new" on the tagged channels, so use them where you think people will like your stuff. I donno, maybe tutorial and education.

Cool, thanks for the info. I hope to get the next part up tomorrow, have half it done :)

A picture for the thread caption can't be bad either.
Btw. where would be the difference between C# plus functional programming and C++ plus functional programming?

I would say that until C++ 11 was released then many techniques would have required mountains of boiler plate noise. Now Lambda is in the language it opens a world of fun. Java 8 was the same as they got Lambda then.

To be fair, all the C style languages require work and enforcing conventions to make work well. C# has a leg up as it has LINQ. I use this for all sorts of computation, validation etc. Each realease C# brings more to the table, now has pattern matching (Ugly imho though) and native tuples.

Lambda is the key though as it opens partial application, curry and the like

Greetings @woz.software! smart move joining steem. Feel free to follow me and ask if you have questions.

Thanks, have done. Follow back would be appreciated to build my numbers :)

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.031
BTC 60327.71
ETH 2568.97
USDT 1.00
SBD 2.57