輸入→處理→輸出

Functional Programming is another popular approach to software development. In Functional Programming, code is organized into smaller, basic functions that can be combined to build complex programs.

In this course, 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().

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

Learn About Functional Programming

 

// 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;
};
// Only change code below this line
const tea4TeamFCC = getTea(40);
// Only change code above this line
console.log(tea4TeamFCC);
得到結果為字串含有40個’greenTea’

Understand Functional Programming Terminology

 

// 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 tea4GreenTeamFCC = getTea(prepareGreenTea, 27);
const tea4BlackTeamFCC = getTea(prepareBlackTea, 13);
// Only change code above this line
console.log(
  tea4GreenTeamFCC,
  tea4BlackTeamFCC
);

Understand the Hazards of Using Imperative Code

了解這兩者的差異 splice() vs slice()

// tabs is an array of titles of each site open within the window
const Window = function(tabs) {
  this.tabs = tabs; // We keep a record of the array inside the object
};
// When you join two windows into one window
Window.prototype.join = function(otherWindow) {
  this.tabs = this.tabs.concat(otherWindow.tabs);
  return this;
};
// When you open a new tab at the end
Window.prototype.tabOpen = function(tab) {
  this.tabs.push(‘new tab’); // Let’s open a new tab for now
  return this;
};
// When you close a tab
Window.prototype.tabClose = function(index) {
  // Only change code below this line
  const tabsBeforeIndex = this.tabs.slice(0, index); // Get the tabs before the tab
  const tabsAfterIndex = this.tabs.slice(index + 1); // Get the tabs after the tab
  this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // Join them together
  // Only change code above this line
  return this;
 };
// Let’s create three browser windows
const workWindow = new Window([‘GMail’, ‘Inbox’, ‘Work mail’, ‘Docs’, ‘freeCodeCamp’]); // Your mailbox, drive, and other work sites
const socialWindow = new Window([‘FB’, ‘Gitter’, ‘Reddit’, ‘Twitter’, ‘Medium’]); // Social sites
const videoWindow = new Window([‘Netflix’, ‘YouTube’, ‘Vimeo’, ‘Vine’]); // Entertainment sites
// Now perform the tab opening, closing, and other operations
const finalTabs = socialWindow
  .tabOpen() // Open a new tab for cat memes
  .join(videoWindow.tabClose(2)) // Close third tab in video window, and join
  .join(workWindow.tabClose(1).tabOpen());
console.log(finalTabs.tabs);

Avoid Mutations and Side Effects Using Functional Programming

 

Pass Arguments to Avoid External Dependence in a Function

 

Refactor Global Variables Out of Functions

 

Not PassedUse the map Method to Extract Data from an Array

 

Not PassedImplement map on a Prototype

 

Not PassedUse the filter Method to Extract Data from an Array

 

Not PassedImplement the filter Method on a Prototype

Not PassedReturn Part of an Array Using the slice Method

Not PassedRemove Elements from an Array Using slice Instead of splice

Not PassedCombine Two Arrays Using the concat Method

Not PassedAdd Elements to the End of an Array Using concat Instead of push

Not PassedUse the reduce Method to Analyze Data

Not PassedUse Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Not PassedSort an Array Alphabetically using the sort Method

Not PassedReturn a Sorted Array Without Changing the Original Array

Not PassedSplit a String into an Array Using the split Method

Not PassedCombine an Array into a String Using the join Method

Not PassedApply Functional Programming to Convert Strings to URL Slugs

Not PassedUse the every Method to Check that Every Element in an Array Meets a Criteria

Not PassedUse the some Method to Check that Any Elements in an Array Meet a Criteria

Not PassedIntroduction to Currying and Partial Application

最後修改日期: 2022 年 6 月 20 日

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。