DEV Community

Randy Rivera
Randy Rivera

Posted on • Edited on

Functional Programming Notes:

  • In Functional Programming, code is organized into smaller, basic functions that can be combined to build complex programs.
  • In these upcoming posts, you'll learn the core concepts of Functional Programming including pure functions, how to avoid mutations, and how to write cleaner code with methods like .map() and .filter().

Learn About Functioning Programming

  • Functional programming is a style of programming where solutions are simple, isolated functions, without any side effects outside of the function scope: INPUT -> PROCESS -> OUTPUT
  • Functional programming is about:
  1. Isolated functions - there is no dependence on the state of the program, which includes global variables that are subject to change

  2. Pure functions - the same input always gives the same output

  3. Functions with limited side effects - any changes, or mutations, to the state of the program outside the function are carefully controlled

  • My friends and family love tea.
  • In the code editor, the prepareTea and getTea functions are already defined for you. Call the getTea function to get 40 cups of tea for them, and store them in the tea4Family variable.
// Function that returns a string representing a cup of green tea
const prepareTea = () => 'greenTea';

/*
Given a function (representing the tea type) and number of cups needed, the
following function returns an array of strings (each representing a cup of
a specific type of tea).
*/
const getTea = (numOfCups) => {
  const teaCups = [];

  for(let cups = 1; cups <= numOfCups; cups += 1) {
    const teaCup = prepareTea();
    teaCups.push(teaCup);
  }
  return teaCups;
};

const tea4Family = getTea(40); <----
Enter fullscreen mode Exit fullscreen mode

Understand Functional Programming Terminology

  • Now they also want both green and black tea.
  • With that information, we'll need to revisit the getTea function from last challenge to handle various tea requests. We can modify getTea to accept a function as a parameter to be able to change the type of tea it prepares. This makes getTea more flexible, and gives the programmer more control when client requests change.
// Function that returns a string representing a cup of green tea
const prepareGreenTea = () => 'greenTea';

// Function that returns a string representing a cup of black tea
const prepareBlackTea = () => 'blackTea';

/*
Given a function (representing the tea type) and number of cups needed, the
following function returns an array of strings (each representing a cup of
a specific type of tea).
*/
const getTea = (prepareTea, numOfCups) => {
  const teaCups = [];

  for(let cups = 1; cups <= numOfCups; cups += 1) {
    const teaCup = prepareTea();
    teaCups.push(teaCup);
  }
  return teaCups;
};

// Only change code below this line
const tea4Green = getTea(prepareGreenTea, 27); <-----
const tea4Black = getTea(prepareBlackTea, 13); <-----
// Only change code above this line

console.log(
  tea4Green,
  tea4Black
); // will display ['greenTea',
  'greenTea',
  'greenTea',
   ....  ['blackTea',
   ....
  'blackTea']
Enter fullscreen mode Exit fullscreen mode
  • Here we just prepared 27 cups of green tea and 13 cups of black tea and store them in tea4Green and tea4Black variables, respectively. Note that the getTea function has been modified so it now takes a function as the first argument.

let's cover some functional terminology:

  • Callbacks are the functions that are slipped or passed into another function to decide the change of that function. You may have seen them passed to other methods, for example in filter(which we will discuss later, the callback function tells JavaScript the criteria for how to filter an array.
  • Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called first class functions. In JavaScript, all functions are first class functions.
  • The functions that take a function as an argument, or return a function as a return value are called higher order functions.
  • When functions are passed in to or returned from another function, then those functions which were passed in or returned can be called a lambda.

Larson, Quincy, editor. “Learn About Functioning Programming.” Https://Www.freecodecamp.org/, Class Central, 2014, twitter.com/ossia.

Top comments (5)

Collapse
 
awolokita profile image
awolokita

You mentioned immutability / pure functions and map yet used a for loop to mutate an array with push. It would be great to see the example taken further to show how getTea can be reduced down to a single line.

Collapse
 
rthefounding profile image
Randy Rivera

I’m constantly learning everyday so yes

Collapse
 
eljayadobe profile image
Eljay-Adobe

The distinguishing features of functional programming for me are: immutability, recursion (making tail recursion optimization critically important), pattern matching, higher-order functions, code-as-data, separation of behavior from data, referential transparency, everything is an expression, and monads.

Elm is the functional programming language for a JavaScript world.

Collapse
 
pcockerell profile image
Peter Cockerell

"In Functional Programming, code is organized into smaller, basic functions that can be combined to build complex programs."

I don't think this is a defining characteristic of functional programming. You could say the same thing about Smalltalk-80 programs, which were OO and definitely not functional, or, going back even further, structured programming, where the term of art was "structured decomposition", but the paradigm was imperative or procedural.

Your paragraph really describes "good" programming, in whatever methodology is being used.

Collapse
 
ayabouchiha profile image
Aya Bouchiha

Amazing