Feature request: Vector append #201

Closed
maurerpe opened this Issue Sep 26, 2012 · 7 comments

Comments

Projects
None yet
6 participants

I was hoping for an append function in openscad. It would take 2 (or more?) arguments and return a vector that has all input vectors appended. If one of the arguments is not a vector, append should either return undef or throw an error.

append( [ 1, 2, 3 ], [ 4, 5, 6 ] ); -> [ 1, 2, 3, 4, 5, 6 ];
append( [ [ 12, -10 ], 5 ], [ [ 2, 9 ] ] ); -> [ [ 12, -10 ], 5, [ 2, 9 ] ];
append( [], [ 1, 2, 3 ] ) -> [ 1, 2, 3 ];

Is there a way to do this that I am missing? I didn't see anything in the manual.

dsoodak commented Dec 31, 2012

I've been looking for the same function. I believe it would be extremely useful for more complex objects.

Contributor

GilesBathgate commented Dec 31, 2012

This could also be implemented as an append operator:

http://www.gilesbathgate.com/2011/09/rapcad-dynamic-arrays/

ZhekaS commented Apr 8, 2013

Yes. Even more rudimentary array manipulation could help - one time creation of array of given size and modifying array elements. Well, the dynamic resizing will be nice to have. I was really surprised and disappointed while trying to write a simple module for parametric polygon creation. Now it seems just impossible.

Owner

kintel commented Apr 11, 2013

See this page in the wiki for an attempt to collect ideas related to this: https://github.com/openscad/openscad/wiki/List-and-Array-operations

I'd suggest concatenate may be a better term than append. Simple v=concat(v1,v2) is the basic building block, along with recursion, then many possibilities open up.
e.g. the str() function can be the equivalent limited to string vectors, allows things like
sub-string function subs(string,s=0,l) = ( s==len(string) || l==0 ? "" : str(string[s],subs(string,s+1,l-1))
echo("subs('abcdefgh',3,2)=", subs("abcdefgh",3,2)); // = "de"

All my LISP is flashing back...

Owner

kintel commented Jun 24, 2013

Yes, a functional paradigm appears to be the prevalent direction proposed by this (and other) discussions.

Well what do you know :)

function vcat(v1,v2) = [v1, v2];

i=[45,65,75];
j=[85,95,96];
k=vcat(i,j);
echo("len(k)=",len(k),"k=",k);
echo("k[0]=",k[0]);
echo("k[0][0]=",k[0][0]);

echo("a","d");
echo([0,1,2]+[3,4,5]);
echo([0,1,2,3]*2);

ECHO: "len(k)=", 2, "k=", [[45, 65, 75], [85, 95, 96]]
ECHO: "k[0]=", [45, 65, 75]
ECHO: "k[0][0]=", 45

edit/ Damn, not a proper concat...jumped to conclusion.../edit
edit/ looking at a double recursion... ;) /edit

@t-paul t-paul added a commit to t-paul/openscad that referenced this issue Dec 26, 2013

@t-paul t-paul Add concat() builtin (fixes #201).
This allows to concatenate multiple vectors and single elements into a
single result vector.
2f6d852

kintel closed this in 6443df3 Jan 3, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment