Live data from Hacker News

Callbacks are imperative, promises are functional

blog.jcoglan.com

121–130 of 154 posts

Re: Callbacks are imperative, promises are functional

#121

Earlier quoted context omitted.

I'm not saying his design sense is wrong. I'm saying his characterization of promises as functional is so close to being right that it's the worst kind of wrongness. It's worse to say, "The capital of Kenya is Nairobi Central" than to say "The capital of Kenya is London." It is worse because errors that are only slightly wrong and maybe even partially correct are more deceptive, durable, and confusing. A promise is n…

Okay, if you require an appeal to authority, here is Oleg Kiselyov deriving Iteratees in Haskell, a more general formalization of "futures"/"promises": http://okmij.org/ftp/Streams.html If you insist on setting up semantic games: if Oleg hacking Haskell isn't functional programming then nothing meaningfully is. ---- > Functions return immutable values. Declarations make promises. Under the hood there may be similarit…

The distinction between functional and imperative is precisely a semantic game and nothing else.

Semantic games are meaningful or this blog post wouldn't be on HN. The title of the post makes it clear that we're playing a semantic game.

Kiselyov is deconstructing the constructs. That's fine. He's playing the semantic game of showing how different concepts share ontology. This doesn't make the concepts identical; it makes their deconstructions identical. These paradigms are not physical things they are conceptual abstractions. Anyway I'm obviously not getting through to you.

const is not a promise, it's a constant. Different concepts, similar ontology.

Re: Callbacks are imperative, promises are functional

#122

Earlier quoted context omitted.

Okay, if you require an appeal to authority, here is Oleg Kiselyov deriving Iteratees in Haskell, a more general formalization of "futures"/"promises": http://okmij.org/ftp/Streams.html If you insist on setting up semantic games: if Oleg hacking Haskell isn't functional programming then nothing meaningfully is. ---- > Functions return immutable values. Declarations make promises. Under the hood there may be similarit…

The distinction between functional and imperative is precisely a semantic game and nothing else. Semantic games are meaningful or this blog post wouldn't be on HN. The title of the post makes it clear that we're playing a semantic game. Kiselyov is deconstructing the constructs. That's fine. He's playing the semantic game of showing how different concepts share ontology. This doesn't make the concepts identical; it m…

I'm not sure we have the necessary language in common to communicate here. Functional programming is not just about stateless pure functions, it's also about composing computations. I'm curious if you've ever written in a natively lazy functional language like Haskell, or encountered `delay`/`force` promises in Scheme. The `const` I'm referring to is not the `const` of C et al: (http://hackage.haskell.org/packages/archive/base/latest/doc/...) and is just an example.

A "promise" is a delayed computation. It's a stand-in for a value, and the computation referencing it will suspend its execution until a value is available for it to consume. Similarly with Iteratees. They encapsulate specific patterns for building computations out of other computations.

What makes the "promise" pattern relatively less imperative than threading callbacks is that it abstracts out a specific pattern for ordering a computation, so that it doesn't need to be restated every time.

What makes the "promise" pattern relatively less declarative than "purely declarative" is that the control of the computation can be specified in the language itself. It's just abstracted out to the pattern in common. Similarly to re-writing certain stateful `for` loops with `map`.

Promises are a pattern in the functional programming paradigm. The "functional programming community" is where the construct comes from, and using them lets you write code that is closer to the declarative ideals of functional programming than the article's specified alternative, threading callbacks, which has relatively more in common with the explicit control of the order of computation that is implied by imperative programming.

I don't see anything in the article that remotely justifies "It's not functional at all. This reaffirms my belief that blog posts are a terrible place to learn. People who know the least shout the loudest.". In fact, I don't see anything wrong with it at all, or anything that substantially disagrees with what I've said or what I've seen any of the type of (yet-unspecified) authorities of certified expertise that you've appealed to have said. Yes, Promises are relatively more declarative, but they're also relatively more functional, relatively less imperative, and I don't see any basis for calling somebody an idiot for not using those terms with total mutual exclusion.

So please, raise, any specific objections you have which I have not addressed, or else have the honesty to retract the undeserved insults you've thrown at the author of this article.

Re: Callbacks are imperative, promises are functional

#123
post #110
post #76

Earlier quoted context omitted.

do f1 Or, if you prefer a list do fs And that seems to be one small example of why you may have already invented monads. I've been loving the impact of Javascript—modify and immediately see it on the browser—but every time I'm not using Haskell I miss it dearly.

Have to admit that has also been my reaction to some of these javascript async frameworks based on promises or deferreds. Congratulations, you've reimplemented a quirky ad-hoc variant on the continuation and error monads. Perhaps people don't spot the link as easily because monads are usually explained in terms of a type system, and javascript is untyped? (Or perhaps just because Monad is a very abstract abstraction…

Regarding your latter point, I don't think monads are even that abstract. "Monad" just happens to be something out of category theory so it has a mathematical weird-sounding name that you MIGHT guess has something to do with monoids -- if you know what monoids are -- so people think it has to be something complicated when in practice it's just a nice unified interface for glue code.

Re: Callbacks are imperative, promises are functional

#124
post #119
post #76

Earlier quoted context omitted.

do f1 Or, if you prefer a list do fs And that seems to be one small example of why you may have already invented monads. I've been loving the impact of Javascript—modify and immediately see it on the browser—but every time I'm not using Haskell I miss it dearly.

Doesn't that have the problem that it won't get around to computing the ratio until it needs to be printed to the screen?

Depends on the semantics of the monad. If you want to control that kind of thing, you can use Strategies from Control.Concurrent. If you just want to force things, then abstract-par [1] and monad-par [2] have some pretty convenient semantics.

[1] http://hackage.haskell.org/package/abstract-par/ [2] http://hackage.haskell.org/package/monad-par/

Re: Callbacks are imperative, promises are functional

#125
post #13

I feel this is twisting the meaning of functional programming. Excel is not functional. It is declarative. You declare the relationships between the cells and Excel uses those to propagate changes. Just like a makefile is not functional but declarative. The dependency of the relationships are enforced to produce action. SQL is another example of declarative language and it is nowhere near as functional.

declarative is an orthogonal attribute to functional. The two attributes are not mutually exclusive. You may as well say something like: "A bicycle isn't a vehicle at all! A Bicycle is a metallic object!"

Imo declarative, functional and OO are somewhat but not really orthogonal concepts.

A mathematical (ie pure) function is declarative in nature as it defines a relation between sets. However, in practice you often use it imperatively, ie as a means to get an output from some input.

An object is imperative in nature as it encapsulates mutable state. However, the set of messages defines an abstract interface which is arguably declarative.

In principle, a purely functional language cannot be object-oriented, and a purely object-oriented language cannot be functional. In practice, this doesn't matter as pure languages are rare.

Re: Callbacks are imperative, promises are functional

#126

Earlier quoted context omitted.

I can speak your comment, since my side project is a Node.js server that talks to several APIs, a database, and N web clients. Like you, I've worked more than a year on it, but I had a positive experience with Q [1] and jQuery promises. Promises make async code easy to manage, even at scale. Each API request gets its own promise. What happens inside that promise doesn't matter, as long as it returns a result or an er…

In other words, promises allow us to separate concerns. Document retrieval is one concern, collation another. Other programming languages have this too. They're called a 'METHOD'. Sorry, couldn't resist. On a serious note, look at your code in here: https://github.com/fruchtose/muxamp/blob/master/lib/playlist... And look at your 'playlistCount' function on line 39 (which for no apparent reason you've made a variable)…

Some pseudocode

    playlistCount :: PromiseDB Int
    playlistCount = withConn $ \conn -> do
                      result 
Wrapping the pool handling into withConn, and the failure modes into query. This PromiseDB monad would be fairly trivial to produce in Haskell. It's also fairly easy to write it point-free as

    withConn $ 
      query 'SELECT COUNT(id) AS count FROM Playlists;' 
      >=> return . parseInt . get 'count'

Re: Callbacks are imperative, promises are functional

#127
post #98

Earlier quoted context omitted.

What kind of object is in your list of async operations? promises. (though probably your own ad hoc, hand rolled and poorly specified version of them)

Just plain-old native functions - that's the whole point.

when you put "plain old native functions" in an array, with the intent of executing them in sequence, with the output of i being fed into the input of i+1, congratulations, the functions are now implicitly promises.

Because, in the end, what, semantically, is the difference between:

runqueue([func1,func2,func3,func4]); and func1().then(func2).then(func3).then(func4);

No significant difference at all, really. except the promises permit you much more flexibility and options.

Re: Callbacks are imperative, promises are functional

#128

This is an interesting perspective. But to me, even having spent a year on a large node.js project, I just don't see how promises would have simplified things at all. If you have some crazy graph of dependencies, I can see how breaking out promises could help simplify things. But I don't feel like that's a super-common scenario. The author says: > * [Promises] are easier to think about precisely because we’ve delegat…

Twitter's finagle is a good example.

Anytime you need to perform more than one interaction with external services in parallel, it's a lot easier to wrap the results as futures and interact with the promise objects than to cope with callback spaghetti.

This is particularly critical anytime you're working in an SOA environment.

I could see why it's easy to believe that you don't need promises if you're averaging 1-2 database queries and 0 API calls per web page/API call reply, but once your scenario gets even slightly more complex - you're fucked.

Promises are one of the big reasons I like Clojure's concurrency better than Go's.

Re: Callbacks are imperative, promises are functional

#129

Earlier quoted context omitted.

The distinction between functional and imperative is precisely a semantic game and nothing else. Semantic games are meaningful or this blog post wouldn't be on HN. The title of the post makes it clear that we're playing a semantic game. Kiselyov is deconstructing the constructs. That's fine. He's playing the semantic game of showing how different concepts share ontology. This doesn't make the concepts identical; it m…

I'm not sure we have the necessary language in common to communicate here. Functional programming is not just about stateless pure functions, it's also about composing computations. I'm curious if you've ever written in a natively lazy functional language like Haskell, or encountered `delay`/`force` promises in Scheme. The `const` I'm referring to is not the `const` of C et al: ( http://hackage.haskell.org/packages/a…

You're defining a promise in terms of what is "under the hood." You actually define promise in terms of what-the-hardware-does. This demonstrates the varying levels of abstraction that we're operating at.

You define promise:

>A "promise" is a delayed computation. It's a stand-in for a value, and the computation referencing it will suspend its execution until a value is available for it to consume.

I define promise:

>A "promise" is a declaration or assurance that one will do a particular thing or that guarantees that a particular thing will happen.

My definition of promise is conceptually and formally more accurate than yours, because your definition raises issues like "what is a computation" and "what is a value" and "what is a stand-in" and "what is execution" and "what does consume mean"

As programmers, we understand what these words mean concretely in terms of lower-level abstractions or in some cases actual hardware operation.

My point is that the declarative ideal that you reference is not defined in terms of lower level programmer abstractions, but rather in terms of natural conceptual metaphors.

Since functional programming is generally seen as a subset of declarative programming, it's natural that functional programmers use declarative concepts and it's even understandable that they call those concepts functional. But they're mixing metaphors and creating conceptual confusion. They're losing track of the original metaphors that draw hair-splitting analytic distinctions between concepts in order to create clarity. It's semantics.

Like I already said, I'm not criticizing his design sense and I'm not criticizing yours. I'm criticizing his and your use of language and I'm asserting that your language is degraded and impure.

To me, aside from the design advice he gives, his blog post is about drawing semantic distinctions. I just find the way he does this to be horribly flawed in a way is "so close to being right that it's the worst kind of wrong."

Similarly, I would describe your definition of promise in this way. So close to being right that it's the worst kind of wrong. Your definition of promise is not a definition; it's a deconstruction. It's actually an imperative deconstruction--you're describing the promise using imperative language--concepts like execution and computation.

HN is full of very "practical" people who have a view that goes something like, "I'm correct enough for this to WORK, I'm correct enough to get the correct answer, therefore I'm correct." It's a reductionist view of truth; that which I can deconstruct, I understand. I don't share this view.

I would say, you ARE correct, but you're not as correct as you could be. Likewise with the author.

When I want to learn about the distinction between declarative paradigms and functional paradigms I talk to people who specialize in drawing that distinction. Since the blog post is prima facie drawing a distinction between functional and imperative, the value I'm looking for is an analytically rigorous distinction between abstract concepts.

That's not what I found, so I'm critical. Sorry. This just isn't the author's area of expertise and it shows. Perhaps most of his readers don't care. More power to them. But if you want to engage in intellectual life you can't throw down the gauntlet every time someone criticizes you.

Re: Callbacks are imperative, promises are functional

#130

Earlier quoted context omitted.

I'm not sure we have the necessary language in common to communicate here. Functional programming is not just about stateless pure functions, it's also about composing computations. I'm curious if you've ever written in a natively lazy functional language like Haskell, or encountered `delay`/`force` promises in Scheme. The `const` I'm referring to is not the `const` of C et al: ( http://hackage.haskell.org/packages/a…

You're defining a promise in terms of what is "under the hood." You actually define promise in terms of what-the-hardware-does. This demonstrates the varying levels of abstraction that we're operating at. You define promise: >A "promise" is a delayed computation. It's a stand-in for a value, and the computation referencing it will suspend its execution until a value is available for it to consume. I define promise: >…

> When I want to learn about the distinction between declarative paradigms and functional paradigms I talk to people who specialize in drawing that distinction. Since the blog post is prima facie drawing a distinction between functional and imperative, the value I'm looking for is an analytically rigorous distinction between abstract concepts.

Who specializes in making that distinction? Point me in the right direction. I am absolutely throwing down the gauntlet, I don't believe for a second you know what you're talking about, and I suspect that your understanding of functional programming does not include the possibility of functions returning functions, and the term "thunk" would be lost on you. If you're going to put so much weight on expertise, please point me in the direction of the researcher who you suspect would take the author of this article to task for his terminology. Or make a positive argument yourself.

"Promise" here refers to a specific computation strategy, it has a well-documented intellectual heritage and has been defined as a lambda calculus. It has its basis in the structuring of computations. With how you've constructed this ontology, I don't see any room for "functional programming" to mean anything at all.

I suppose that does leave a lot of room for calling the OP "the village loudmouth" for having any actual content behind his words.

Post reply on HN