FunctionalProgramming

๐Ÿ“— What is Functional Programming:

๐Ÿซ Every software program has two things:

๐Ÿ“Œ Behavior (what the program does)
๐Ÿ“Œ Data (data, is well, data)
๐Ÿ“Œ When weโ€™re learning about a programming paradigm โ€” like functional programming โ€” itโ€™s often helpful to consider how the paradigm approaches behavior and data respectively.

๐Ÿซ Behavior, in functional programming, is handled purely using functions in functional programming. Functions are โ€œself containedโ€ pieces of code that accomplish a specific task. They define a relationship between a set of possible inputs and a set of possible outputs โ€” they usually take in data, process it, and return a result. Once a function is written, it can be used over and over and over again.

๐Ÿซ Data, in functional programming, is immutable โ€” meaning it canโ€™t be changed. Rather than changing data they take in, functions in functional programming take in data as input and produce new values as output. Always.

๐Ÿซ Functions and immutable data are the only two things you need to ever deal with in functional programming. To make it even simpler, functions are treated no differently than data.

๐Ÿซ Put another way, functions in functional programming can be passed around as easily as data. You can refer to them from constants and variables, pass them as parameters to other functions, and return them as results from other functions.

๐Ÿซ This is the most important thing to understand when approaching functional programming.
By treating functions as nothing more special than a piece of data and by only using data that is immutable, we are given a lot more freedom in terms of how we can use functions.

๐Ÿซ Namely, it allows us to create small, independent functions that can be reused and combined together to build up increasingly complex logic. We can break any complex problem down into smaller sub-problems, solve them using functions, and finally combine them together to solve the bigger problem.

๐Ÿซ Considering the ever-growing complexity of software applications, this kind of โ€œbuilding-blockโ€ approach makes a huge difference in keeping programs simple, modular, and understandable. This is also why developers strive to make their functions as general-purpose as possible, so that they can be combined to solve large, complex problems and reused to speed up development time for subsequent programs.

๐Ÿซ Ultimately, the reason that functions are so powerful in functional programming is because the functions follow certain core tenets.

๐Ÿ Functions are pure
๐Ÿ Functions use immutable data
๐Ÿ Functions guarantee referential transparency
๐Ÿ Functions are first-class entities

๐Ÿซ After that, Iโ€™ll briefly touch on how functional programming applies these tenets to encourage us to think carefully about our data and the functions that interact with it.

๐Ÿซ By the end, youโ€™ll be able to understand how this approach leads to code that is:

๐Ÿ Easier to understand (that is, โ€œexpressiveโ€)
๐Ÿ Easier to reuse
๐Ÿ Easier to test
๐Ÿ Easier to maintain
๐Ÿ Easier to refactor
๐Ÿ Easier to optimize
๐Ÿ Easier to reason about