📜 ⬆️ ⬇️

Can I use functional programming in my own language?

Here is an amateur translation of the essay by Eric Normand, a consultant and speaker promoting functional programming.

I was pretty free with terms, did not hesitate to use anglicisms like "feature". I am confused by the literal translation of programmer terms. Enjoy reading

Summary: we answer the question directly, but then we analyze the prejudices hidden behind it.


Quite often, experienced developers ask me: "What can I get functional programming of something that I don’t have? Can I just start using AF in my own language?" This is a great question, I hope I can give you a decent answer.


I completely understand the emergence of this issue. JavaScript has map , filter and reduce . Java 8 has streams ( I decided to use Anglicism instead of the word "stream" - approx. Translator ). It seems that each language gets more and more functional features, incl. immutable data structures , first-order functions, composition of functions, destructuring and good typing.


And I agree: you can use FP in any language .


Functional programming is just a paradigm. Just as you can write in a procedural, object-oriented, or logical style in any language, you can write in a functional style in any language.


Just a minute. This is the answer to the question. But is everything so simple?


Ask yourself: is it possible to use OOP in C language? According to what I said above, the answer is yes. Moreover, I already did small object systems on C earlier. But then why do I need an object-oriented language if I can use objects in C? Feels like my object system provided me with all the features needed for OOP. But here's another question: could I use OOP in C without knowing OOP? I doubt it.


These paradigms are called paradigms, since these are holistic approaches to how to solve problems. OOP divides a task into objects that interact with each other using messages. The FP represents a task, as data, which displays the state of the simulated process. The procedural approach describes a solution as a set of clear steps. Each paradigm offers a different way to solve the problem. If you think that you can use the OP in Java, not being an expert in the OP, then you claim that functional programming is not a paradigm, but simply a set of features (immutable data, pure functions, their composition, etc.).


I have come across this many times. People claim to write in functional style in javascript. In fact, they simply use map and reduce together with a number of pure functions . And it gives good results, but in general, their code is procedural. They studied not the paradigm, but only its features.


The advantage of a language that rigidly imposes a paradigm is that it teaches you to use this approach to solving problems. And the more he imposes it, the more often you will have to try to solve the problem within this paradigm. The more functional your language is, the more you will be trained to use a functional approach when solving problems. The same applies to the PLO and procedural style. In short, you train at the expense of the fact that you can not use the approaches to which have become accustomed.


Let me express an even more controversial statement: outside languages ​​that are functional (Clojure, Haskell, Elm, Scala, Erlang, etc.), there are not that many AFs used. Except when developers are already familiar with the functional style, thanks to a wealth of experience, they use only crumbs of what the paradigm suggests. And they miss the most important thing: the opportunity to look at the task from a different angle.


I’m not saying that it’s impossible to really learn AF with JavaScript, but this will require self-discipline and help . Language will not instruct you on this path. You will have to do it yourself, or find someone who will help you.


Conclusion


All paradigms are excellent, because "one point of view is equivalent to 80 IQ points . " You can use any paradigm in any language. But it is very difficult to study the paradigm without plunging into it. And it is quite difficult to dive without using language that imposes it. Until you do this, you will use the paradigm features, but do not solve problems in a new way. In general, functional programming is not a set of features. If you want to learn it - immerse yourself in it.



Source: https://habr.com/ru/post/436396/