5 ms·
They're using objects, including arrays [1], so I think they would have been more precise to say that they're using objects and arrays as simple data structures
by metaxy2 3y ago
They're using objects, including arrays [1], so I think they would have been more precise to say that they're using objects and arrays as simple data structures rather than using OO features like inheritance and mutation.
[1] https://github.com/tonaljs/tonal/blob/main/packages/chord/index.ts https://github.com/tonaljs/tonal/blob/main/packages/chord/in...
- pcthrowaway 3y agoThis is just semantics then. If you're implementing a linked list or a tree without a class (or at the very least a function that behaves like a class), it's going to be awkward and clumsy. Data-structures beyond what the standard lib gives you (string and array) are the main reason the addition of classes to JS was so helpful.
- eitacaraleo 3y agoUh… what? It’s quite the opposite; why would you need a class for linked lists and trees?
- hasty_pudding 3y agoYou definitely need generic node objects to define the nodes. You could have a completely stateless class/object that would manipulate those nodes and add them and remove them from the data structure.
- metaxy2 3y agoWell, in principle you could have the node objects be created with an object literal in an outside builder function, and the manipulation also be done by outside functions. That's how it's done in languages without OOP. The question is how far are you willing to go to avoid doing anything that looks like OOP.
- hasty_pudding 3y agoVery far
- recursive 3y agoTo provide an easy place to find the behaviors that are included with those data structures.
- kamov 3y agoThis isn't quite right, linked lists and trees in functional languages can be represented extremely succintly, i.e. data List a = Nil | Node a (List a) data Tree a = Leaf a | Node (Tree a) (Tree a) there is nothing stopping you from writing something like this in JS const list = { kind: 'node', val: 3, next: { kind: 'nil' } }; And then using a switch case to navigate the list/tree matching on the kind field.
- recursive 3y agoAbsolutely. The navigation behaviors will have to be in functions though. And from an application code developer's perspective, the connection between these objects and functions may not be obvious. If only there were some way to associate behavior with state.
- lioeters 3y agoThe whole point of the top comment is that they appreciate how this library chose not to associate behavior with state, and instead organized things as functions and data structures. With the latter approach, the user is not limited to the bahaviors that the library author wrote. The data (not state) can be used freely. Similarly, the functions are not tied to any particular state - the data can come from anywhere, as long as it has the right shape.