Mastering JavaScript Functional Programming.. - Federico Kereki - E-Book

Mastering JavaScript Functional Programming.. E-Book

Federico Kereki

0,0
35,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Functional programming is a programming paradigm that uses functions for developing software. This book is filled with examples that enable you to leverage the latest JavaScript and TypeScript versions to produce modern and clean code, as well as teach you to how apply functional programming techniques to develop more efficient algorithms, write more concise code, and simplify unit testing.
This book provides comprehensive coverage of the major topics in functional programming to produce shorter, clearer, and testable programs. You’ll begin by getting to grips with writing and testing pure functions, reducing side effects, as well as other key features to make your applications functional in nature. The book specifically explores techniques to simplify coding, apply recursion, perform high-level coding, learn ways to achieve immutability, implement design patterns, and work with data types.
By the end of this book, you’ll have developed the practical programming skills needed to confidently enhance your applications by adding functional programming to wherever it’s most suitable.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 720

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Mastering JavaScript Functional Programming

Third Edition

Write clean, robust, and maintainable web and server code using functional JavaScript and TypeScript

Federico Kereki

BIRMINGHAM—MUMBAI

Mastering JavaScript Functional Programming

Third Edition

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Pavan Ramchandani

Publishing Product Manager: Bhavya Rao

Senior Editor: Mark D’Souza

Technical Editor: Joseph Aloocaran

Copy Editor: Safis Editing

Project Coordinator: Sonam Pandey

Proofreader: Safis Editing

Indexer: Rekha Nair

Production Designer: Shyam Sundar Korumilli

Marketing Coordinators: Namita Velgekar, Nivedita Pandey, and Anamika Singh

First published: November 2017

Second edition: January 2020

Third edition: May 2023

Production reference: 1040423

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80461-013-8

www.packtpub.com

Writing a book involves many people, and even if I cannot mention and name all of them, there are some who really deserve to be highlighted.

At Packt Publishing, I want to thank Larissa Pinto, senior acquisition editor, for proposing the theme for this book and helping me get started with it. Thanks must also go to Mohammed Yusuf Imaratwale, content development editor, and Ralph Rosario, technical editor, for their help in giving shape to the book and making it clearer and better structured. I also want to send my appreciation to the reviewers, Gerónimo García Sgritta and Steve Perkins, who went through the initial draft, enhancing it with their comments.

There are some other people who deserve extra consideration. This book was written under unusual circumstances, around 10,000 miles away from home! I had gone from Uruguay, where I live, to work on a project in India, and that’s where I wrote every single page of the text. This would not have been possible if I hadn’t had complete support from my family, who stayed in Montevideo, but who were constantly nearby, thanks to the internet and modern communication. In particular, I must single out my wife, Sylvia Tosar, not only for supporting and aiding me both with the project and the book but also for dealing with everything and the rest of the family on her own in Uruguay—this book wouldn’t have been possible otherwise, and she is the greatest reason the book could be written!

For the second edition: Revisiting and expanding a book for a second edition was an interesting task. I had great support and must thank Aamir Ahmed, content development editor; Jane D’Souza, technical editor; and Crystian Bietti and Steve Perkins (again, for double merit!), the reviewers who helped produce a much better text.

For the third edition: Expanding the book for a third edition was, once more, a challenging task. In this case, I had support from Bhavya Rao, publishing product manager; Mark D’Souza, senior editor; Joseph Aloocaran, technical editor; and Anu Nagan and Markandey Pathak, reviewers, all of whom greatly aided my work, aiming for an even higher quality final text.

- Federico Kereki

Contributors

About the author

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.

He is currently a subject matter expert at Globant, where he gets to use a good mixture of development frameworks, programming tools, and operating systems. He is familiar with languages such as JavaScript and TypeScript; modern backend runtime environments such as Node.js and Deno; frontend frameworks such as React, Vue, and Angular; and services, microservices, and cloud components for systems architecture.

He has taught several computer science courses at Universidad de la República, Universidad ORT Uruguay, and Universidad de la Empresa. He has also written texts for these courses.

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.

Kereki has given talks on functional programming at public conferences (such as JSCONF 2016 and Development Week Santiago 2019) and has used functional programming techniques to develop internet systems for businesses in Uruguay and abroad.

His current interests tend toward software quality and software engineering – with Agile methodologies topmost – while on the practical side, he works with diverse languages, tools, and frameworks, and Free/Libre Open Source Software (FLOSS) wherever possible!

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; the third edition was the first actually completed in his homeland!

About the reviewers

Markandey Pathak is an accomplished full-stack developer and software architect with over 11 years of experience. Currently, he works as an expert engineer and vice president with one of the biggest financial firms. He has also worked with top firms such as Deloitte, PwC, and Sapient in the past. In addition to his technical work, Markandey is also passionate about teaching coding to kids and playing with robots in his spare time. He is an avid traveler and enjoys spending time in nature and exploring new destinations with his wife and son whenever possible. Markandey’s diverse interests and impressive professional experience make him a well-rounded and highly respected member of the software engineering community.

Anu Nagan G has worked in various corporate organizations, from a SaaS start-up (Gendeep) to a mid-size organization (GAVS) and a Fortune 500 company (DXC). He has held various positions such as technical product manager, full-stack product lead (Angular, Java, Python, and AWS), and delivery lead in his 9+ years of tenure. He has experience in leading advanced AI and analytics product Cortex AI (API automation), Salesforce CRM B2B automation, and mobile app development, and contributed to various AIOps products such as ZIF, Gcare, and Gavel in the past. Currently, he is leading parallel projects with Bounteous, which include data integration with the Braze marketing platform with a custom middleware and digital experience transformation for Fintech customers using AEM. He is an avid reader and cinephile who loves to play the guitar and make short films with his friends.

I would like to thank my wife, Hema, and my daughter, Chekhov, for always giving me the freedom to pursue my interests.

Table of Contents

Preface

1

Becoming Functional – Several Questions

What is functional programming?

Theory versus practice

A different way of thinking

FP and other programming paradigms

What FP is not

Why use FP?

What we need

What we get

Not all is gold

Is JavaScript functional?

JavaScript as a tool

Going functional with JavaScript

Key features of JavaScript

How do we work with JavaScript?

Using transpilers

Working online

A step further – TypeScript

Testing

Summary

Questions

2

Thinking Functionally – A First Example

Our problem – doing something only once

Solution 1 – hoping for the best!

Solution 2 – using a global flag

Solution 3 – removing the handler

Solution 4 – changing the handler

Solution 5 – disabling the button

Solution 6 – redefining the handler

Solution 7 – using a local flag

A functional solution to our problem

A higher-order solution

Testing the solution manually

Testing the solution automatically

Producing an even better solution

Summary

Questions

3

Starting Out with Functions – A Core Concept

All about functions

Of lambdas and functions

Arrow functions – the modern way

Functions as objects

Using functions in FP ways

Injection – sorting it out

Callbacks and promises

Continuation-passing style

Polyfills

Stubbing

Immediate invocation (IIFE)

Summary

Questions

4

Behaving Properly – Pure Functions

Pure functions

Referential transparency

Side effects

Advantages of pure functions

Impure functions

Avoiding impure functions

Is your function pure?

Testing – pure versus impure

Testing pure functions

Testing purified functions

Testing impure functions

Summary

Questions

5

Programming Declaratively – A Better Style

Transformations

Reducing an array to a value

Applying an operation – map()

Dealing with arrays of arrays

More general looping

Logical HOFs

Filtering an array

Searching an array

Higher-level predicates – every() and some()

Checking negatives – none()

Working with async functions

Some strange behaviors

Async-ready looping

Working with parallel functions

Unresponsive pages

A frontend worker

A backend worker

Workers, FP style

Long-living pooled workers

Summary

Questions

6

Producing Functions – Higher-Order Functions

Wrapping functions – keeping behavior

Logging

Timing functions

Memoizing functions

Altering a function’s behavior

Doing things once, revisited

Logically negating a function

Inverting the results

Arity changing

Throttling and debouncing

Changing functions in other ways

Turning operations into functions

Turning functions into promises

Getting a property from an object

Demethodizing – turning methods into functions

Methodizing – turning functions into methods

Finding the optimum

Summary

Questions

7

Transforming Functions – Currying and Partial Application

A bit of theory

Currying

Dealing with many parameters

Currying by hand

Currying with bind()

Partial application

Partial application with arrow functions

Partial application with closures

Partial currying

Partial currying with bind()

Partial currying with closures

Final thoughts

Variable number of parameters

Parameter order

Being functional

Summary

Questions

8

Connecting Functions – Pipelining, Composition, and More

Pipelining

Piping in Unix/Linux

Revisiting an example

Creating pipelines

Debugging pipelines

Pointfree style

Chaining and fluent interfaces

An example of fluent APIs

Chaining method calls

Composing

Some examples of composition

Composing with higher-order functions

Transducing

Composing reducers

Generalizing for all reducers

Testing connected functions

Testing pipelined functions

Testing composed functions

Testing chained functions

Testing transduced functions

Summary

Questions

9

Designing Functions – Recursion

Using recursion

Thinking recursively

Higher-order functions revisited

Searching and backtracking

Mutual recursion

Odds and evens

Doing arithmetic

Recursion techniques

Tail call optimization

Continuation-passing style

Trampolines and thunks

Recursion elimination

Summary

Questions

10

Ensuring Purity – Immutability

Going the straightforward JavaScript way

Mutator functions

Constants

Freezing

Cloning and mutating

Getters and setters

Lenses

Prisms

Creating persistent data structures

Working with lists

Updating objects

A final caveat

Summary

Questions

11

Implementing Design Patterns – The Functional Way

Understanding design patterns

Design pattern categories

Do we need design patterns?

Object-oriented design patterns

Facade and Adapter

Decorator or Wrapper

Strategy, Template, and Command

Dependency Injection

Observers and reactive programming

Other patterns

Functional design patterns

Summary

Questions

12

Building Better Containers – Functional Data Types

Specifying data types

Signatures for functions

Other data type options

Building containers

Extending current data types

Containers and functors

Monads

Functions as data structures

Binary trees in Haskell

Functions as binary trees

Summary

Questions

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, Composition, and More

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

Bibliography

Index

Other Books You May Enjoy