Foldįold takes data in one format and gives it back to you in another. I will not talk about map and filter since it has been widely used in other functional languages. These are four functional operations that I often used as I got into the functional programming journey that you can use in your tool belt to write programs declaratively. This article will not talk about both paradigm’s pros and cons but to give you an idea of how you can program your program declaratively. SCALA FOR LOOP CODEWhile most of us usually started imperatively writing code, I want to show the other way of how you can write your code to be more declarative so that your code can be more descriptive. The declarative form of doing this will be to looping each element’s recursive way, and on each component, we want to add each other. For instance, to get the sum of a list, you will have for loop and add each item on the array to a variable. Conversely, the imperative form of writing programs usually requires loops. A declarative way of coding programs tends to be more recursive. In terms of writing programs, they have various ways of writing programs. Imperative programming focuses on how the program should operate. It doesn’t tell you what the intention, but how you want to achieve that intent. Usually, we see this a lot when we look at recipe books. Your focus is on the state of the application - you have a list of commands for the computer to perform. SCALA FOR LOOP HOW TOOn the other hand, thinking in terms of imperatively is when you think about how to accomplish the computation logic. You are essentially declaring to other people what your intention is instead of how you wish to achieve your purpose. Let me give an example - when you are thirsty, and you want to drink water, you don’t tell others than “I want to fill up water on a cup, and chuck it on my mouth” you tell them that I want to drink water. While these two are correct, the key to thinking in functional programming is to think in a declarative way.Ī declarative way of thinking about programs is where you express a computation logic without describing its control flow. The first one is usually pure function the second one is immutability. When people think about functional programming, two things come to mind. Stop Using Pattern Matching To Compose Your Program, Use These Functional Operations Instead
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |