3 ms·
On top of persistent data structures, I find the main innovation of Clojure is the way it mixes functional programming with dynamic typing into its unique parad
by xiaq 8y ago
On top of persistent data structures, I find the main innovation of Clojure is the way it mixes functional programming with dynamic typing into its unique paradigm:
a) Data is just data, divorced from behaviors, and functions (not methods) manipulate data. This is really just old-school functional programming.
b) Data is represented by hashmaps, and are extensible - if the data gets richer over time (= more keys in the hashmap), functions that used to work on the previous, poorer data still work on the new, richer data. In other words, each key of a hashmap is automatically an interface, and data satisfies interfaces depending on which keys they have (structural typing).
Now, this interface mechanism is a bit sloppy if keys are just string identifiers, but this is overcome by namespacing keywords (which has its own syntax, ::keyword), thus removing the risk of the same name being used for different purposes. This idea is most clearly articulated in the core.spec library, which encourages you to establish a 1-to-1 relationship between a (namespaced) keyword and a spec (https://clojure.org/guides/spec#_registry https://clojure.org/guides/spec#_registry).
Previous dynamic languages have always used hashmaps to represent data, either directly or indirectly. But once in the land of high-level abstractions, they would revert to "canonical" Java-style OOP (Python and ES6 being the most notable examples). Clojure seems to be the only mainstream-ish language out there that uses -- or at least aspires to use -- hashmaps as a real abstraction mechanism rather than a convenience.
- aidenn0 8y agoEverything you say is not new to lisp though.
- sswezey 8y agoHave you ever looked into Lua? Lua uses their tables (hash maps) for everything. It is a prototypical language similar to Javascript and everything is a key in the table. The language provides syntactic sugar for certain accessors such as `table.x` translates to `table["x"]` to make it seem like a more traditional OOP language.