3 Prevalent Themes in the Hacker News Discussion
1. Arrays and Functions are Conceptually Equivalent in Type Theory
Many users argued that from a mathematical or functional programming perspective, an array can be viewed as a function mapping an index (domain) to a value (codomain). This equivalence is foundational in functional languages and type theory, where both are defined by their input-output mappings.
"In fact, from wikipedia: 'An n-tuple can be formally defined as the image of a function that has the set of the n first natural numbers as its domain.'" â stevefan1999
"In Clojure, vectors literally are functions. You can supply a vector (~= array) or map any place that a single parameter function is expected." â sethev
"Haskell provides indexable arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers." â [Referenced in the discussion]
2. Practical Utility vs. Theoretical Elegance
A recurring debate centered on whether unifying arrays and functions is practically useful or merely an abstract, academic exercise. Proponents highlighted benefits like memoization and language simplicity, while critics argued it obscures performance realities and common programming needs.
"For example this insight explains Memoization. If you know that Arrays are Functions or equivalently Functions are Arrays, in some sense, then Memoization is obvious." â tialaramex
"This is one of those rare times when I read something coming our of the FP community and go 'oh, you mean iterators, we've had those for decades over here in imperative-programming land'" â swiftcoder
"This presumes the framework in which one is working. The type of map is and always will be the same as the type of function. This is a simple fact of type theory, so it is worthwhile to ponder the value of providing a language mechanism to coerce one into another." â d-us-vb
"Industry programmers do not struggle because arrays lack ontological clarity. They struggle because memory hierarchies exist, cache lines exist, branch predictors exist, GPUs exist, deadlines exist." â diimdeep
3. Implementation and Performance Considerations
Many participants moved beyond theory to discuss how the array-function equivalence manifests in real systems, emphasizing that computational performance (e.g., memory access vs. recomputation) and language-specific implementations are critical practical concerns.
"Modern CPUs are usually bottlenecked by memory bandwidth and cache size. So a function that recomputes the value can often be quicker than a look up table, at least outside of microbenchmarks." â VorpalWay
"However the big difference (in most languages) is that functions can take arbitrarily long. Array access either succeeds or fails quickly." â eru
"An array is a data structure that stores pre-calculated values in memory, whereas a function is executable logic that computes a result only when it is called." â dankwizard
"No, the best thing you can do for simplicity is to not conflate concepts. The perpetual idea of mixing data and execution is a misguided search for a silver bullet..." â CyberDildonics