software-development - done reading

map is actually a functional programming concept that's easy to understand when thinking about list. If you call map on a list, you get back a new list where each element of the new list is the result of applying a function to each element of the original list.

We were naturally led to using RxJava for wrangling UI interactions in a declarative fashion, and we came up with a simplified way of separating the pure logic from the side-effects in our code, very much in the spirit of the Functional Core, Imperative Shell. The result was a highly testable code base most of which could be fully understood, in isolation, with no concern for how it interacted with the rest of the system.

Where is the word functional come from?

The word functional is related to math function, particularly with Lambda calculus. From a more pragmatic perspective, a beginner needs to understand only two concepts in order to use it for everyday applications (no calculus required!).

What is functional programming?

Data processing without the data is just… well, processing. I'm afraid you're going to have to be patient. When you work with functional programming, all you get is functions forming a pipeline. One function feeds data to the next, which feeds it to the next, and so on until the results you need flow out the end.

What are the benefits provided by functional programming?

  1. If you’re a front-end developer working with data, especially if you’re formatting that data for visualization using D3, Raphael or the like, then functional programming will be an essential weapon in your arsenal. However, be warned that if you do a lot of data processing on the client side, the browser may give you the "slow running" script error dialog which can be annoying. Data processing should be done on the server-side to avoid this issue.
  2. Functional programming makes your code a bit more concise. With functional programming, we break the complex task into functions each accomplish a simple task, and then we combine those simple functions to perform more complex tasks.

What are some key points about functional programming?

  1. First, data in functional programs should be immutable, which sounds serious but just means that it should never change. At first, this might seem odd (after all, who needs a program that never changes anything?), but in practice, you would simply create new data structures instead of modifying ones that already exist. For example, if you need to manipulate some data in an array, then you’d make a new array with the updated values, rather than revise the original array. Easy!
  2. Secondly, functional programs should be stateless, which basically means they should perform every task as if for the first time, with no knowledge of what may or may not have happened earlier in the program’s execution (you might say that a stateless program is ignorant of the past). Combined with immutability, this helps us think of each function as if it were operating in a vacuum, blissfully ignorant of anything else in the application besides other functions. In more concrete terms, this means that your functions will operate only on data passed in as arguments and will never rely on outside values to perform their calculations.

What are the best practices?

  1. All of your functions must accept at least one argument.
  2. All of your functions must return data or another function.
  3. No loops!
  4. Get rid of this or objects if possible -
  5. Functional functions and pure Json object. No more data with methods. No more classes. No more if then. No more looping.
  6. Code is composable, testable, predictable and a joy to read.

How can we do programming without loop?

  • Use recursion
  • Need to think more on this. Perhaps there are other techniques as well.

Why should we not use loops?

Nested loops are difficult to read and understand.

What should we do if we find ourselves needing to use loops?

Rethink our design, and try to break it down further into multiple simpler functions, and then combine them. Consider using a functional programming libraries such as Underscore that already provide a bunch of methods such as each, map, reduce, etc for avoiding loops.

What are some functional programming languages?

What is the definition of higher-order functions?

Helper methods that perform actions on arrays in lieu of looping are often called “higher-order functions.”

Higher order functions are functions that accept other functions as an input, or functions that output a function as its output. A helpful example of this is Haskell's built-in map function. map takes in a function that was meant for a single value, and performs this function on an array of objects. Higher order functions allow you to abstract sections of code that multiple functions have in common, and then simply supply a function as a parameter to change the overall effect.

Why should we use Ramda instead of Underscore?

They are all great libraries. Ramda is designed in a way to be more friendly to functional design. The methods are curried by default and have the parameters switched to a more compatible way for partial application. See

Why should we use const instead of var?

Just for the sake of clarity towards other readers, I would expand on this paragraph a bit and clarify that *const* itself is not enough to make your data structures immutable. Const only preserves the binding between a variable name and its value, but you can still manipulate the value if it’s not a scalar type (eg. Reassign a property in an object value or add/remove elements in an array value).

Const is definitely preferable to var and let but if you are aiming for pure immutability that’s not something completely baked in js (yet). You can anyway resort to Object.freeze or to libraries like deep-freeze or immutable.js

What are the advantages and disadvantages of FP versus OOP?

With OOP, you can wrap up your entire library into an easy to use object with a set API of methods and properties. How do you handle this with Functional Programming? DO you even handle this with Functional Programming? Is the best bet just to have an OOP exterior with a Functional Programming inside?

This is a great question. Organizing your code is very important to keeping it maintainable and usable. I would recommend looking into ES6 modules. I break my code up into ES6 modules in a similar way OOP would use classes. With ES6 modules, you can even have functions that are private to those modules, which you cannot do with a JavaScript class. Of course you have to now use webpack or some sort of bundle if you are using ES6 modules. Though you should be using one already.

What about inheritance and polymorphism?

After trying Ramda in a project I realised that without native immutable data structures FP programming in JS is basically a hack, and the need for functional libraries makes debugging flow ugly since you have to jump into vendor code. Ramda, fp-lodash, immutablejs are libraries attempt to patch JS rather than being elegant fp solutions where is alternatives like Purescript, cCojurescript or Elm offer superior and straightforward approach to fp.

I always focus on datastructures when I write javascript. Sometimes I go so far as to use objects. I consider objects “A contract for interacting with a datastructure”. Only when my datastructure is mature and I want to make it “general purpose”, as opposed to “application specific”, do I replace datastructures with formal objects. Objects serve to hide details, and limit the ways you interact with the data of an abstraction. (I don’t really care for inheritance)

I use what I call a pseudo-functional style. This boils down to explicitly identifying the inputs and outputs of a procedure, instead of strictly enforcing no side-effects. The first parameter, or group of parameters is the “output parameters” These may be mututated by the javascript function/procedure, and frequently I recycle existing datastructures to save memory, because I find using memory well is frequently worth it, even if other optimizations are not.

The next group of parameters is the “input parameters”. After those come any callbacks, and finally come optional arguments. I frequently group my inputs into a single object/map when there’s many of them, to reduce the number of parameters. This also means that parameters can be indicated by name where the function is called, which reduces a lot of headaches(was this supposed to be the third or fourth parameter? I forget).

In my pseudo functional style, I mutate state freely within a function, such as by using for loops, but I don’t mess with external state. Once the js function returns, only the changes to “output parameters” persist. Sometimes I really do find for loops are easier to reason about than “map” etc. Using arrays a lot is pretty memory intensive, and isn’t always better conceptually.

Functionally manipulating arrays instead of doing iteration feels restrictive and limiting to me. Now, in lisp or haskell it might be a lot better, but js isn’t designed this way from the bottom up, there’s just a few of these functional tools tacked on.

Obviously the es6 lambda syntax makes this a lot cleaner and better, but I still don’t think pure functional programming is the best choice for js.

I definitely agree about using a pseudo-functional style. And the majority of my code can be described this way. I do have other developers on my projects that are not “functional” and I try to find a good balance between functional and readable by non fp devs.

I have been using ES6 modules for encapsulating private functions, or just straight up replacements where a class would normally be used. I have found ES6 modules can completely replace classes.

A function that has internal state mutation is not bad, especially in JavaScript since it wasn’t designed as a pure functional language.

State has to be mutated somewhere, even in FP. FP is about managing where this mutation happens.

I still try to lock down mutations of objects being passed in and out unless absolutely necessary. These mutations could block future function compositions. And function composition is one of my favorite parts about FP :)

My articles might use extreme language that may come off as trying to force a pure FP design, but it is all about balance and what is right for your project and team.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License