Live data from Hacker News

Folding Promises in JavaScript

codementor.io

1–10 of 48 posts

Re: Folding Promises in JavaScript

#2
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.

Re: Folding Promises in JavaScript

#4
The author mentions the library Bluebird, which I think is a fantastic library. The 'mapSeries' method it offers is also very useful when iterating over an array of values that need to be 'promisified' and mapped in the given order. You can even set 'concurrency' as an option, which puts a limit on the concurrent promises that can run (great for reducing API load).

Re: Folding Promises in JavaScript

#5
I can't quite understand the difference between endomorphism ("input and output of the transformer must be from the same category") and homomorphism ("structure preserving transformation. We always stay in the same category"). Can someone help?

Re: Folding Promises in JavaScript

#6
post #2

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.

The pair of functions form an isomorphism. You have these two laws:

    forall x. objToArray(arrayToObj(x)) == x
    forall x. arrayToObj(objToArray(x)) == x

Re: Folding Promises in JavaScript

#7
post #2

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.

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.

Re: Folding Promises in JavaScript

#8
>How can we make it better ? Let's start by removing the requirement for identity value to always be the promise.

I challenge the view that making the identity value being able to be something other than a Promise is 'making it better'. Pointless abstraction is one of my pet peeves in this industry. This looks like it has gone from a fairly straightforward, if kludgy, piece of code to something far more complex. Why not just:

  const listOfPromises = [...]

  const result = Promise.all(listOfPromises).then(results => {
    return results.reduce((acc, next) => acc + next)
  })


?

Re: Folding Promises in JavaScript

#9
post #2

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.

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…

Re: Folding Promises in JavaScript

#10

I can't quite understand the difference between endomorphism ("input and output of the transformer must be from the same category") and homomorphism ("structure preserving transformation. We always stay in the same category"). Can someone help?

I believe homomorphism is a subset of endomorphism.

So a function that turns an array into another array of different length would be endomorphic (since it maintains the same type), but not homomorphic since it has a different structure (a different set of keys).

Post reply on HN