You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simplify indented output rendering by using stateful output routines.
This replaces the "pure functional" output style, which took great pains to
build a nested list structure which was only sent to the output device at the
very end. This required passing around a hidden "tab" parameter in a monadic
fashion, the use of a "next" continuation parameter, and semicolons to ensure
that the tail of a structure was threaded in properly.
Now when you call routines like say, nl, put, and indent, the effects of those
calls are going directly into a file handle or other target as they run. You
are no longer building up a lazy nested piece of data which is _later_ sent to
an output target. All the complexity of the "pure" method vanishes like a bad
dream.
This drastically simplifies the code supporting the b15 and index_C tests.
I also use the technique to great advantage in the demo.ray project:
https://github.com/chkoreff/demo.ray
This illustrates a principle which I title:
"Pure Functions Sometimes Considered Harmful"
The pure functional style still has its place in certain aspects of a program,
but for input and output it's easier just to cut to the chase and deal with the
machine on its own terms. In those cases, "var" is your friend -- and it can
even create more opportunities for implementing routines directly in C, as I
did with the parsing primitives in stream.c in the Fexl project.
I am also finding the var technique useful for "throughput". In one major
project I was passing a single immutable state parameter around everywhere,
and I greatly simplified the code by keeping that state in a handful of vars.
The good thing about "var" is that it is _controlled_ mutability, not the
haphazard and dangerous kind where you modify nested data structures in place.
In Fexl the data structures are still immutable, but you use a var to _point_
to a new version when you make a state change. I think this strikes the right
balance between pure functions and routines with side effects.
0 commit comments