DEV Community

Discussion on: Explain functional programming paradigm Like I'm Five

Collapse
 
rhymes profile image
rhymes

Your hands can do things right? And your feet and your tummy do things, and so does your head. If we were talking about OOP, all of the different parts of your body and the behaviors they perform would all be apart of the object that is you. If someone needed you to do something with just your hands, they would need to create an entire you to do it.

With functional programming, your head and hands and feet and tummy are all separate functions that manipulate all of the different types. They don't have to be coupled to you anymore. If someone needed more than 2 of your hands, they would just need to invoke multiple calls to your hand functions. They wouldn't need to create two or more of you in order to use your hands. They do all the same stuff, but they can all be there own separate parts.

A less like I'm 5 explanation: functional programming languages can have strong typing systems, but those types can't expose behaviors, nor are they tied to any specific behavior or contract. There's no such thing as interfaces or class hierarchies.

Instead, when you create a type, you would write functions that can be used to manipulate that type. Those functions can be chained with other function calls to do other things.

Check out Elm if you're interested in learning functional programming. It compiles into JavaScript and can be used to create web applications. Compiler messages are super helpful, and the community is awesome.

Imagine three people sitting in a room, lets call them Anna, Brad and Carol.

In object oriented programming, Anna has a paper with cats written on it, and Brad has a paper with are cool written on it. Then Carol comes and asks Brad what do you think?. Brad asks Anna what do you have?, Anna looks at her paper and answers cats. Brad then looks at his paper and answers to Carol Cats are cool.

In functional programming, Carol comes and asks What do you think? then Anna produces a piece of paper, writes cats on it and passes it to Brad. Brad looks at the paper and writes are cool next to cats. Then they hand out the paper to Carol and she reads cats are cool.

img

I can't take credit for the image, but it seemed to helped me to remember how to use each 😉

A couple of resources which might help:

A practical introduction to functional programming