Cool, so if that's all we should just use those. But wait, what about those dozens of megabytes of data, should we really zip/transpose/concat them? We should use non-strict evaluation then...
No thanks, that's even worse. It's honest, but that doesn't make it a good idea. I like to do one thing at a time, not jump around like a wild horse. Turns out optimizing for not jumping around too much also makes programs simpler overall.
I've recently cleaned up some code of this kind. Defines objects with unsuitable lifetime and tries to be smart about reusing work instead of planning out in which order to do things in the first place. While the actual task was not difficult, the code was incomprehensible, to state it in a diplomatic way.
And if you think consciously about the intermediate data you probably can use it for a whole lot of other things!
In Haskell, unexpected memory expansions and duplication of work can happen. And these problems are not nice to debug.
I might be generalizing too much, though. Maybe you're talking about short-lived web applications that never see a large amount of data, or something like that.
Any code examples?
What I was thinking of is hand-rolling
for (a; b; c;)
BODY
to a;
loop:
if (!b)
goto out;
BODY
c;
goto loop;
out:
...For small data (MB) the compiler or library can parallelize things nicely. Bigger data (TB+) can be chewed threw by using MPI aware libraries that optimize and schedule and everything. Database work is already declarative because SQL is just so awesome. C# and java now handle fusion and streams more nicely and c++ can in-line and r-value a lot of overhead away.
In most cases, it shouldn't matter and it's much more important to think about the data accesses, due to the relative slowness of memory.
foreach(i in list){
if i == 0 { class.a() }
else { class.b() }
}
The weird thing about your pattern is that class.a() and class.b() may have side effects and don’t seem to depend on i at all. If I were to optimize this loop or even make it parallel, then I’m certainly missing some information. Some information that could be captured by another expression.
I’ve periodically done microbenchmarks of inner loops over the years and the various gcc outputs of the c++ loop functions are always performing slightly better than if I hand rolled. Perhaps with gcc7 or clang this is different.