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 mutatio…


This content originally appeared on DEV Community and was authored by Randy Rivera

  • 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); <----

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']
  • 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.


This content originally appeared on DEV Community and was authored by Randy Rivera


Print Share Comment Cite Upload Translate Updates
APA

Randy Rivera | Sciencx (2021-06-17T21:51:38+00:00) Functional Programming Notes:. Retrieved from https://www.scien.cx/2021/06/17/functional-programming-notes/

MLA
" » Functional Programming Notes:." Randy Rivera | Sciencx - Thursday June 17, 2021, https://www.scien.cx/2021/06/17/functional-programming-notes/
HARVARD
Randy Rivera | Sciencx Thursday June 17, 2021 » Functional Programming Notes:., viewed ,<https://www.scien.cx/2021/06/17/functional-programming-notes/>
VANCOUVER
Randy Rivera | Sciencx - » Functional Programming Notes:. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/17/functional-programming-notes/
CHICAGO
" » Functional Programming Notes:." Randy Rivera | Sciencx - Accessed . https://www.scien.cx/2021/06/17/functional-programming-notes/
IEEE
" » Functional Programming Notes:." Randy Rivera | Sciencx [Online]. Available: https://www.scien.cx/2021/06/17/functional-programming-notes/. [Accessed: ]
rf:citation
» Functional Programming Notes: | Randy Rivera | Sciencx | https://www.scien.cx/2021/06/17/functional-programming-notes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.