5 ms·
Promises are actually very simple - so fundamental that you can think of them as dual to function composition. By a dual to function composition i mean that you
by dustingetz 13y ago
Promises are actually very simple - so fundamental that you can think of them as dual to function composition. By a dual to function composition i mean that you can mechanically convert async promise code to everyday synchronous function calls - including everyday exceptions. That means that literally the code can look exactly the same whether or not you are async or sync. The trick is you need either macros or monads to do this.
(This is kind of the whole point of monads - you could write a piece of code by composing a few functions, and then make it asynchronous without breaking the function composition! No need to rewrite your code to use callbacks; the same code will just work.)
- gcr 13y agoIs this currently possible with NodeJS? Javascript has none of the following: - Macros - Monads - call/cc
- jonsterling 13y agoMonads are not a language feature, they are a library feature.
- gcr 13y agoOk, true. They're certainly much nicer to use if you have nice syntax for them though. :)
- dustingetz 13y agoOnly true in the most pedantic sense. For monads to work, all your code needs to be pure (in the FP sense). So if you want to do anything substantial with monads in an imperative language, you need to rewrite the entire ecosystem of libraries. That would cost quite a bit more money than your average organization can afford ;)
- jonsterling 13y agoAlso has nothing to do with the language, mate. Your argument does nothing but agree with my point :)
- jonsterling 13y agoAlso, you of course do not need purity to get monads to “work”. You can do them in SML or OCaml, for instance, which are not pure by a long shot; by the definition of “pure” which I employ, Haskell isn’t either. Lack of purity is a defect which bites you whether or not you have monads: so if you're perfectly happy dealing with it in your existing code base (like most of us are), making explicit the “monadic” patterns in your codebase will not make it worse, and may improve things a bit.
- coldtea 13y agoWell, on the other hand languages that DO have macros, monads, and call/cc built-in don't power the web and haven't even contributed to many essential software we use everyday, from our OSes to stuff like Chrome, Photoshop, LaTeX, etc. In fact, most of it was and is written in C/C++, horror of horrors, and fewer in Pascal / Java. The notable exceptions I can think of are Emacs (partly), Autocad (partly) and Pandoc. So I guess there's a tradeoff, use cool languages and not contribute much of usable software, or use some so-so language and create stuff people use.
- gcr 13y agoNitpicking: LaTeX is a bad example because it's bulit on TeX, which does have macros. :) But I don't want to go down the rabbit hole of saying that "TeX & Emacs are somehow more powerful than Chrome because their systems use macros." Granted, sure. People cope well with not being able to express their problems the way they want to. The existence of so much high-quality C/C++/blub software proves this fact. But for those of us who don't want to change our thinking, tools that fundamentally extend the language are great to have in our toolbox. Maybe it's our weakness, that we feel uncomfortable when we can't express the problem the way we want. Or maybe it's our strength. I don't know.
- coldtea 13y ago>Nitpicking: LaTeX is a bad example because it's bulit on TeX, which does have macros. :) True, had TeX itself in mind!
- deleted 13y ago[deleted]
- dustingetz 13y agoIf something this powerful was reasonable to do in javascript, it would be mainstream knowledge by now. Clojurescript's core.async is a macro approach which is not quite the same but very similar to what I describe. You could probably do it in sweet.js (marcos for javascript that are expanded at compile time) but as far as I know, nobody is actually doing it so there are probably showstoppers. A monadic approach requires all of your code to be pure-functional, which is unreasonable because you'd have to rewrite all of your dependencies to be pure.
- jonsterling 13y agoPromises are _not_ dual to function composition, unless you mean by “dual” something very different from the common mathematical meaning.