Table of Contents

Copyright

Brief Table of Contents

Table of Contents

Preface

Acknowledgments

About This Book

About the Cover Illustration

Chapter 1. Introduction to typing

1.1. Whom this book is for

1.2. Why types exist

1.2.1. 0s and 1s

1.2.2. What are types and type systems?

1.3. Benefits of type systems

1.3.1. Correctness

1.3.2. Immutability

1.3.3. Encapsulation

1.3.4. Composability

1.3.5. Readability

1.4. Types of type systems

1.4.1. Dynamic and static typing

1.4.2. Weak and strong typing

1.4.3. Type inference

1.5. In this book

Summary

Chapter 2. Basic types

2.1. Designing functions that don’t return values

2.1.1. The empty type

2.1.2. The unit type

2.1.3. Exercises

2.2. Boolean logic and short circuits

2.2.1. Boolean expressions

2.2.2. Short circuit evaluation

2.2.3. Exercise

2.3. Common pitfalls of numerical types

2.3.1. Integer types and overflow

2.3.2. Floating-point types and rounding

2.3.3. Arbitrarily large numbers

2.3.4. Exercises

2.4. Encoding text

2.4.1. Breaking text

2.4.2. Encodings

2.4.3. Encoding libraries

2.4.4. Exercises

2.5. Building data structures with arrays and references

2.5.1. Fixed-size arrays

2.5.2. References

2.5.3. Efficient lists

2.5.4. Binary trees

2.5.5. Associative arrays

2.5.6. Implementation trade-offs

2.5.7. Exercise

Summary

Answers to exercises

Designing functions that don’t return values

Boolean logic and short circuits

Common pitfalls of numerical types

Encoding text

Building data structures with arrays and references

Chapter 3. Composition

3.1. Compound types

3.1.1. Tuples

3.1.2. Assigning meaning

3.1.3. Maintaining invariants

3.1.4. Exercise

3.2. Expressing either-or with types

3.2.1. Enumerations

3.2.2. Optional types

3.2.3. Result or error

3.2.4. Variants

3.2.5. Exercises

3.3. The visitor pattern

3.3.1. A naïve implementation

3.3.2. Using the visitor pattern

3.3.3. Visiting a variant

3.3.4. Exercise

3.4. Algebraic data types

3.4.1. Product types

3.4.2. Sum types

3.4.3. Exercises

Summary

Answers to exercises

Compound types

Expressing either-or with types

The visitor pattern

Algebraic data types

Chapter 4. Type safety

4.1. Avoiding primitive obsession to prevent misinterpretation

4.1.1. The Mars Climate Orbiter

4.1.2. The primitive obsession antipattern

4.1.3. Exercise

4.2. Enforcing constraints

4.2.1. Enforcing constraints with the constructor

4.2.2. Enforcing constraints with a factory

4.2.3. Exercise

4.3. Adding type information

4.3.1. Type casting

4.3.2. Tracking types outside the type system

4.3.3. Common type casts

4.3.4. Exercises

4.4. Hiding and restoring type information

4.4.1. Heterogenous collections

4.4.2. Serialization

4.4.3. Exercises

Summary

Answers to exercises

Avoiding primitive obsession to prevent misinterpretation

Enforcing constraints

Adding type information

Hiding and restoring type information

Chapter 5. Function types

5.1. A simple strategy pattern

5.1.1. A functional strategy

5.1.2. Typing functions

5.1.3. Strategy implementations

5.1.4. First-class functions

5.1.5. Exercises

5.2. A state machine without switch statements

5.2.1. Early Programming with Types

5.2.2. State machines

5.2.3. State machine implementation recap

5.2.4. Exercises

5.3. Avoiding expensive computation with lazy values

5.3.1. Lambdas

5.3.2. Exercise

5.4. Using map, filter, and reduce

5.4.1. map()

5.4.2. filter()

5.4.3. reduce()

5.4.4. Library support

5.4.5. Exercises

5.5. Functional programming

Summary

Answers to exercises

A simple strategy pattern

A state machine without switch statements

Avoiding expensive computation with lazy values

Using map, filter, and reduce

Chapter 6. Advanced applications of function types

6.1. A simple decorator pattern

6.1.1. A functional decorator

6.1.2. Decorator implementations

6.1.3. Closures

6.1.4. Exercises

6.2. Implementing a counter

6.2.1. An object-oriented counter

6.2.2. A functional counter

6.2.3. A resumable counter

6.2.4. Counter implementations recap

6.2.5. Exercises

6.3. Executing long-running operations asynchronously

6.3.1. Synchronous execution

6.3.2. Asynchronous execution: callbacks

6.3.3. Asynchronous execution models

6.3.4. Asynchronous functions recap

6.3.5. Exercises

6.4. Simplifying asynchronous code

6.4.1. Chaining promises

6.4.2. Creating promises

6.4.3. More about promises

6.4.4. async/await

6.4.5. Clean asynchronous code recap

6.4.6. Exercises

Summary

Answers to exercises

A simple decorator pattern

Implementing a counter

Executing long-running operations asynchronously

Simplifying asynchronous code

Chapter 7. Subtyping

7.1. Distinguishing between similar types in TypeScript

7.1.1. Structural and nominal subtyping pros and cons

7.1.2. Simulating nominal subtyping in TypeScript

7.1.3. Exercises

7.2. Assigning anything to, assigning to anything

7.2.1. Safe deserialization

7.2.2. Values for error cases

7.2.3. Top and bottom types recap

7.2.4. Exercises

7.3. Allowed substitutions

7.3.1. Subtyping and sum types

7.3.2. Subtyping and collections

7.3.3. Subtyping and function return types

7.3.4. Subtyping and function argument types

7.3.5. Variance recap

7.3.6. Exercises

Summary

Answers to exercises

Distinguishing between similar types in TypeScript

Assigning anything to, assigning to anything

Allowed substitutions

Chapter 8. Elements of object-oriented programming

8.1. Defining contracts with interfaces

8.1.1. Exercises

8.2. Inheriting data and behavior

8.2.1. The is-a rule of thumb

8.2.2. Modeling a hierarchy

8.2.3. Parameterizing behavior of expressions

8.2.4. Exercises

8.3. Composing data and behavior

8.3.1. The has-a rule of thumb

8.3.2. Composite classes

8.3.3. Implementing the adapter pattern

8.3.4. Exercises

8.4. Extending data and behavior

8.4.1. Extending behavior with composition

8.4.2. Extending behavior with mix-ins

8.4.3. Mix-in in TypeScript

8.4.4. Exercise

8.5. Alternatives to purely object-oriented code

8.5.1. Sum types

8.5.2. Functional programming

8.5.3. Generic programming

Summary

Answers to exercises

Defining contracts with interfaces

Inheriting data and behavior

Composing data and behavior

Extending data and behavior

Chapter 9. Generic data structures

9.1. Decoupling concerns

9.1.1. A reusable identity function

9.1.2. The optional type

9.1.3. Generic types

9.1.4. Exercises

9.2. Generic data layout

9.2.1. Generic data structures

9.2.2. What is a data structure?

9.2.3. Exercises

9.3. Traversing any data structure

9.3.1. Using iterators

9.3.2. Streamlining iteration code

9.3.3. Iterators recap

9.3.4. Exercises

9.4. Streaming data

9.4.1. Processing pipelines

9.4.2. Exercises

Summary

Answers to exercises

Decoupling concerns

Generic data layout

Traversing any data structure

Streaming data

Chapter 10. Generic algorithms and iterators

10.1. Better map(), filter(), reduce()

10.1.1. map()

10.1.2. filter()

10.1.3. reduce()

10.1.4. filter()/reduce() pipeline

10.1.5. Exercises

10.2. Common algorithms

10.2.1. Algorithms instead of loops

10.2.2. Implementing a fluent pipeline

10.2.3. Exercises

10.3. Constraining type parameters

10.3.1. Generic data structures with type constraints

10.3.2. Generic algorithms with type constraints

10.3.3. Exercise

10.4. Efficient reverse and other algorithms using iterators

10.4.1. Iterator building blocks

10.4.2. A useful find()

10.4.3. An efficient reverse()

10.4.4. Efficient element retrieval

10.4.5. Iterator recap

10.4.6. Exercises

10.5. Adaptive algorithms

10.5.1. Exercise

Summary

Answers to exercises

Better map(), filter(), reduce()

Common algorithms

Constraining type parameters

Efficient reverse and other algorithms using iterators

Adaptive algorithms

Chapter 11. Higher kinded types and beyond

11.1. An even more general map

11.1.1. Processing results or propagating errors

11.1.2. Mix-and-match function application

11.1.3. Functors and higher kinded types

11.1.4. Functors for functions

11.1.5. Exercise

11.2. Monads

11.2.1. Result or error

11.2.2. Difference between map() and bind()

11.2.3. The monad pattern

11.2.4. The continuation monad

11.2.5. The list monad

11.2.6. Other monads

11.2.7. Exercise

11.3. Where to next?

11.3.1. Functional programming

11.3.2. Generic programming

11.3.3. Higher kinded types and category theory

11.3.4. Dependent types

11.3.5. Linear types

Summary

11.4. Answers to exercises

An even more general map

Monads

A. TypeScript installation and source code

Online

Local

Source Code

DIY

B. TypeScript cheat sheet

 Types and possible values

 Common algorithms

map()

filter()

reduce()

Index

List of Figures

List of Tables

List of Listings

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.199.88