Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 17, 2021 09:51 pm GMT

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 teaconst prepareTea = () => 'greenTea';/*Given a function (representing the tea type) and number of cups needed, thefollowing function returns an array of strings (each representing a cup ofa 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 teaconst prepareGreenTea = () => 'greenTea';// Function that returns a string representing a cup of black teaconst prepareBlackTea = () => 'blackTea';/*Given a function (representing the tea type) and number of cups needed, thefollowing function returns an array of strings (each representing a cup ofa 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 lineconst tea4Green = getTea(prepareGreenTea, 27); <-----const tea4Black = getTea(prepareBlackTea, 13); <-----// Only change code above this lineconsole.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.

Original Link: https://dev.to/rthefounding/functional-programming-notes-19j6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To