Movatterモバイル変換


[0]ホーム

URL:


Packt
Search iconClose icon
Search icon CANCEL
Subscription
0
Cart icon
Your Cart(0 item)
Close icon
You have no products in your basket yet
Save more on your purchases!discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Profile icon
Account
Close icon

Change country

Modal Close icon
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timerSALE ENDS IN
0Days
:
00Hours
:
00Minutes
:
00Seconds
Home> Web Development> Web Programming> Mastering JavaScript Functional Programming
Mastering JavaScript Functional Programming
Mastering JavaScript Functional Programming

Mastering JavaScript Functional Programming: Write clean, robust, and maintainable web and server code using functional JavaScript , Second Edition

Arrow left icon
Profile Icon Federico Kereki
Arrow right icon
AU$14.99AU$77.99
Full star iconFull star iconFull star iconFull star iconHalf star icon4.2(6 Ratings)
eBookJan 2020470 pages2nd Edition
eBook
AU$14.99 AU$77.99
Paperback
AU$96.99
Subscription
Free Trial
Renews at AU$24.99p/m
eBook
AU$14.99 AU$77.99
Paperback
AU$96.99
Subscription
Free Trial
Renews at AU$24.99p/m

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
Product feature iconAI Assistant (beta) to help accelerate your learning

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Table of content iconView table of contentsPreview book icon Preview Book

Mastering JavaScript Functional Programming

Becoming Functional - Several Questions

Functional programming (orFP) has been around since the earliest days of computing, and is going through a sort of revival because of its increased use with several frameworks and libraries, most particularly inJavaScript (JS). In this chapter, we shall do the following:

  • Introduce some concepts of FP to give a small taste of what it means.
  • Show the benefits (and problems) implied by the usage of FP and why we should use it.
  • Start thinking about why JavaScript can be considered an appropriate language for FP.
  • Go over the language features and tools that you should be aware of in order to fully take advantage of everything in this book.

By the end of this chapter, you'll have the basic tools that we'll be using in the rest of the book, so let's get started by learning about functional programming.

What is functional programming?

If you go back in computer history, you'll find that the second oldest programming language still in use, Lisp, is based on FP. Since then, there have been many more functional languages, and FP has been applied more widely. But even so, if you ask people what FP is, you'll probably get two widely dissimilar answers.

For trivia or history buffs, the oldest language still in use is Fortran, which appeared in 1957, a year before Lisp. Quite shortly after Lisp came another long-lived language, COBOL, for business-oriented programming.

Depending on whom you ask, you'll either learn that it's a modern, advanced, enlightened approach to programming that leaves every other paradigm behind or that it's mainly a theoretical thing, with more complications than benefits, practically impossible to implement in the real world. And, as usual, the real answer is not in the extremes, but somewhere in between. Let's start by looking at the theory versus practice and see how we plan to use FP. 

Theory versus practice

In this book, we won't be going about FP in a theoretical way. Instead, our point is to show you how some of its techniques and tenets can be successfully applied for common, everyday JavaScript programming. But—and this is important—we won't be going about this in a dogmatic fashion, but in a very practical way. We won't dismiss useful JavaScript constructs simply because they don't happen to fulfill the academic expectations of FP. Similarly, we won't avoid practical JavaScript features just to fit the FP paradigm. In fact, we could almost say that we'll be doingSorta Functional Programming (SFP) because our code will be a mixture of FP features, more classical imperative ones, andobject-oriented programming (OOP).

Be careful, though: what we just said doesn't mean that we'll be leaving all the theory by the side. We'll be picky, and just touch the main theoretical points, learn some vocabulary and definitions, and explain core FP concepts, but we'll always be keeping in sight the idea of producing actual, useful JavaScript code, rather than trying to meet some mystical, dogmatic FP criteria.

OOP has been a way to solve the inherent complexity of writing large programs and systems, and developing clean, extensible, scalable application architectures; however, because of the scale of today's web applications, the complexity of all codebases is continuously growing. Also, the newer features of JavaScript make it possible to develop applications that wouldn't even have been possible just a few years ago; think of mobile (hybrid) apps that are made with Ionic, Apache Cordova, or React Native or desktop apps that are made with Electron or NW.js, for example. JavaScript has also migrated to the backend with Node.js, so today, the scope of usage for the language has grown in a serious way that deals with all the added complexity of modern designs.

A different way of thinking

FP is a different way of writing programs, and can sometimes be difficult to learn. In most languages, programming is done in an imperative fashion: a program is a sequence of statements, executed in a prescribed fashion, and the desired result is achieved by creating objects and manipulating them, which usually means modifying the objects themselves. FP is based on producing the desired result by evaluating expressions built out of functions that are composed together. In FP, it's common to pass functions around (such as passing parameters to other functions or returning functions as the result of a calculation), to not use loops (opting for recursion instead), and to skip side effects (such as modifying objects or global variables).

In other words, FP focuses onwhat should be done, rather than onhow. Instead of worrying about loops or arrays, you work at a higher level, considering what you need to be done. After becoming accustomed to this style, you'll find that your code becomes simpler, shorter, and more elegant, and can be easily tested and debugged. However, don't fall into the trap of considering FP as the goal! Think of FP only as a means to an end, as with all software tools. Functional code isn't good just for being functional, and writing bad code is just as possible with FP as with any other technique!

What FP is not

Since we have been saying some things about what FP is, let's also clear up some common misconceptions, and look at what FP isnot:

  • FP isn't just an academic ivory tower thing: It is true that the lambda calculus upon which it is based was developed by Alonzo Church in 1936 as a tool to prove an important result in theoretical computer science (which preceded modern computer languages by more than 20 years!); however, FP languages are being used today for all kinds of systems.
  • FP isn't the opposite of object-oriented programming (OOP): It isn't a case of choosing declarative or imperative ways of programming. You can mix and match as best suits you, and we'll be doing this throughout this book, bringing together the best of all worlds. 
  • FP isn't overly complex to learn: Some of the FP languages are rather different from JavaScript, but the differences are mostly syntactic. Once you learn the basic concepts, you'll see that you can get the same results in JavaScript as with FP languages.

It may also be relevant to mention that several modern frameworks, such as the React and Redux combination, include FP ideas.

For example, in React, it's said that theview (whatever the user gets to see at a given moment) is a function of the currentstate. You use a function to compute what HTML and CSS must be produced at each moment, thinking in a black-box fashion.

Similarly, in Redux you have the concept ofactions that are processed byreducers. An action provides some data, and a reducer is a function that produces the new state for the application in a functional way out of the current state and the provided data. 

So, both because of the theoretical advantages (we'll be getting to those in the following section) and the practical ones (such as getting to use the latest frameworks and libraries), it makes sense to consider FP coding. Let's get on with it.

Why use FP?

Throughout the years, there have been many programming styles and fads. However, FP has proven quite resilient and is of great interest today. Why would you want to use FP? The question should rather first be, what do you want to get? and only then,does FP get you that? Let's answer these important questions in the following sections.

What we need

We can certainly agree that the following list of concerns is universal. Our code should have the following qualities:

  • Modular: The functionality of your program should be divided into independent modules, each of which contains what it needs to perform one aspect of the program's functionality. Changes in a module or function shouldn't affect the rest of the code.
  • Understandable: A reader of your program should be able to discern its components, their functions, and their relationships without undue effort. This is closely linked with the maintainability of the code; your code will have to be maintained at some time in the future, whether to be changed or to have new functionality added.
  • Testable:Unit tests try out small parts of your program, verifying their behavior independently of the rest of the code. Your programming style should favor writing code that simplifies the job of writing unit tests. Unit tests are also like documentation in that they can help readers understand what the code is supposed to do.
  • Extensible: It's a fact that your program will someday require maintenance, possibly to add new functionality. Those changes should impact the structure and data flow of the original code only minimally (if at all). Small changes shouldn't imply large, serious refactoring of your code.
  • Reusable:Code reuse has the goal of saving resources, time, and money, and reducing redundancy by taking advantage of previously written code. There are some characteristics that help this goal, such asmodularity (which we already mentioned), high cohesion (all the pieces in a module belong together),low coupling (modules are independent of each other),separation of concerns (the parts of a program should overlap in functionality as little as possible), andinformation hiding (internal changes in a module shouldn't affect the rest of the system).

What we get

So does FP give you the five characteristics we just listed in the previous section?

  • In FP, the goal is to write separate independent functions that are joined together to produce the final results.
  • Programs that are written in a functional style usually tend to be cleaner, shorter, and easier to understand.
  • Functions can be tested on their own, and FP code has advantages in achieving this.
  • You can reuse functions in other programs because they stand on their own, not depending on the rest of the system. Most functional programs share common functions, several of which we'll be considering in this book.
  • Functional code is free from side effects, which means you can understand the objective of a function by studying it without having to consider the rest of the program.

Finally, once you get used to the FP style of programming, code becomes more understandable and easier to extend. So it seems that all five characteristics can be achieved with FP!

Not all is gold

However, let's strive for a bit of balance. Using FP isn't a silver bullet that will automagically make your code better. Some FP solutions are actually tricky, and there are developers who greatly enjoy writing code and then asking, what does this do? If you aren't careful, your code may becomewrite-only and practically impossible to maintain; there goes understandable, extensible, and reusable out the door!

Another disadvantage is that you may find it harder to find FP-savvy developers. (Quick question: how manyfunctional programmers sought job ads have you ever seen?) The vast majority of today's web code is written in imperative, non-functional ways, and most coders are used to that way of working. For some, having to switch gears and start writing programs in a different way may prove an unpassable barrier. 

Finally, if you try to go fully functional, you may find yourself at odds with JavaScript, and simple tasks may become hard to do. As we said at the beginning, we'll opt forsorta FP, so we won't be drastically rejecting any language features that aren't 100% functional. After all, we want to use FP to simplify our coding, not to make it more complex!

So, while I'll strive to show you the advantages of going functional in your code, as with any change, there will always be some difficulties. However, I'm fully convinced that you'll be able to surmount them and that your organization will develop better code by applying FP. Dare to change! So, given that you accept that FP may apply to your own problems, let's now consider the other question, can we use JavaScript in a functional way and is it appropriate?

Is JavaScript functional?

At about this time, there is another important question that you should be asking: Is JavaScript a functional language? Usually, when thinking about FP, the list of languages that are mentioned does not include JavaScript, but does include less common options, such as Clojure, Erlang, Haskell, and Scala; however, there is no precise definition for FP languages or a precise set of features that such languages should include. The main point is that you can consider a language to be functional if it supports the common programming style associated with FP. Let's start by learning about why we would want to use JavaScript at all and how the language has evolved to its current version, and then see some of the key features that we'll be using to work in a functional way.

JavaScript as a tool

What is JavaScript? If you considerpopularity indices, such as the ones atwww.tiobe.com/tiobe-index/ orhttp://pypl.github.io/PYPL.html, you'll find that JavaScript is consistently in the top ten most popular languages. From a more academic point of view, the language is sort of a mixture, borrowing features fromseveral different languages. Several libraries helped the growth of the language by providing features that weren't so easily available, such as classes and inheritance (today's version of the language does support classes, but that was not the case not too long ago), that otherwise had to be achieved by doing someprototypetricks.

The nameJavaScript was chosen to take advantage of the popularity of Java—just as a marketing ploy! Its first name wasMocha, then,LiveScript, and only then,JavaScript.

JavaScript has grown to be incredibly powerful. But, as with all power tools, it gives you a way to not only produce great solutions, but also to do great harm. FP could be considered as a way to reduce or leave aside some of the worst parts of the language and focus on working in a safer, better way; however, due to the immense amount of existing JavaScript code, you cannot expect it to facilitate large reworkings of the language that would cause most sites to fail. You must learn to live with the good and the bad, and simply avoid the latter parts.

In addition, the language has a broad variety of available libraries that complete or extend the language in many ways. In this book, we'll be focusing on using JavaScript on its own, but we will make references to existing, available code.

If we ask whether JavaScript is actually functional, the answer will be, once again,sorta. It can be seen as functional because of several features, such as first-class functions, anonymous functions, recursion, and closures—we'll get back to this later. On the other hand, it also has plenty ofnon-FP aspects, such as side effects (impurity), mutable objects, and practical limits to recursion. So, when programming in a functional way, we'll be taking advantage of all the relevant, appropriate language features, and we'll try to minimize the problems caused by the more conventional parts of the language. In this sense, JavaScript will or won't be functional, depending onyour programming style!

If you want to use FP, you should decide which language to use; however, opting for fully functional languages may not be so wise. Today, developing code isn't as simple as just using a language; you will surely require frameworks, libraries, and other sundry tools. If we can take advantage of all the provided tools but at the same time introduce FP ways of working in our code, we'll be getting the best of both worlds, never mind whether JavaScript is functional!

Going functional with JavaScript

JavaScript has evolved through the years, and the version we'll be using is (informally) called JS10, and (formally) ECMAScript 2019, usually shortened to ES2019 or ES10; this version was finalized in June 2019. The previous versions were as follows:

  • ECMAScript 1, June 1997
  • ECMAScript 2, June 1998, which was basically the same as the previous version
  • ECMAScript 3, December 1999, with several new functionalities
  • ECMAScript 5, December 2009 (and no, there never was an ECMAScript 4, because it was abandoned)
  • ECMAScript 5.1, June 2011
  • ECMAScript 6 (or ES6; later renamed ES2015), June 2015
  • ECMAScript 7 (also ES7, or ES2016), June 2016
  • ECMAScript 8 (ES8 or ES2017), June 2017
  • ECMAScript 9 (ES9 or ES2018), June 2018

You can read the standard language specification atwww.ecma-international.org/ecma-262/7.0/. Whenever we refer to JavaScript in the text without further specification, ES10 (ES2019) is what is being referred to; however, in terms of the language features that are used in the book, if you were just to use ES2015, then you'd mostly have no problems with this book

No browsers fully implement ES10; most provide an older version, JavaScript 5 (from 2009), with an (always growing) smattering of features from ES6 up to ES10. This will prove to be a problem, but fortunately, a solvable one; we'll get to this shortly. We'll be using ES10 throughout the book.

In fact, there are only a few differences between ES2016 and ES2015, such as the Array.prototype.includes method and the exponentiation operator, **. There are more differences between ES2017 and ES2016—such as async andawait, some string padding functions, and more—but they won't impact our code. We will also be looking at alternatives for even more modern additions, such asflatMap(), in later chapters.

As we are going to work with JavaScript, let's start by considering its most important features that pertain to our FP goals.

Key features of JavaScript

JavaScript isn't a purely functional language, but it has all the features that we need for it to work as if it were. The main features of the language that we will be using are as follows: 

  • Functions as first-class objects
  • Recursion
  • Arrow functions
  • Closures
  • Spread

Let's see some examples of each one and find out why they will be useful to us. Keep in mind, though, that there are more features of JavaScript that we will be using; the upcoming sections just highlight the most important features in terms of what we will be using for FP.

Functions as first-class objects

Saying that functions arefirst-class objects (also called first-class citizens) means that you can do everything with functions that you can do with other objects. For example, you can store a function in a variable, you can pass it to a function, you can print it out, and so on. This is really the key to doing FP; we will often be passing functions as parameters (to other functions) or returning a function as the result of a function call. 

If you have been doing async Ajax calls, then you have already been using this feature: acallback is a function that will be called after the Ajax call finishes and is passed as a parameter. Using jQuery, you could write something like the following:

$.get("some/url", someData, function(result, status) {
//check status, and do something
//with the result
});

The$.get() function receives a callback function as a parameter and calls it after the result is obtained. 

Since functions can be stored in variables, you could also write something like the following. Pay attention to how we use thedoSomething variable in the$.get(...) call:

var doSomething = function(result, status) {
//check status, and do something
//with the result
};

$.get("some/url", someData,doSomething);

We'll be seeing more examples of this inChapter 6,Producing Functions – Higher-Order Functions.

Recursion

Recursion is the most potent tool for developing algorithms and a great aid for solving large classes of problems. The idea is that a function can at a certain point call itself, and whenthat call is done, continue working with whatever result it has received. This is usually quite helpful for certain classes of problems or definitions. The most often quoted example is the factorial function (the factorial ofn is written as n!) as defined for nonnegative integer values:

  • Ifn is 0, then n!=1
  • Ifn is greater than 0, thenn! = n * (n-1)!
The value ofn! is the number of ways that you can ordern different elements in a row. For example, if you want to place five books in line, you can pick any of the five for the first place, and then order the other four in every possible way, so5! = 5*4!. If you continue to work this example, you'll get5! = 5*4*3*2*1=120, son! is the product of all numbers up ton.

This can be immediately turned into code:

functionfact(n) {
if (n === 0) {
return 1;

} else {
return n *fact(n - 1);
}
}

console.log(fact(5)); //120

Recursion will be a great aid for the design of algorithms. By using recursion, you could do without anywhile orfor loops—not that wewant to do that, but it's interesting that wecan! We'll be devoting the entirety of Chapter 9Designing Functions – Recursion, to designing algorithms and writing functions recursively.

Closures

Closures are a way to implement data hiding (with private variables), which leads to modules and other nice features. The key concept of closures is that when you define a function, it can refer to not only its own local variables but also to everything outside of the context of the function. We can write a counting function that will keep its own count by means of a closure:

function newCounter() {
let count = 0;
return function() {
count++;
return count;
};
}

const nc = newCounter();
console.log(nc()); //1
console.log(nc()); //2
console.log(nc()); //3

Even afternewCounter() exits, the inner function still has access tocount, but that variable is not accessible to any other parts of your code.

This isn't a very good example of FP— a function (nc(), in this case) isn't expected to return different results when called with the same parameters!

We'll find several uses for closures, such as memoization (seeChapter 4,Behaving Properly – Pure Functions, andChapter 6,Producing Functions – Higher-Order Functions) and themodule pattern (seeChapter 3,Starting out with Functions – A Core Concept, andChapter 11,Implementing Design Patterns – The Functional Way), among others.

Arrow functions

Arrow functions are just a shorter, more succinct way of creating an (unnamed) function. Arrow functions can be used almost everywhere a classical function can be used, except that they cannot be used as constructors. The syntax is either(parameter, anotherparameter, ...etc) => { statements } or (parameter, anotherparameter, ...etc) => expression. The first allows you to write as much code as you want, and the second is short for{ returnexpression }. We could rewrite our earlier Ajax example as follows:

$.get("some/url", data,(result, status) =>{
//check status, and do something
//with the result
});

A new version of the factorial code could be like the following code:

constfact2 = n => {
if (n === 0) {
return 1;

} else {
return n * fact2(n - 1);
}
};
console.log(fact2(5)); //also 120

You would probably write the latter as a one-liner—can you see the equivalence to our earlier code? Using a ternary operator in lieu of anif is quite common:

const fact3 = n => (n === 0 ? 1 : n * fact3(n - 1));

console.log(fact3(5)); // again 120

With this shorter form, you don't have to writereturn—it's implied.

In lambda calculus, a function such as x => 2*x would be represented as λx.2*x. Although there are syntactical differences, the definitions are analogous. Functions with more parameters are a bit more complicated; (x,y)=>x+y would be expressed as λx.λy.x+y. We'll learn more about this in the section called Lambdas and functions, in Chapter 3Starting out with Functions - A Core Concept, and in the section called Currying, in Chapter 7Transforming Functions - Currying and Partial Application.

There's one other small thing to bear in mind: when the arrow function has a single parameter, you can omit the parentheses around it. I usually prefer leaving them, but I've applied a JS beautifier,Prettier, to the code, which removes them. It's really up to you whether to include them or not! (For more on this tool, check out https://github.com/prettier/prettier.) By the way, my options for formatting were --print-width 75 --tab-width 2 --no-bracket-spacing.

Spread

The spread operator (seehttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator) lets you expand an expression in places where you would otherwise require multiple arguments, elements, or variables. For example, you can replace arguments in a function call, as shown in the following code:

const x = [1, 2, 3];

function sum3(a, b, c) {
return a + b + c;
}

const y = sum3(...x); // equivalent to sum3(1,2,3)
console.log(y); // 6

You can also create or join arrays, as shown in the following code:

const f = [1, 2, 3];

const g = [4,...f, 5]; // [4,1,2,3,5]

const h = [...f, ...g]; // [1,2,3,4,1,2,3,5]

It works with objects too:

const p = { some: 3, data: 5 };

const q = { more: 8,...p }; // { more:8, some:3, data:5 }

You can also use it to work with functions that expect separate parameters instead of an array. Common examples of this would beMath.min() andMath.max():

const numbers = [2, 2, 9, 6, 0, 1, 2, 4, 5, 6];
const minA =Math.min(...numbers); //0

const maxArray =arr => Math.max(...arr);
const maxA = maxArray(numbers); //9

You can also write the following equality since the.apply() method requires an array of arguments, but.call() expects individual arguments:

someFn.apply(thisArg,someArray) === someFn.call(thisArg,...someArray);
If you have problems remembering what arguments are required by.apply() and.call(), this mnemonic may help:A is for an array, and C is for a comma. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call for more information.

Using the spread operator helps write a shorter, more concise code, and we will be taking advantage of it. We have seen all of the most important JavaScript features that we will be using. Let's round off the chapter by looking at some tools that we'll be working with.

How do we work with JavaScript?

This is all well and good, but as we mentioned before, it so happens that the JavaScript version available almost everywhere isn't ES10, but rather the earlier JS5. An exception to this is Node.js. It is based on Chrome's v8 high-performance JavaScript engine, which already has several ES10 features available. Nonetheless, as of today, ES10 coverage isn't 100% complete, and there are features that you will miss. (Check out https://nodejs.org/en/docs/es6/ for more on Node.js and v8.) This will surely change in the future, as Internet Explorer will fade away, and the newest Microsoft's browser will share Chrome's engine, but for the time being, we must still deal with older, less powerful engines.

So what can you do if you want to code using the latest version, but the available one is an earlier, poorer one? Or what happens if most of your users are using older browsers, which don't support the fancy features you're keen on using? Let's see some solutions for this.

If you want to be sure of your choices before using any given new feature, check out the compatibility table at https://kangax.github.io/compat-table/es6/ (seeFigure 1.1). For Node.js specifically, check out http://node.green/.
Figure 1.1. - The latest JavaScript features may not be widely and fully supported, so you'll have to check before using them.

Using transpilers

In order to get out of this availability and compatibility problem, there are a couple oftranspilers that you can use. Transpilers take your original ES10 code, which might use the most modern JavaScript features, and transforms it into equivalent JS5 code. It's a source-to-source transformation, instead of a source-to-object code that would be used in compilation. You can code using advanced ES10 features, but the user's browsers will receive JS5 code. A transpiler will also let you keep up with upcoming versions of the language, despite the time needed by browsers to adopt new standards across desktop and mobile devices.

If you wonder where the word transpiler came from, it is a portmanteau oftranslate andcompiler. There are many such combinations in technological speak:email (electronic andmail),emoticon (emotion andicon),malware (malicious andsoftware), oralphanumeric (alphabetic andnumeric), and many more.

The most common transpilers for JavaScript are Babel (at https://babeljs.io/) and Traceur (at https://github.com/google/traceur-compiler). With tools such as npm or webpack, it's fairly easy to configure things so that your code will get automatically transpiled and provided to end-users. You can also carry out transpilation online; seeFigure 1.2 for an example of this using Babel's online environment:

Figure 1.2 - The Babel transpiler converts ES10 code into compatible JS5 code

If you prefer Traceur, you can use its tool at https://google.github.io/traceur-compiler/demo/repl.html# instead, but you'll have to open a developer console to see the results of your running code (seeFigure 1.3 for an example of transpiled code). Select theEXPERIMENTAL option to fully enable ES10 support:

Figure 1.3 - The Traceur transpiler is an equally valid alternative for ES10-to-JS5 translation
Using transpilers is also a great way to learn new language features. Just type in some code on the left and see the equivalent code on the right. Alternatively, you can use the command-line interface (CLI) tools to transpile a source file and then inspect the produced output.

There's a final possibility that you may want to consider: instead of JavaScript, opt for Microsoft's TypeScript (athttp://www.typescriptlang.org/), a superset of the language that is itself compiled to JS5. The main advantage of TypeScript is the ability to add (optional) static type checks to JavaScript, which helps detect certain programming errors at compile time. But beware: as with Babel or Traceur, not all of ES10 will be available.

You can also perform type checks without using TypeScript by using Facebook's Flow (see https://flow.org/).

If you opt to go with TypeScript, you can also test it online at theirplayground (see http://www.typescriptlang.org/play/). You can set options to be more or less strict with data type checks, and you can also run your code on the spot (seeFigure 1.4 for more details):

Figure 1.4 - TypeScript adds type-checking features for safer programming

By using TypeScript, you will be able to avoid common type-related mistakes. A positive trend is that most tools (frameworks, libraries, and so on) are slowly going in this direction, so work will be easier.

Working online

There are some more online tools that you can use to test out your JavaScript code. Check out JSFiddle (at https://jsfiddle.net/), CodePen (at https://codepen.io/), and JSBin (athttp://jsbin.com/), among others. You may have to specify whether to use Babel or Traceur; otherwise, newer language features will be rejected. You can see an example of JSFiddle inFigure 1.5:

Figure 1.5 - JSFiddle lets you try out modern JavaScript code (plus HTML and CSS) without requiring any other tools

Using these tools provides a very quick way to try out code or do small experiments—and I can truly vouch for this since I've tested much of the code in the book in this way!

Testing

We will also touch on testing, which is, after all, one of FP's main advantages. For this, we will be using Jasmine (https://jasmine.github.io/), though we could also opt for Mocha (http://mochajs.org/).

You can run Jasmine test suites with a runner, such as Karma (https://karma-runner.github.io), but I opted for standalone tests; see https://github.com/jasmine/jasmine#installation for details.

Summary

In this chapter, we have seen the basics of FP, a bit of its history, its advantages (and also some possible disadvantages, to be fair), why we can apply it in JavaScript (which isn't usually considered a functional language), and what tools we'll need in order to go through the rest of this book.

In Chapter 2Thinking Functionally - A First Example, we'll go over an example of a simple problem, look at it in common ways, and end by solving it in a functional manner and analyzing the advantages of our method.

Questions

1.1. Classes as first-class objects: We learned that functions are first-class objects, but did you know that classes also are? (Though, of course, speaking of classes asobjects does sound weird.) Look at the following example and see what makes it tick! Be careful: there's some purposefully weird code in it:

const makeSaluteClass = term =>
class {
constructor(x) {
this.x = x;
}

salute(y) {
console.log(`${this.x} says "${term}" to ${y}`);
}
};

const Spanish = makeSaluteClass("HOLA");
new Spanish("ALFA").salute("BETA");
//ALFA says "HOLA" to BETA

new (makeSaluteClass("HELLO"))("GAMMA").salute("DELTA");
//GAMMA says "HELLO" to DELTA

const fullSalute = (c, x, y) => new c(x).salute(y);
const French = makeSaluteClass("BON JOUR");
fullSalute(French, "EPSILON", "ZETA");
//EPSILON says "BON JOUR" to ZETA

1.2.Factorial errors: Factorials, as we defined them, should only be calculated for non-negative integers; however, the function that we wrote in theRecursion section doesn't verify whether its argument is valid. Can you add the necessary checks? Try to avoid repeated, redundant tests!

1.3.Climbing factorial: Our implementation of a factorial starts by multiplying byn, then byn-1, thenn-2, and so on in what we could call a downward fashion. Can you write a new version of the factorial function that will loopupwards?

1.4.Code squeezing: Not that it's a goal in itself, but by using arrow functions and some other JavaScript features, you can shortennewCounter() to half its length. Can you see how?

Download code iconDownload Code

Key benefits

  • Explore this second edition updated to cover features like async functions and transducers, as well as functional reactive programming
  • Enhance your functional programming (FP) skills to build web and server apps using JavaScript
  • Use FP to enhance the modularity, reusability, and performance of apps

Description

Functional programming is a paradigm for developing software with better performance. It helps you write concise and testable code. To help you take your programming skills to the next level, this comprehensive book will assist you in harnessing the capabilities of functional programming with JavaScript and writing highly maintainable and testable web and server apps using functional JavaScript.This second edition is updated and improved to cover features such as transducers, lenses, prisms and various other concepts to help you write efficient programs. By focusing on functional programming, you’ll not only start to write but also to test pure functions, and reduce side effects. The book also specifically allows you to discover techniques for simplifying code and applying recursion for loopless coding. Gradually, you’ll understand how to achieve immutability, implement design patterns, and work with data types for your application, before going on to learn functional reactive programming to handle complex events in your app. Finally, the book will take you through the design patterns that are relevant to functional programming.By the end of this book, you’ll have developed your JavaScript skills and have gained knowledge of the essential functional programming techniques to program effectively.

Who is this book for?

This book is for JavaScript developers who want to enhance their programming skills and build efficient web applications. Frontend and backend developers who use various JavaScript frameworks and libraries like React, Angular, or Node.js will also find the book helpful. Working knowledge of ES2019 is required to grasp the concepts covered in the book easily.

What you will learn

  • Simplify JavaScript coding using function composition, pipelining, chaining, and transducing
  • Use declarative coding as opposed to imperative coding to write clean JavaScript code
  • Create more reliable code with closures and immutable data
  • Apply practical solutions to complex programming problems using recursion
  • Improve your functional code using data types, type checking, and immutability
  • Understand advanced functional programming concepts such as lenses and prisms for data access

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date :Jan 24, 2020
Length:470 pages
Edition :2nd
Language :English
ISBN-13 :9781839217425
Languages :

What do you get with eBook?

Product feature iconInstant access to your Digital eBook purchase
Product feature icon Download this book inEPUB andPDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature iconDRM FREE - Read whenever, wherever and however you want
Product feature iconAI Assistant (beta) to help accelerate your learning

Contact Details

Modal Close icon
Payment Processing...
tickCompleted

Billing Address

Product Details

Publication date :Jan 24, 2020
Length:470 pages
Edition :2nd
Language :English
ISBN-13 :9781839217425
Category :
Languages :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
AU$24.99billed monthly
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconSimple pricing, no contract
AU$249.99billed annually
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconChoose a DRM-free eBook or Video every month to keep
Feature tick iconPLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick iconExclusive print discounts
AU$349.99billed in 18 months
Feature tick iconUnlimited access to Packt's library of 7,000+ practical books and videos
Feature tick iconConstantly refreshed with 50+ new titles a month
Feature tick iconExclusive Early access to books as they're written
Feature tick iconSolve problems while you work with advanced search and reference features
Feature tick iconOffline reading on the mobile app
Feature tick iconChoose a DRM-free eBook or Video every month to keep
Feature tick iconPLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick iconExclusive print discounts

Frequently bought together


40 Algorithms Every Programmer Should Know
40 Algorithms Every Programmer Should Know
Read more
Jun 2020382 pages
Full star icon3.8 (33)
eBook
eBook
AU$14.99AU$55.99
AU$68.99
Mastering JavaScript Functional Programming
Mastering JavaScript Functional Programming
Read more
Jan 2020470 pages
Full star icon4.2 (6)
eBook
eBook
AU$14.99AU$77.99
AU$96.99
Clean Code in JavaScript
Clean Code in JavaScript
Read more
Jan 2020548 pages
Full star icon4.1 (7)
eBook
eBook
AU$14.99AU$42.99
AU$53.99
AU$68.99
Stars icon
TotalAU$219.97
40 Algorithms Every Programmer Should Know
AU$68.99
Mastering JavaScript Functional Programming
AU$96.99
Clean Code in JavaScript
AU$53.99
TotalAU$219.97Stars icon
Buy 2+ to unlockAU$11.99 prices - master what's next.
SHOP NOW

Table of Contents

16 Chapters
Technical RequirementsChevron down iconChevron up icon
Technical Requirements
Becoming Functional - Several QuestionsChevron down iconChevron up icon
Becoming Functional - Several Questions
What is functional programming?
Why use FP?
Is JavaScript functional?
How do we work with JavaScript?
Summary
Questions
Thinking Functionally - A First ExampleChevron down iconChevron up icon
Thinking Functionally - A First Example
Our problem – doing something only once
A functional solution to our problem
Summary
Questions
Starting Out with Functions - A Core ConceptChevron down iconChevron up icon
Starting Out with Functions - A Core Concept
All about functions
Using functions in FP ways
Summary
Questions 
Behaving Properly - Pure FunctionsChevron down iconChevron up icon
Behaving Properly - Pure Functions
Pure functions
Impure functions
Testing – pure versus impure
Summary
Questions
Programming Declaratively - A Better StyleChevron down iconChevron up icon
Programming Declaratively - A Better Style
Transformations
Logical higher-order functions
Working with async functions
Summary
Questions
Producing Functions - Higher-Order FunctionsChevron down iconChevron up icon
Producing Functions - Higher-Order Functions
Wrapping functions – keeping behavior
Altering a function's behavior
Changing functions in other ways
Summary
Questions
Transforming Functions - Currying and Partial ApplicationChevron down iconChevron up icon
Transforming Functions - Currying and Partial Application
A bit of theory
Currying
Partial application
Partial currying
Final thoughts
Summary
Questions
Connecting Functions - Pipelining and CompositionChevron down iconChevron up icon
Connecting Functions - Pipelining and Composition
Pipelining
Chaining and fluent interfaces
Composing
Transducing
Summary
Questions
Designing Functions - RecursionChevron down iconChevron up icon
Designing Functions - Recursion
Using recursion
Recursion techniques
Summary
Questions
Ensuring Purity - ImmutabilityChevron down iconChevron up icon
Ensuring Purity - Immutability
Going the straightforward JavaScript way
Creating persistent data structures
Summary
Questions
Implementing Design Patterns - The Functional WayChevron down iconChevron up icon
Implementing Design Patterns - The Functional Way
Understanding design patterns
Object-oriented design patterns
Functional design patterns
Summary
Questions
Building Better Containers - Functional Data TypesChevron down iconChevron up icon
Building Better Containers - Functional Data Types
Specifying data types
Building containers 
Functions as data structures
Summary
Questions
BibliographyChevron down iconChevron up icon
Bibliography
Answers to QuestionsChevron down iconChevron up icon
Answers to Questions
Chapter 1, Becoming Functional – Several Questions
Chapter 2, Thinking Functionally – a First Example
Chapter 3, Starting Out with Functions – a Core Concept
Chapter 4, Behaving Properly – Pure Functions
Chapter 5, Programming Declaratively – a Better Style
Chapter 6, Producing Functions – Higher-Order Functions
Chapter 7, Transforming Functions – Currying and Partial Application
Chapter 8, Connecting Functions – Pipelining and Composition
Chapter 9, Designing Functions – Recursion
Chapter 10, Ensuring Purity – Immutability
Chapter 11, Implementing Design Patterns – the Functional Way
Chapter 12, Building Better Containers – Functional Data Types
Other Books You May EnjoyChevron down iconChevron up icon
Other Books You May Enjoy
Leave a review - let other readers know what you think

Recommendations for you

Left arrow icon
Full-Stack Flask and React
Full-Stack Flask and React
Read more
Oct 2023408 pages
Full star icon3.8 (5)
eBook
eBook
AU$14.99AU$44.99
AU$55.99
C# 13 and .NET 9 – Modern Cross-Platform Development Fundamentals
C# 13 and .NET 9 – Modern Cross-Platform Development Fundamentals
Read more
Nov 2024828 pages
Full star icon4.3 (4)
eBook
eBook
AU$14.99AU$66.99
AU$82.99
Real-World Web Development with .NET 9
Real-World Web Development with .NET 9
Read more
Dec 2024578 pages
Full star icon3.5 (4)
eBook
eBook
AU$14.99AU$55.99
AU$68.99
Django 5 By Example
Django 5 By Example
Read more
Apr 2024826 pages
Full star icon4.6 (36)
eBook
eBook
AU$14.99AU$55.99
AU$68.99
React and React Native
React and React Native
Read more
Apr 2024518 pages
Full star icon4.2 (9)
eBook
eBook
AU$14.99AU$48.99
AU$60.99
Scalable Application Development with NestJS
Scalable Application Development with NestJS
Read more
Jan 2025614 pages
Full star icon4.5 (6)
eBook
eBook
AU$14.99AU$44.99
AU$55.99
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals
Read more
Nov 2023828 pages
Full star icon4.3 (61)
eBook
eBook
AU$14.99AU$66.99
AU$82.99
Responsive Web Design with HTML5 and CSS
Responsive Web Design with HTML5 and CSS
Read more
Sep 2022504 pages
Full star icon4.5 (56)
eBook
eBook
AU$14.99AU$66.99
AU$82.99
Modern Full-Stack React Projects
Modern Full-Stack React Projects
Read more
Jun 2024506 pages
Full star icon4.8 (9)
eBook
eBook
AU$14.99AU$49.99
AU$61.99
Learning Angular
Learning Angular
Read more
Jan 2025494 pages
Full star icon4.1 (7)
eBook
eBook
AU$14.99AU$49.99
AU$61.99
Right arrow icon

Customer reviews

Top Reviews
Rating distribution
Full star iconFull star iconFull star iconFull star iconHalf star icon4.2
(6 Ratings)
5 star16.7%
4 star83.3%
3 star0%
2 star0%
1 star0%
Filter icon Filter
Top Reviews

Filter reviews by




Charly RamirezApr 27, 2021
Full star iconFull star iconFull star iconFull star iconFull star icon5
Un excelente libro! Altamente recomendado!
Amazon Verified reviewAmazon
SebastianAug 31, 2020
Full star iconFull star iconFull star iconFull star iconEmpty star icon4
Este es un excelente libro de introducción a la programación funcional, los capítulos finales tienen un salto muy grande que lo hace difícil de seguir, pero con apoyo de internet se puede entender lo que el autor trata de explicar
Amazon Verified reviewAmazon
Daler AsrorovDec 25, 2020
Full star iconFull star iconFull star iconFull star iconEmpty star icon4
As I am reading through the chapters of this book I can get what the writer is trying to aim at, but there are multiple occurrences of grammatical errors or poor explanations due to confusing choice of words. One valuable piece of feedback I could give to the the author is to hire a proofreader to help the author structure sentences in more meaningful ways and find better choice of words in some cases. The book content as a whole is great, but it can be hard to follow what the author is trying to say due to poor choice of words or structural sentence inconsistencies.
Amazon Verified reviewAmazon
NINOJan 16, 2023
Full star iconFull star iconFull star iconFull star iconEmpty star icon4
Ha soddisfatto le mie aspettative.Chiaro e con buoni esempi
Amazon Verified reviewAmazon
AlessandroFeb 19, 2022
Full star iconFull star iconFull star iconFull star iconEmpty star icon4
I don't know if I would buy this book again. Some of the code I did not get but I don't have the strongest math skills. I did learn some new techniques for JavaScript and overall the it is very well written. I think I may have to read it a couple of times more for everything to clicks and sticks. I found some concepts challenging to wrap my head around.
Amazon Verified reviewAmazon
  • Arrow left icon Previous
  • 1
  • 2
  • Arrow right icon Next

People who bought this also bought

Left arrow icon
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals
Read more
Nov 2023828 pages
Full star icon4.3 (61)
eBook
eBook
AU$14.99AU$66.99
AU$82.99
Responsive Web Design with HTML5 and CSS
Responsive Web Design with HTML5 and CSS
Read more
Sep 2022504 pages
Full star icon4.5 (56)
eBook
eBook
AU$14.99AU$66.99
AU$82.99
React and React Native
React and React Native
Read more
May 2022606 pages
Full star icon4.6 (17)
eBook
eBook
AU$14.99AU$55.99
AU$68.99
AU$60.99
Building Python Microservices with FastAPI
Building Python Microservices with FastAPI
Read more
Aug 2022420 pages
Full star icon4 (8)
eBook
eBook
AU$14.99AU$51.99
AU$64.99
Right arrow icon

About the author

Profile icon Federico Kereki
Federico Kereki
LinkedIn iconGithub icon
Federico Kereki is a Uruguayan Systems Engineer, with a Master's degree in Education, and over 30 years of experience as a consultant, system developer, and writer. Currently a Subject Matter Expert at Globant, he has taught at Universidad de la República, Universidad ORT Uruguay, and Universidad de la Empresa. He has written articles and booklets on programming, web development, security, and open source topics for blogs, magazines, and websites. He has also written several books, including Modern JavaScript Web Development Cookbook and the upcoming Data Structures and Algorithms in JavaScript. He resides, works, and teaches in Uruguay, but he wrote the first edition of this book while working in India, and the second edition during a sojourn in Mexico.
Read more
See other products by Federico Kereki
Getfree access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook?Chevron down iconChevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website?Chevron down iconChevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook?Chevron down iconChevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support?Chevron down iconChevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks?Chevron down iconChevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook?Chevron down iconChevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.

Create a Free Account To Continue Reading

Modal Close icon
OR
    First name is required.
    Last name is required.

The Password should contain at least :

  • 8 characters
  • 1 uppercase
  • 1 number
Notify me about special offers, personalized product recommendations, and learning tips By signing up for the free trial you will receive emails related to this service, you can unsubscribe at any time
By clicking ‘Create Account’, you are agreeing to ourPrivacy Policy andTerms & Conditions
Already have an account? SIGN IN

Sign in to activate your 7-day free access

Modal Close icon
OR
By redeeming the free trial you will receive emails related to this service, you can unsubscribe at any time.

[8]ページ先頭

©2009-2025 Movatter.jp