by pcthrowaway · 2y ago · 0 comments
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.
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.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.
You could have a completely stateless class/object that would manipulate those nodes and add them and remove them from the data structure.