Re:Ask HN: What are some cool but obscure data structures yo...
by maest on Friday July 22, 2022 @05:46AM
(#32190260)
More generally, I believe this is a monad structure.
[ Parent
| on HN ]
Re:Ask HN: What are some cool but obscure data structures yo...
by pdpi on Friday July 22, 2022 @06:07AM
(#32190366)
Promises are not monads, for one simple reason: they're not referentially transparent. The whole point of OP's example is to double down and take advantage of that.
Re:Ask HN: What are some cool but obscure data structures yo...
by epolanski on Friday July 22, 2022 @09:36AM
(#32191735)
Referential transparency is a property of expressions, not type classes. Referential transparency is nowhere in the definition of a monad because a monad is obviously not an expression but a type class.
It is definitely true that Promise is a data type that _could_ admit a monad instance. It has:
- a data type M a which is Promise a
- a function pure with the signature a => M a, which is x => Promise.resolve(x)
- a bind function with the signature M a => a => M b => M b which is essentialy `then`
But a monad requires three properties to hold true: Right identity, Left identity and associativity. Right identity holds for every function `f: a => M b`, but left identity and associativity do not hold.
Re:Ask HN: What are some cool but obscure data structures yo...
by brewmarche on Friday July 22, 2022 @05:07PM
(#32197129)
Maybe comonads fit better