JUCS - Journal of Universal Computer Science 23(1): 112-126, doi: 10.3217/jucs-023-01-0112
First-Class Functions in an Imperative World
expand article infoRoberto Ierusalimschy
‡ PUC-Rio, Rio de Janeiro, Brazil
Open Access
First-class functions are a hallmark of functional languages, but they are a useful concept in imperative languages, too. Even ANSI C offers a restricted form of first-class functions (in the form of pointers to functions), and several more recent imperative languages, such as Python 3, Go, Lua, and Swift, offer first-class, anonymous functions with lexical scoping. In most imperative languages, however, first-class functions are an advanced feature used by seasoned programmers. Lua, by contrast, uses first-class functions as a building block of the language. Lua programmers regularly benefit from diverse properties of its functions for routine constructions such as exception handling, module definitions, object-oriented programming, and iterators. Moreover, first-class functions play a central role in the API between Lua and C. In this paper, we present some aspects of Lua that were designed with first-class functions in mind, in particular its module system, exception handling, iterators, facilities for object-oriented programming, and the API between C and Lua. We also discuss how those different aspects of Lua use different properties of first-class functions to achieve two important goals in the design of Lua, namely small size and embeddability (its easiness of interfacing with other languages).
Lua, scripting languages, functional languages