SOURCE BELOW, and here as paste-bin snippet: http://pastebin.com/UEnQCTEe
Any function you can write in javascript, you can write in openSCAD. (Note, it might be hacky and inefficient, but you can do it.)
Here is my template for when I need to convert an imperative for-loop into a functional programming style supported by openSCAD function syntax.
data = recursiveLoop(0,9,[]);
echo(data); //Output==> ECHO:[0,1,2,3,4,5,6,7,8,9]
function recursiveLoop(os, num_pts, data_out)=
(
os <= num_pts
?
recursiveLoop_WORK_DETOUR(os, num_pts, data_out)
:
data_out
);
function recursiveLoop_WORK_DETOUR(os, num_pts, data_out)=
(
recursiveLoop
(
os+1, num_pts, ( concat(data_out,os) )
)
);