Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing a string in OpenSCAD

In OpenSCAD, I'd like to create a string, pattern, that starts with 'a', ends with 'c', and has enough 'b's in the middle that len(pattern) is equal to units. For example, if units is 5, pattern should be "abbbc".

The best solution I have so far is a list comprehension:

pattern = [if (units > 0) "a",
           if (units > 2) for (i = [1:units - 2]) "b",
           if (units > 1) "c"];

This will do, but ideally I'd like to make a string from this list? I tried str(pattern), but that gives a string representation of the list, with brackets, quotes, and commas.

Is there an alternative to the list comprehension that naturally results in a string?

like image 887
Adrian McCarthy Avatar asked Mar 02 '26 18:03

Adrian McCarthy


1 Answers

My solution to this was a following recursive function:

// Joins strings with an optional separator and limit
// 
// join(["A", "B", "C"]) -> "ABC"
// join(["A", "B", "C"], ",") -> "A,B,C"
// join(["A", "B", "C"], ",", 2) -> "A,B"
//
function join(parts, sep="", limit = undef) =
  let(n = is_undef(limit) ? len(parts) : limit)
    n == 0 ? "" :
    n == 1 ? parts[n-1] : 
    str(join(parts, sep, n-1), sep, parts[n-1]);

Or one adapted from solution proposed by @T.P. in a comment here: Constructing a string in OpenSCAD is simpler and tail-recursive so can be optimized by compiler:

function join(v, s = "", i = 0) = 
  i >= len(v) ? s : join(v, str(s, v[i]), i+1);

Then there's O(log(n)) solution here: https://github.com/thehans/funcutils/blob/master/string.scad#L3

like image 50
Marcin Raczkowski Avatar answered Mar 05 '26 13:03

Marcin Raczkowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!