Chapter 1. Introduction to typing
Listing 1.1. Trying to interpret data as code
Listing 1.2. Insufficient type information
Listing 1.3. Refined type information
Listing 1.6. Not enough encapsulation
Listing 1.8. Noncomposable system
Listing 1.9. Noncomposable system update
Chapter 2. Basic types
Listing 2.1. Raising and logging an error if a config file is not found
Listing 2.2. Empty type implemented as an uninstantiable class
Listing 2.3. A “Hello world!” function
Listing 2.4. Unit type implemented as a singleton without state
Listing 2.6. Alternative gatekeeper implementation
Listing 2.7. Function adding up item total
Listing 2.8. Checking for addition overflow
Listing 2.9. Currency class and currency addition function
Listing 2.10. Floating-point equality within epsilon
Listing 2.11. Simple text-breaking function
Listing 2.12. Text-breaking function using grapheme-splitter library
Listing 2.13. Linked-list implementation
Listing 2.14. Array-based list implementation
Listing 2.15. Array-based list implementation with additional capacity
Chapter 3. Composition
Listing 3.1. Distance between two points
Listing 3.2. Distance between two points defined as tuples
Listing 3.4. Distance between two points defined as records
Listing 3.5. Ill-formed currency
Listing 3.6. Currency maintaining invariants
Listing 3.7. Immutable Currency
Listing 3.8. Encoding day of week as a number
Listing 3.9. Encoding day of week with constants
Listing 3.10. Encoding day of week as an enum
Listing 3.11. Parsing input into a DayOfWeek or undefined
Listing 3.13. Returning result and error from a function
Listing 3.15. Returning result or error from a function
Listing 3.16. Tagged union of shapes
Listing 3.18. Union of shapes as variant
Listing 3.19. Naïve implementation
Chapter 4. Type safety
Listing 4.1. Sketch of incompatible components
Listing 4.2. Pound-force second and Newton-second types
Listing 4.3. Converting lbfs to Ns
Listing 4.4. Updated components
Listing 4.5. Constructor throwing on invalid value
Listing 4.6. Constructor coercing an invalid value
Listing 4.7. Factory returning undefined on invalid value
Listing 4.8. Type cast causing a run-time error
Listing 4.9. Revisiting Either implementation
Listing 4.10. makeLeft and makeRight
Listing 4.11. Triangle or Square
Listing 4.12. isLeft() and getLeft()
Listing 4.15. A collection of types implementing IDocumentItem
Listing 4.16. A collection of types as a sum type
Listing 4.17. A collection of unknown type
Listing 4.18. Serializing a cat
Listing 4.19. Deserializing a Cat
Chapter 5. Function types
Listing 5.1. Car-wash strategy
Listing 5.2. Car-wash strategy revisited
Listing 5.3. Pluggable Greeter
Listing 5.6. State machine implementation
Listing 5.7. Alternative state machine implementation
Listing 5.8. Eager Car production
Listing 5.9. Lazy Car production
Listing 5.10. Anonymous Car production
Chapter 6. Advanced applications of function types
Listing 6.1. WidgetFactory decorator
Listing 6.2. Functional widget factory
Listing 6.3. Functional widget factory decorator
Listing 6.4. Decorator function
Listing 6.6. Object-oriented counter
Listing 6.7. Functional counter
Listing 6.8. Resumable counter
Listing 6.9. Synchronous execution
Listing 6.10. Asynchronous execution with callback
Listing 6.11. Counting down in an event loop
Listing 6.12. Two counters in an event loop
Listing 6.13. Counter with callback
Listing 6.14. Chaining callbacks
Listing 6.15. Functions returning promises
Listing 6.16. Chaining promises
Listing 6.17. getUserName() returning a promise
Listing 6.18. Rejecting a promise
Listing 6.19. Chaining functions returning promises
Listing 6.20. Chaining functions that don’t return promises
Listing 6.21. Using Promise.all() to sequence execution
Listing 6.22. Using Promise.race() to sequence execution
Chapter 7. Subtyping
Listing 7.1. Pound-force second and Newton-second types
Listing 7.2. Pound-force second and Newton-second without unique symbols
Listing 7.3. User is structurally a subtype of Named
Listing 7.4. Simulating nominal subtyping
Listing 7.5. Deserializing any
Listing 7.6. Run-time type checking for User
Listing 7.7. Stronger typing using unknown
Listing 7.8. TurnDirection to angle conversion
Listing 7.10. turnAngle() using fail()
Listing 7.11. turnAgain() using fail() and returning a dummy value
Listing 7.12. turnAngle() using fail() and returning its result
Listing 7.13. Triangle | Square as Triangle | Square | Circle
Listing 7.14. Triangle | Square | Circle as Triangle | Square
Listing 7.15. EquilateralTriangle declaration
Listing 7.16. Triangle[] as Shape[]
Listing 7.17. LinkedList<Triangle> as LinkedList<Shape>
Listing 7.18. () => Triangle as () => Shape
Listing 7.19. () => Shape as () => Triangle
Listing 7.20. Overriding a method with a subtype as return type
Listing 7.21. Draw and render functions
Listing 7.22. Shape and Triangle with isRightAngled() method
Listing 7.23. Updated draw and render functions
Listing 7.24. Attempting to call isRightAngled() on a supertype of Triangle
Chapter 8. Elements of object-oriented programming
Listing 8.3. Extended logger interface
Listing 8.4. Combining interfaces
Listing 8.6. Expression hierarchy
Listing 8.7. Inheritance and composition
Listing 8.11. Hunting behavior
Listing 8.12. Extending an instance with the members of another one
Listing 8.13. Mixing in behavior
Listing 8.14. Visitor with OOP
Chapter 9. Generic data structures
Listing 9.2. Default transform()
Listing 9.3. assembleWidgets()
Listing 9.5. doNothing() and pluckAll()
Listing 9.7. Unsafe use of any
Listing 9.11. Binary tree of numbers
Listing 9.12. Linked list of strings
Listing 9.13. Generic binary tree
Listing 9.14. Generic linked list
Listing 9.16. printInOrder() example
Listing 9.17. Print linked list
Listing 9.18. printLinkedList() example
Listing 9.20. Iterator interface
Listing 9.21. Binary tree iterator
Listing 9.22. Linked list iterator
Listing 9.23. print() using iterator
Listing 9.24. contains() using iterator
Listing 9.25. Iterable interface
Listing 9.26. Iterable linked list
Listing 9.27. print() and contains() with Iterator argument
Listing 9.28. print() and contains() with Iterable argument
Listing 9.29. Binary tree iterator
Listing 9.30. Binary tree iterator using generator
Listing 9.31. Linked list iterator
Listing 9.32. Linked list iterator using generator
Listing 9.33. Iterable linked list using generator
Listing 9.34. Inifinite stream of random numbers
Chapter 10. Generic algorithms and iterators
Listing 10.2. map() with iterator
Listing 10.4. filter() with iterator
Listing 10.6. reduce() with iterator
Listing 10.7. filter()/reduce() pipeline
Listing 10.8. filter/reduce pipeline
Listing 10.10. Fluent filter/reduce pipeline
Listing 10.11. Better fluent iterable
Listing 10.12. Better fluent filter/reduce pipeline
Listing 10.13. renderAll sketch
Listing 10.14. renderAll with constraint
Listing 10.15. IComparable interface
Listing 10.16. max() algorithm
Listing 10.17. max() algorithm with compare() argument
Listing 10.18. reverse() with stack
Listing 10.19. reverse() for array
Listing 10.20. IReadable<T> and IIncrementable<T>
Listing 10.21. IInputIterator<T>
Listing 10.22. Linked list implementation
Listing 10.23. Linked list input iterator
Listing 10.24. Pair of iterators over linked list
Listing 10.25. IWritable<T> and IOutputIterator<T>
Listing 10.26. Console output iterator
Listing 10.27. map() with input and output iterators
Listing 10.28. find() with iterable
Listing 10.29. IForwardIterator<T>
Listing 10.30. LinkedListIterator<T> implementing IForwardIterator<T>
Listing 10.31. find() with forward iterator
Listing 10.32. Replacing 42 with 0 in a linked list
Listing 10.33. IBidirectionalIterator<T> and ArrayIterator<T>
Listing 10.34. reverse() with bidirectional iterator
Listing 10.35. Reversing an array of numbers
Listing 10.36. IRandomAccessIterator<T>
Listing 10.37. ArrayIterator<T> implementing a random-access iterator
Listing 10.39. elementAt() with input and random-access iterators
Chapter 11. Higher kinded types and beyond
Listing 11.7. square() and stringify()
Listing 11.8. readNumber() return type
Listing 11.9. Processing a number
Listing 11.10. Processing with map()
Listing 11.11. Processing with lambda
Listing 11.12. Unpacking values for square()
Listing 11.13. Unpacking values for stringify()
Listing 11.15. Sketch of Functor interface
Listing 11.16. Box implementing the interface
Listing 11.17. Functor interface
Listing 11.19. Applying map() over a function
Listing 11.20. Functions returning result or error
Listing 11.22. Processing and explicitly checking for errors
Listing 11.24. Incompatible types
Listing 11.26. Branchless readCatFromFile()
18.220.137.164