Functional

software-development

http://danieltao.com/lazy.js/
https://hackernoon.com/rethinking-javascript-break-is-the-goto-of-loops-51b27b1c85f8
https://hackernoon.com/rethinking-javascript-the-if-statement-b158a61cd6cb
https://hackernoon.com/rethinking-javascript-eliminate-the-switch-statement-for-better-code-5c81c044716d
https://hackernoon.com/functional-javascript-decoupling-methods-from-their-objects-aa3ca13d7ae8
https://hackernoon.com/the-beauty-in-partial-application-currying-and-function-composition-d885bdf0d574
https://hackernoon.com/functional-programming-in-javascript-is-an-antipattern-58526819f21e
https://hackernoon.com/transforming-callbacks-into-promises-and-back-again-e274c7cf7293
http://ramdajs.com/docs/#cond
https://hackernoon.com/open-vs-closed-user-interfaces-46daf6e9a1b4
https://medium.com/javascript-scene/javascript-factory-functions-with-es6-4d224591a8b1
https://medium.com/@joelthoms/this-is-a-great-example-you-have-brought-up-6b084ac86391
https://www.youtube.com/channel/UCO1cgjhGzsSYb1rsB4bFe4Q/videos
https://www.youtube.com/watch?v=e-5obm1G_FY
https://www.sitepoint.com/testing-functional-javascript/
https://www.sitepoint.com/introduction-functional-javascript/
http://www.smashingmagazine.com/2014/07/02/dont-be-scared-of-functional-programming/ - done reading
https://www.edx.org/course/introduction-functional-programming-delftx-fp101x-0
https://hackernoon.com/how-i-rediscovered-my-love-for-javascript-after-throwing-90-of-it-in-the-trash-f1baed075d1b
https://www.sitepoint.com/filtering-and-chaining-in-functional-javascript/
http://blog.plataformatec.com.br/2016/05/beyond-functional-programming-with-elixir-and-erlang/
http://www.sitepoint.com/an-introduction-to-reasonably-pure-functional-programming/
http://www.sitepoint.com/quick-tip-stop-writing-loops-start-thinking-with-maps/
http://en.wikipedia.org/wiki/Functional_programming
https://www.haskell.org/haskellwiki/Functional_programming
https://pragprog.com/magazines/2013-01/functional-programming-basics
http://eloquentjavascript.net/1st_edition/chapter6.html
http://c2.com/cgi/wiki?FunctionalProgramming
http://worrydream.com/refs/Hughes-WhyFunctionalProgrammingMatters.pdf
https://docs.python.org/2/howto/functional.html
http://www.sitepoint.com/currying-in-functional-javascript/
http://www.sitepoint.com/introduction-functional-javascript/
http://www.sitepoint.com/higher-order-functions-javascript/
http://www.sitepoint.com/recursion-functional-javascript/
https://newcome.wordpress.com/2012/03/06/functional-programming-and-the-death-of-the-unix-way/

https://www.destroyallsoftware.com/talks/boundaries

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. https://kickstarter.engineering/open-sourcing-our-android-and-ios-apps-6891be909fcd#.ftn1ado8b

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. http://fr.umio.us/why-ramda/

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 - http://bit.ly/2vJFbQK
  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. http://code.tutsplus.com/tutorials/why-haskell--net-28066

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 http://bit.ly/2vJFbQK

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