[OpenSCAD] Programming in Functional OpenSCAD

Ronaldo Persiano rcmpersiano at gmail.com
Fri Jan 26 18:21:17 EST 2018


There is no information hiding resource in OpenSCAD. However, the coder can
avoid to mix lower and higher level operations by using proper atomic
functions for creation, inspecting and information retrieval of data nodes
virtually isolating its data representation from data structure operations.
I would not call those atomic functions as helper functions.

Nate's treefindvalue() function for instance might have the following much
more readable form:

function is_in_tree(tree, value) =
let( Lvalue = Lvalue(tree),
Rvalue = Rvalue(tree),
left = Lbranch(tree),
right = Rbranch(tree) )
is_2node(tree)?
( value==Lvalue )
|| ( (value<Lvalue) && is_in_tree(left, value) )
|| is_in_tree(right, value)
:is_3node(tree)?
(value==Lvalue)
|| ( value==Rvalue )
|| ( (value<Lvalue) && is_in_tree(left, value) )
|| ( (value>Rvalue) && is_in_tree(right, value) )
|| is_in_tree(Cbranch(tree), value)
:is_2leaf(tree)?
( value==Lvalue )
|| ( value==Rvalue )
:is_leaf(tree) && (value==Lvalue)
;


where a bunch of data representation access functions are called. To write
and read this code we don't need know where the left branch Lbranch() is
stored or how the test is_2node(tree) is performed. The code of is_in_tree
may be a bit less efficient than Nate's one but readability,
maintainability are prime qualities.

The algorithms for value insertion and removal from a 2-3 tree require the
manipulation of temporary abnormal nodes storing 3 values. So, the node
data representation should be able to store that kind of nodes. A set of
functions for creation, testing and value retrieval of temporary nodes may
be:

// temporary nodes
function tnode(v1,v2,v3, t1,t2,t3) = [t1, v1, t2, v2, t3, v3]; // creation
function is_temp(t) = (len(t)==6); // testing
function Tvalue(tree,ind) = t[2*ind+1]; // retrieval
function Tbranch(tree,ind) = t[2*ind];


By calling those functions, the value insertion coding will be easier and
the code clearer.

2-3 tree is a data abstraction for the user code. The node might be
regarded as a data abstraction for the 2-3 tree data structure.


2018-01-26 17:05 GMT-02:00 doug moen <doug at moens.org>:

> As Ronaldo says, "OpenSCAD is not a programming language". It's missing
> features that would make this kind of programming easier and more
> comfortable. I don't like calling OpenSCAD a functional programming
> language, because it's missing many of the features that you expect in a
> functional language, such as function values.
>
> OpenSCAD uses reference counting for memory management.
>
>
> You wrote:
>
>    :(1==len(tree))?
>       (value==tree[0])?
>          true
>       :
>          false
>
> You could make the code shorter by writing
>
>    :(1==len(tree))?
>       value==tree[0]
>
> I don't claim this is easier to understand, but it it is shorter.
>
>
> This kind of code is hard to read, because of all of the "magic number"
> indexes:
>
> [treenode[0],treenode[1],[treenode[2][0],treenode[2][1],treenode[2][2]],treenode[2][3],[treenode[2][4],treenode[3],treenode[4][1]]]
>
> It would be better to use names, instead of numbers. Ronaldo suggests
> using helper functions. I tend to use named constants as indexes into lists
> that actually structures/records, kind of like this:
>
> BRANCH1 = 0;
> VALUE1 = 1;
> BRANCH2 = 2;
> VALUE2 = 3;
> BRANCH3 = 4;
>
> [ treenode[BRANCH1],
>   treenode[VALUE1],
>   [ treenode[BRANCH2][BRANCH1], treenode[BRANCH2][VALUE1],
> treenode[BRANCH2][BRANCH2] ],
>   treenode[BRANCH2][VALUE2],
>   [ treenode[BRANCH2][BRANCH3], treenode[VALUE2],
> treenode[BRANCH3][VALUE1] ]
> ]
>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20180126/bee07eb3/attachment-0002.html>


More information about the Discuss mailing list