Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Another way to think of currying is that it creates a scoping that lasts for longer than the lifetime of the function in which it is defined.   That is, the returned function from make_addX retains the scoping defined by the running of make_addX long after make_addX has finished executing.   All of this is part of the notion of a function's "closure", a topic we will discuss more formally very soon,

In the language of Design Patterns, make-addX is called a "factory" because it "manufactures" a function that we can use later. 

Some important consequences of currying that we will exploit:

  • Using invariant code, we can generate variant functions that behave differently simply by supplying different input values to the factory function that made them. 
  • The functions made by the factory can be used elsewhere in our system, meaning that we can dynamically change one part of the system and have those changes be reflected as different behaviors in a different part of the system, even though the code does not change at all.  In effect, we can have parts of our system interact with each other without directly coupling their code together.    This will become hugely important when we try to build larger, more complex systems.