Hacker News new | ask | show | jobs
by egeozcan 3198 days 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.

4 comments

It'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.

EDIT: 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…
The pair of functions form an isomorphism. You have these two laws:

    forall x. objToArray(arrayToObj(x)) == x
    forall x. arrayToObj(objToArray(x)) == x
Yeah, 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' }