Giter VIP home page Giter VIP logo

functional-programming-js's Introduction

Notes on Functional Programming

Functional programming is a paradigm promoting the usage of functions in it's mathematical sense. It aims to provide a declarative way of coding, enhancing readability and reusability (DRY).

Functions in computing, also known as procedure, are reusable bits of code. They can have inputs, and outputs, but these are not mandatory.

In math, a function is a black box that convert one or many input to a single output. These functions are pure : for the same input, they provide the same output.

In Functional Programing, we talk about functions as mathematical functions. Followings these rules allows us to take advantage of many mathematical rules that makes code cleaner.

Parameters and Arguments

A function can accept one or many parameters. The amount of parameters a function can accept are called it's arity. When we call a function, we provide arguments. A function with an arity of :

  • 0 is called a Nullary
  • 1 is called an Unary
  • 2 is called a Binary
  • N is called a n-ary
function sum(a, b) {
  return a + b;
}

sum(1, 2) // Outputs 3
sum(1) // Outputs NaN because b is undefined

To distinguish between these two :

  • The function arity is always the same. Here, parameters are a and b
  • The arguments passed vary at runtime. At first call, we passe 1 and 2. At second call, we pass only 1.

Partial Application

Partial Application is the concept of providing some arguments to a function, but not all.

function sum(a, b) {
  return a + b;
}

let addOneTo = partial(sum, 1); 
addOneTo(3);

In this example, we partially apply sum by providing it's first argument : 1. Therefore, our function looks like this :

function addOneTo() {
  return function(b) {
    return sum(1, b);
  }
}

Using partial application, we can create functions out of existing functions, with some of the first parameters already provided. A more verbose example would be fetching data.

function fetch(url, data, callback) {
  // Imagine fetching logic
}

// In this function, we provide fetch with the url
const fetchFromWebsite = partial(fetch, "https://mywebsite.com");
fetchFromWebsite({ userId: 1}, function() {
  // Some logic with the user fetched
})

// In this function, we provide the url and the data
const fetchCurrentUser = partial(fetch, "https://mywebsite.com", { userId: 1 });
fetchCurrentUser(function() {
  // Some logic with the user fetched
})

// We can do it even more concisely
const fetchCurrentUser = partial(fetchFromWebsite, { userId: 1 });
fetchCurrentUser(function() {
  // Some logic with the user fetched
})

In this example we start to understand how it makes code clearer. Which is better ?

fetch("https://mywebsite.com", { userId: 1 }, function() {
  // Some logic with the user fetched
})
fetchCurrentUser(function() {
  // Some logic with the user fetched
})

The first example rely on the implementation of fetch in which we provide a lot of arguments. Usually, in a big project, the URL would be given way prior to the actual call to handle various environments (a production site and a development site).

The second example is verbose and straight-forward : required arguments are passed before, so we don't need to take care of them. Moreover, it says what it's doing : it fetches the user. This is the main benefit of functional programming : it focuses on the result, not on how to get it.

Currying

A special case of Partial Application is called Currying. It consist of partially applying one argument at a time. Our fetch example would look like it :

const myFetch = curry(fetch);
myFetch("https://mywebsite.com")({ userId: 1 })(function() {
  // Some logic with the user fetched
})

Why would one do this ? It appears that functional programming really is efficient when we talk about functions expecting a single input (more about this later).

Standard Functions

Identity : returns it's input as the output

Unary : Accepts only one argument.

Constant : always return the same constant value

Reducing Side Effects

Side Effect : operating on the outside world. A function that manipulate some external state of perform I/O is doing side effects. The problem with them is that it's hard to reason about without viewing the implementation of the code.

Idempotence : the characteristic of returning the same output given same input.

Purity : a function is pure when it has no side effects.

Referential Transparency : if we can replace a function call directly by it's output and see no difference in the program execution, we say the function has referental transparency.

functional-programming-js's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.