by hasty_pudding · 2y ago · 0 comments
God i love to see this
[1] https://github.com/tonaljs/tonal/blob/main/packages/chord/in...
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.The functions are kept pure and the data is kept pure.
As opposed to an object oriented programming style in which data and functions share the same object and create bug ridden state machines thats not as easy to reason about.
Some further explanation about this:
https://www.goodreads.com/quotes/702062-immutable-objects-ar...
a semi-nuanced discussion:
https://www.reddit.com/r/scala/comments/ejwsp1/is_mutability...
you can just Google mutable state to find more info.
separating your data structures and functions in many cases will make your code cleaner and more bug free.
Your first link's first sentence's first two words are "Immutable objects". No problem there. It doesn't conflate objects with mutability.
the point is obvious.
Being that every single language on Earth uses objects for non-primitive data structures that should be inferrable to a reasonable person.
a data structure in that context is clearly an ordered grouping of objects versus an unordered/loose grouping of objects.
I guess maybe low level c and assembly where you implement everything as bits in physical memory?
There's often a stack-/heap-allocated dichotomy. Some languages consider stack-allocated structs not to be objects. They have no reference identity. Assignment has copy semantics. C# is one such language.