CallStream : project Euler

I am currently in the stage of searching for simple but valid examples of CallStream validity (aka: usefulness) as interfacing concept and programming idiom.

And, I think I have found one. The mathematical nature of some computational problems make them a great fit for the functional paradigm. For those of you who don’t know, “Project Euler is a series of challenging mathematical/computer programming problems…” ( I have concluded I could use the simple and interesting solution for a problem from “Project Euler” to show CallStream in action.

It seems to me, that the problem no. 1, contains quite enough opportunities to play with it, in the context of an CallStream usage example. Without “much ado about nothing”, I shall present my classical solution, without using CallStream:

I have decided to solve a problem with one “building block” function, and one expression using it. sum_n(), above returns a single number, that is a sum of natural numbers in range [div .. max], where each number is multiplier of a value div.  In which case this expression :

Gives the final result of the solution to the problem no. 1? No. The above is not a correct solution and you dear reader are sleeping on the job! Now … wake up and think: 3 x 5 = 15. So? So in the final expression above, 15 is “calculated in” twice, for 3 and for 5. Here is the proper final expression that yields the correct result.

After solution comes optimization

Strep 1 to the “final” solution is basically solved. But. Beside algorithmically, optimizations are achieved on the level of the language and/or runtime environment. For example one could safely predict that running each sum_n() call on a separate thread would speed up things. Or one could extend the solution so that more than two divisors are involved under one limiter :

Of course do not loose the fact that composable programming solutions are reusable and thus applicable to wide range of simillar programs. Like this one is. We are composing the finals solution from building block function. This is very different to writing a single function or program for each variant of the same problem.

Here is the JSFiddle source so far.

Why not stop here?

Answer 1: performance

Looking at above, it is obvious that parallel processing might be crucial, in improving performance. For that to happen, an correct mechanism (not algorithm) is found, selected and implemented.

Answer 2: change management

Whoever has spent some time developing the code for a customer, knows it is obvious that, requirements do change suddenly and drastically and good programming solution must be *also* resilient to change, to make improvements possible and feasible.

Change to the program is an improvement only and only if it is both possible and feasible.

Never loose this very simple but often overlooked wisdom. If it is not satisfied your change is not an improvement. This is why architectural patterns like CallStream are important. To make the inevitable changes achievable in a feasible manner. Not always, but most of the time.

CallStream, at last

I shall use a simple form of CallStream to create a better solution that will be resilient to change, but in the same time non-intrusive. Which means: Not getting in the way of the solution of the original problem. This is very important quality for every architectural pattern to be adopted. Let us at last, dive into CallStream usage.

This is now more usable. And rather more elegant too. One can ‘stream’ in any number of call’s to compute sum_n(), and then show the result by using a callback. And of course, implementation might change in any direction without affecting the usage. We could, for example, implement sending input to server, in next version. Above is trivial to implement.

But why stop here?

We are basing our design on a CallStream idiom and our solution is thus ultimately changeable. And somehow strangely monadic in nature too. There is a real possibility, not just a hint, of being able to easily compose new solution form atomic particles of logic. Above initial “sketch”, I could extend to have something like “tool-bench”, for the whole expandable family of similar solutions to “Euler-ian” problems.

Here we have an euler_bench(), call stream based mediator, which saves results of solutions to two problems, and shows the current result when required. Argument to euler_bench(), is an id of the result data-set for managing the state of the solution, so that we can instantiate and use one or more euler_bench( id ), in the same time in the same scope.

For this last “release” to work, we would have to implement “behind” few non-trivial state management mechanisms. For example each solution function, like .e.g. sum_n(), would have to emit the result to separate “result stack”. One result-stack for one euler-ian solution. otherwise final expressions will not work, since they will use results from disparate solutions. And, as above, all this named result stacks will have to be clustered under one id, like “id01” in the example above. For this to work I will have the each solution function (aka: fundamental indivisible particle of logic) return an array of values so that calling mechanism can record its results properly associated with the worker who made it and its input parameters.

Also. Callbacks we send, can be used to control the behaviour of the solution “behind”. For example, we can have a callback that will erase the current result data-set, callback that will send the current result data-set to server, another one that can record the time passed, etc. But, all of these will not change the callers experience at all. If properly micro-designed CallStream based interfacing mechanism stays intact.

Onto the next page for more usage and helpfull thinking.