discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

What to call "variables"

J
jon
Wed, Sep 7, 2016 2:49 PM

I hope that examples like this, and nophead's, will make it into some
documentation, so that people do not have to reinvent the wheel.

Jon

On 9/7/2016 9:07 AM, Ronaldo wrote:

Counting ocurrences of some event was one of the first astonishing task stuck
me in my first steps in OpenSCAD when I found that variables are immutable.
I finally arrived to this non recursive solution:

len( [ for(element=list) if(event_predicate(element)) 1] )

For the problem of finding the first ocurrence of an event, you always can
do:

[ for(element=list) if(event_predicate(element)) element ][0]

Another surprising task was to add up the elements of a list. Now I have a
bunch of solutions to that.
Besides given better name to what is called now variables, it is important
to ilustrate the techniques for common tasks like those in the manual.

I hope that examples like this, and nophead's, will make it into some documentation, so that people do not have to reinvent the wheel. Jon On 9/7/2016 9:07 AM, Ronaldo wrote: > Counting ocurrences of some event was one of the first astonishing task stuck > me in my first steps in OpenSCAD when I found that variables are immutable. > I finally arrived to this non recursive solution: > > len( [ for(element=list) if(event_predicate(element)) 1] ) > > For the problem of finding the first ocurrence of an event, you always can > do: > > [ for(element=list) if(event_predicate(element)) element ][0] > > Another surprising task was to add up the elements of a list. Now I have a > bunch of solutions to that. > Besides given better name to what is called now variables, it is important > to ilustrate the techniques for common tasks like those in the manual. > >
MS
Mark Schafer
Wed, Sep 7, 2016 9:42 PM

+1 for parameters.

please also consider that  - besides in loops - its useful to have
changeable variables for those of us who want to use random sequences
with seeds for procedural variations in the design.

This is hard to control right now.

+1 for parameters. please also consider that - besides in loops - its useful to have changeable variables for those of us who want to use random sequences with seeds for procedural variations in the design. This is hard to control right now.
RU
Richard Urwin
Wed, Sep 7, 2016 10:50 PM

Neon22 wrote

please also consider that  - besides in loops - its useful to have
changeable variables for those of us who want to use random sequences
with seeds for procedural variations in the design.

I don't believe that will help you.

The language is defined such that every procedure call is determinate.
Therefore the result of calling your Random() function can be assumed to be
constant and so it only needs to be called once. Even if it does get called
every time you want it to be, you  can never predict the order in which each
instance is called.

It's messy, but it can be done. Here's one way.

  1. create a file containing a million random numbers and read them into your
    open SCAD program.
  2. calculate how many random numbers every one of your functions will
    consume. Or, in the difficult cases, the maximum number. Probably make
    variables to keep that information.
  3. Give each function an argument specifying the base index to use for its
    random numbers.
  4. Your Random function has a single argument giving the index into the
    million numbers that it should use.

So for example if B() used two numbers and C() used three, then A(), which
called B and C would use five. It would call B with a base index the same as
the base index it was given, and then it would call C with the base index
plus two.

--
View this message in context: http://forum.openscad.org/What-to-call-variables-tp18260p18344.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Neon22 wrote > please also consider that - besides in loops - its useful to have > changeable variables for those of us who want to use random sequences > with seeds for procedural variations in the design. I don't believe that will help you. The language is defined such that every procedure call is determinate. Therefore the result of calling your Random() function can be assumed to be constant and so it only needs to be called once. Even if it does get called every time you want it to be, you can never predict the order in which each instance is called. It's messy, but it can be done. Here's one way. 1. create a file containing a million random numbers and read them into your open SCAD program. 2. calculate how many random numbers every one of your functions will consume. Or, in the difficult cases, the maximum number. Probably make variables to keep that information. 3. Give each function an argument specifying the base index to use for its random numbers. 4. Your Random function has a single argument giving the index into the million numbers that it should use. So for example if B() used two numbers and C() used three, then A(), which called B and C would use five. It would call B with a base index the same as the base index it was given, and then it would call C with the base index plus two. -- View this message in context: http://forum.openscad.org/What-to-call-variables-tp18260p18344.html Sent from the OpenSCAD mailing list archive at Nabble.com.
O
OzAtMichael
Thu, Sep 8, 2016 1:33 AM

How about for starters, we deprecate the ability to reassign. In the GUI, so command line -D is OK.
That will make it clear that such usage is not normal.

How about for starters, we deprecate the ability to reassign. In the GUI, so command line -D is OK. That will make it clear that such usage is not normal.