- 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:
Isolated functions - there is no dependence on the state of the program, which includes global variables that are subject to change
Pure functions - the same input always gives the same output
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 modifygetTea
to accept a function as a parameter to be able to change the type of tea it prepares. This makesgetTea
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
andtea4Black
variables, respectively. Note that thegetTea
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)
You mentioned immutability / pure functions and
map
yet used a for loop to mutate an array withpush
. It would be great to see the example taken further to show howgetTea
can be reduced down to a single line.I’m constantly learning everyday so yes
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.
"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.
Amazing