11 First-class functions: Part 2

 
Image

In this chapter

  • Learn more applications of replace body with callback.
  • Understand how returning functions from functions can give functions superpowers.
  • Get lots of practice writing higher-order functions to sharpen your skills.

In the last chapter, we learned skills for creating higher-order functions. This chapter will deepen the learning as we apply those skills to more examples. We start by codifying our copy-on-write discipline. We then improve the logging system so it’s not as much work.

One code smell and two refactorings

In the last chapter, we learned a code smell and two refactorings that help us eliminate duplication and find better abstractions. They let us create first-class values and higher-order functions. Just as a reminder, here they are again. We’ll apply these new skills throughout the whole of part 2 of this book. Here they are again for reference.

Code smell: Implicit argument in function name

This code smell identifies aspects of code that could be better expressed as first-class values. If you are referring to a value in the body of a function, and that value is named in the function name, this smell applies. The solution is the next refactoring.

Characteristics
  1. There are very similar function implementations.
  2. The name of the function indicates the difference in implementation.

Refactoring: Express implicit argument

Refactoring copy-on-write

Refactoring copy-on-write for arrays

Returning functions from functions

Conclusion

Summary

Up next…

sitemap