No matter, if I read an article over it or watch a youtube video to understand, I never get to truly understand to the point that I can think in a functional way to solve a problem.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (6)
(I'm assuming that as a 5yo, you know at least basic programming concepts)
The core and most important concept of functional programming is that of a pure function. A function is pure when its return value depends only on its input arguments, and the function does not cause side effects observable elsewhere in the system. For example, a
function add(a, b) { return a+b; }
is a pure function. Functional programming is fundamentally about building as much of your program as you can out of such pure functions.Pure functions have a number of benefits:
Trying to build a program out of pure functions drives you towards a different style than imperative/OOP approach: code written in functional style tends to be built out of transformations of data.
Think of e.g. washing the dishes. You usually wash them, then dry them, then stack them together. A functional code implementing that would look like:
or, in short:
This is the core concept of functional programming. Of course, you can't write your entire program using just pure functions - after all, reading data from user or printing it out are those unpure "side effects". But you can write most of your program this way.
There are more ideas that work well with this style, that you'll frequently find in functional languages.
thanks so much, <3
FP is just using types (data structures) and functions to solve problems. Like anything, it requires practice to do well.
If you understand procedural programming, you can start with that understanding. But where procedures encourage you to pass data into another procedure to be modified, functions expect input data (which will not be modified) and return output data. Much like electronic circuits have dedicated input and output pins/leads. FP also adds several capabilities over procedural.
Versus OO, the primary difference is that OO objects bind data and behavior together. Using the traditional OO organizational technique of inheritance, this can lead to hard-to-resolve conflicts of behavior or data structure. This in turn leads to all manner of OO guiding principles including SOLID. However, FP keeps data and behavior (functions) separate. So you can compose data types with other data types and functions with other functions without much fuss. Types act as a contract between functions. And functions simply transform input into output.
Up to now I have assumed pure transformation functions (only converting input to output, no side effects such as printing to screen), as these are the easiest to understand and test in isolation. You want as much of your program as possible to be made of these. But at some point, you have to perform side effects. This is best done "at the edges" of your program. Personally I like to declare side effects as data (whatever data is needed to perform it), so the inner core of my app can return the side effect as plain data. Then I have an effect function at the edge which takes the declared side effect and actually performs it.
Here is a relevant video.
I read somewhere that functional programming was easier for young kids to understand than imperative. Maybe you should ask for it to be explained like you're 20.
I'm not sure I think functionally enough to help achieve that state. I would say that the main gate is for your function calls to take arguments, don't modify them and chain those functions.
D has a keyword, pure, to help enforce these rules. But it defaults to what is referred to as weakly pure.
This is true in so far as this is how math works, and people learn that in school. Learning imperative programming involves understanding things like "function" of "what X = 2 means" differently than in math classes, whereas in functional programming, those concepts are behave similar to math.
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.
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:
Higher-Order Functions for 5 year olds
K 👓
A practical introduction to functional programming