3 ms·
I don't know much about these concepts but isn't `const objToArray = ({ a }) => [a];` losing data, that being the key of the value in the object? I'm asking be
by egeozcan 9y ago
I don't know much about these concepts but isn't `const objToArray = ({ a }) => [a];` losing data, that being the key of the value in the object? I'm asking because it says that "Isomorphism is a pair of transformations between two categories with no data loss".
In any case, this is very helpful, thanks for writing/sharing.
- wereHamster 9y agoThe pair of functions form an isomorphism. You have these two laws: forall x. objToArray(arrayToObj(x)) == x forall x. arrayToObj(objToArray(x)) == x
- paavohtl 9y agoIt's a pair of transformations between [A] and { a: A }, not between arbitrary arrays and objects. As long as you know what the transformation is, you can convert between them without data loss.
- navaati 9y agoEDIT: see paavohtl's comment: I hadn't payed attention to the types, dumb me. You're right, because for a pair of functions f and g, you have an isomorphism if: f(g(x)) == x g(f(x)) == x for every x. However, here of course (([a]) => {a})( (({ a }) => [a])({ key: 'data'}) ) is equal to { a: 'data' } The OP doesn't quite master what he's talking about…
- idbehold 9y agoYeah, that was my though as well. It seems like what you need is something like: const objToArray = Object.entries const arrayToObj = (a) => a.reduce((a, [k, v]) => ((a[k]=v), a), {}) arrayToObj(objToArray({ foo: 'bar' })) // { foo: 'bar' }