Skip to content

foyez/fp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Functional Programming


✔ 1.1 What is functional programming?

  • a programming paradigm
  • a code style
  • a mindset
  • a sexy, buzz-wordy trend

✔ 1.2 Why functional JavaScript?

  • object-oriented JS gets tricky(prototypes? this?!?)
  • safer, easier to debug/maintain
  • established community

✔ 1.3 Do everything with functions

input → output

Non functional / imperative programming:

var name = “Anjana”;
var greeting = “Hi, I’m ;
console.log(greeting + name);

> “Hi, I’m Anjana”

functional:

function greet(name) {
  return “Hi, I’m  + name;
}
greet(“Anjana”);

> “Hi, I’m Anjana”

✔ 1.4 Avoid side effects

  • use pure functions

Not pure:

var name = 'Foyez'
function greet() {
  console.log("Hi, I'm " + name)
}

Pure:

function greet(name) {
  return "Hi, I'm " + name
}

✔ 1.5 Use higher-order functions

  • functions can be inputs/outputs
function makeAdjectifier(adjective) {
  return function (string) {
    return adjective +   + string;
  };
}

var coolifier = makeAdjectifier(“cool”);
coolifier(“conference”);

> “cool conference”

✔ 1.6 Don't iterate

  • use map, reduce, filter

✔ 1.7 Avoid mutability

  • use immutable data

Mutation (bad!):

var rooms = [“H1”, “H2”, “H3”];
rooms[2] = “H4”;

// rooms => ["H1", "H2", "H4"]

No mutation (good!):

var rooms = [“H1”, “H2”, “H3”];
var newRooms = rooms.map(rm => rm === "H3" ? "H4" : rm);

// newRooms => ["H1", "H2", "H4"]
// rooms => ["H1", "H2", "H3"]

✔ 1.8 Persistent data structures for efficient immutability

  • libraries: Mori, Immutable.js

✔ 1.9 FP libraries for JS

✔ 1.10 Useful links


⬆ Back to top

Releases

No releases published

Packages

No packages published