On Fri, Dec 28, 2012 at 3:03 AM, Alex ("Tatumizer") <
tatu...@gmail.com> wrote:
> After a lot of head-scratching, I found the way to eliminate all boilerplate
> and syntactic noise, and eventually came up with completely new design -much
> simpler one, and much more powerful: just two functions and their
> combinations do the job in most practical cases. I renamed the concept to
> "circuit" - it's a better name for this device.
>
>
https://github.com/tatumizer/circuit/blob/master/README.md
I've quickly read through your proposal (and the other comments to
this thread), and tried to understand the context, the problem and the
solution:
* all in all I tend to agree that asynchronous callbacks tend to
get messy especially when they are a lot of them chained together;
* but I also tend to think that most of the time the problem is in
the code itself and not in the language / environment (i.e. NodeJS),
as with a little bit of refactoring things could look better again;
But on the topic of your proposal, I see it more as a DSL (Domain
Specific Language) than as a library (or framework) to solve
asynchronous composition. (This stems from the fact that your current
solution for JavaScript is based on source code inspection, or
explicitly through crafted strings at beginning of functions; although
I do understand that this is only one of the possible solutions.)
This DSL approach makes me think that maybe it isn't very
appropriate for a programming language lacking syntax flexibility. To
be more clearer: I would say that your approach could be a perfect
match for a language like Lisp (or Scheme) which has prime support for
syntax manipulation. It could also work for other languages like Ruby
(which also has a more "sugary" syntax), or even Python (by using
decorators and its AST library), but most likely it would be broken
with compiled languages (like C, Go, etc.)
As such I tend to see JavaScript as a challenging language to
implement your proposal.
What you are describing seems very much similar to a workflow
engine, that takes as input the recipes of the steps and then deduces
the order. Maybe a more explicit approach would be better, taking your
example and moving the "crafted" string outside the function body,
which would eliminate the function source code parsing while keeping
intact your idea:
~~~~
var circuit=new Circuit({
"->sum1" : function() { add(1, 2, this.setter.sum1) },
"->sum2" : function() { add(3, 4, this.setter.sum2) },
"sum1,sum2->result" : function() { add(this.sum1, this.sum2,
this.setter.result) },
"->" : "result" // this is the actual output of the workflow
})
circuit.run(printResult);
~~~~
(Of course the keys have to be unique, but you could label them
like this "step1:a,b->c", "step2:a,b->d".)
As an ending note, what I think the NodeJS community could be
doing instead (and maybe it has already done, and I didn't see it, as
I currently strained away from NodeJS) are the following:
* laying out "best practices" both for library developers and
users; (like the error argument as the first one for each callback,
etc.);
* providing a generic function composition library (in the line of
"underscore"), allowing things as functional composition but taking
into account the callback nature, i.e.:
~~~~
work(composeCallback(f, g))
~~~~
which should be equivalent to
~~~~
work(function(err, out) {
if (err!=nil) g(err, nil);
// continue with a call to f which has as callback g
f(out, g);
}
~~~~
Ciprian.