discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Functions literals / higher order functions

A
adrianv
Sat, Nov 9, 2019 3:15 PM

tp3 wrote

On 03.11.19 22:40, adrianv wrote:

foo(function(x) sin(x))

Yes, all built-in functions and also those defined in
the previously existing function name() = expression
syntax have their own special namespace and are no values
that can be passed around. To do that they need to be
wrapped like that.

Would it be worth adding a simpler mechanism for passing existing functions?
It seems like the existing scheme creates two independent namespaces for
functions and it's sort of clumsy to get between them.  In MATLAB, for
example, you can pass an existing function by writing "@sin".  I can
imagine having to write code where I generate a table of function
redefinitions to make it easy to pass pre-existing functions without the
need for all the verbosity of a function declaration embedded in the code
for every call.

What would be the disadvantage of writing all of my functions using the new
syntax.  Would there be any problems with this?  So  always write

funcname = function(...) definition....;

This seems like it could be desirable since you can do something with a
function literal defined this way that you can't do with a regular function.
And there is no apparent advantage to defining functions the "normal" way.
I mean, the one obvious limitation is that I no longer have a different
namespace for functions if I do this.

--
Sent from: http://forum.openscad.org/

tp3 wrote > On 03.11.19 22:40, adrianv wrote: >> foo(function(x) sin(x)) > > Yes, all built-in functions and also those defined in > the previously existing function name() = expression > syntax have their own special namespace and are no values > that can be passed around. To do that they need to be > wrapped like that. Would it be worth adding a simpler mechanism for passing existing functions? It seems like the existing scheme creates two independent namespaces for functions and it's sort of clumsy to get between them. In MATLAB, for example, you can pass an existing function by writing "@sin". I can imagine having to write code where I generate a table of function redefinitions to make it easy to pass pre-existing functions without the need for all the verbosity of a function declaration embedded in the code for every call. What would be the disadvantage of writing all of my functions using the new syntax. Would there be any problems with this? So always write funcname = function(...) definition....; This seems like it could be desirable since you can do something with a function literal defined this way that you can't do with a regular function. And there is no apparent advantage to defining functions the "normal" way. I mean, the one obvious limitation is that I no longer have a different namespace for functions if I do this. -- Sent from: http://forum.openscad.org/
NH
nop head
Sat, Nov 9, 2019 3:15 PM

The issue is the old format functions have their own namespace,
separate from variables and modules so you can have the same name it all
three namespaces and there is no ambiguity. However new style function
literals are anonymous until you assign them to a variable and then that
variable is in the variable namespace, not the function namespace.

On Sat, 9 Nov 2019 at 15:03, adrianv avm4@cornell.edu wrote:

tp3 wrote

On 03.11.19 22:40, adrianv wrote:

foo(function(x) sin(x))

Yes, all built-in functions and also those defined in
the previously existing function name() = expression
syntax have their own special namespace and are no values
that can be passed around. To do that they need to be
wrapped like that.

Would it be worth adding a simpler mechanism for passing existing
functions?
It seems like the existing scheme creates two independent namespaces for
functions and it's sort of clumsy to get between them.  In MATLAB, for
example, you can pass an existing function by writing "@sin".  I can
imagine having to write code where I generate a table of function
redefinitions to make it easy to pass pre-existing functions without the
need for all the verbosity of a function declaration embedded in the code
for every call.

What would be the disadvantage of writing all of my functions using the new
syntax.  Would there be any problems with this?  So  always write

funcname = function(...) definition....;

This seems like it could be desirable since you can do something with a
function literal defined this way that you can't do with a regular
function.
And there is no apparent advantage to defining functions the "normal"
way.
I mean, the one obvious limitation is that I no longer have a different
namespace for functions if I do this.

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

The issue is the old format functions have their own namespace, separate from variables and modules so you can have the same name it all three namespaces and there is no ambiguity. However new style function literals are anonymous until you assign them to a variable and then that variable is in the variable namespace, not the function namespace. On Sat, 9 Nov 2019 at 15:03, adrianv <avm4@cornell.edu> wrote: > tp3 wrote > > On 03.11.19 22:40, adrianv wrote: > >> foo(function(x) sin(x)) > > > > Yes, all built-in functions and also those defined in > > the previously existing function name() = expression > > syntax have their own special namespace and are no values > > that can be passed around. To do that they need to be > > wrapped like that. > > Would it be worth adding a simpler mechanism for passing existing > functions? > It seems like the existing scheme creates two independent namespaces for > functions and it's sort of clumsy to get between them. In MATLAB, for > example, you can pass an existing function by writing "@sin". I can > imagine having to write code where I generate a table of function > redefinitions to make it easy to pass pre-existing functions without the > need for all the verbosity of a function declaration embedded in the code > for every call. > > What would be the disadvantage of writing all of my functions using the new > syntax. Would there be any problems with this? So always write > > funcname = function(...) definition....; > > This seems like it could be desirable since you can do something with a > function literal defined this way that you can't do with a regular > function. > And there is no apparent advantage to defining functions the "normal" > way. > I mean, the one obvious limitation is that I no longer have a different > namespace for functions if I do this. > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
A
adrianv
Sat, Nov 9, 2019 3:33 PM

Yeah, I was aware of that.  But I don't think I'd really miss this.  In most
languages, functions don't get a separate namespace from variables, so it's
actually kind of an odd quirk of OpenSCAD.  I wonder if there are any other
issues I have overlooked, or relating to performance.

nophead wrote

The issue is the old format functions have their own namespace,
separate from variables and modules so you can have the same name it all
three namespaces and there is no ambiguity. However new style function
literals are anonymous until you assign them to a variable and then that
variable is in the variable namespace, not the function namespace.

On Sat, 9 Nov 2019 at 15:03, adrianv <

avm4@

> wrote:

I mean, the one obvious limitation is that I no longer have a different
namespace for functions if I do this.

Yeah, I was aware of that. But I don't think I'd really miss this. In most languages, functions don't get a separate namespace from variables, so it's actually kind of an odd quirk of OpenSCAD. I wonder if there are any other issues I have overlooked, or relating to performance. nophead wrote > The issue is the old format functions have their own namespace, > separate from variables and modules so you can have the same name it all > three namespaces and there is no ambiguity. However new style function > literals are anonymous until you assign them to a variable and then that > variable is in the variable namespace, not the function namespace. > > On Sat, 9 Nov 2019 at 15:03, adrianv &lt; > avm4@ > &gt; wrote: > > >> I mean, the one obvious limitation is that I no longer have a different >> namespace for functions if I do this. -- Sent from: http://forum.openscad.org/
TP
Torsten Paul
Sat, Nov 9, 2019 3:46 PM

On 09.11.19 16:15, adrianv wrote:

Would it be worth adding a simpler mechanism for passing
existing functions?

Yes, that certainly is open for discussion. It might even be
possible to bind the built-in functions to the respective
names in the variable namespace.

As that is one layer up from the top layer available to the
user, it would still be possible to just overwrite those names
in existing code.

I believe the only non-compatible change is that you can
still observe this by "if (is_undef(sin))" on top level.

It seems like the existing scheme creates two independent
namespaces for functions and it's sort of clumsy to get
between them.

The other way around. Those separate namespaces already exist
and cause the issues now. The problem is that first class
functions obviously need to be in the variable namespace as
they are really just values.

What would be the disadvantage of writing all of my functions
using the new syntax. Would there be any problems with this?
So always write

funcname = function(...) definition....;

Right now, the only disadvantage I'm aware of is that use<>
will not import them as they are variables, and use<> only
imports (old style) functions and modules.

That's something which needs to be changed of cause.

ciao,
Torsten.

On 09.11.19 16:15, adrianv wrote: > Would it be worth adding a simpler mechanism for passing > existing functions? Yes, that certainly is open for discussion. It might even be possible to bind the built-in functions to the respective names in the variable namespace. As that is one layer up from the top layer available to the user, it would still be possible to just overwrite those names in existing code. I believe the only non-compatible change is that you can still observe this by "if (is_undef(sin))" on top level. > It seems like the existing scheme creates two independent > namespaces for functions and it's sort of clumsy to get > between them. The other way around. Those separate namespaces already exist and cause the issues now. The problem is that first class functions obviously need to be in the variable namespace as they are really just values. > What would be the disadvantage of writing all of my functions > using the new syntax. Would there be any problems with this? > So always write > > funcname = function(...) definition....; Right now, the only disadvantage I'm aware of is that use<> will not import them as they are variables, and use<> only imports (old style) functions and modules. That's something which needs to be changed of cause. ciao, Torsten.
NH
nop head
Sat, Nov 9, 2019 3:51 PM

But I don't think I'd really miss this.

Maybe but people like me would as I have made extensive use of it so
changing it would break a lot of code.

On Sat, 9 Nov 2019 at 15:47, Torsten Paul Torsten.Paul@gmx.de wrote:

On 09.11.19 16:15, adrianv wrote:

Would it be worth adding a simpler mechanism for passing
existing functions?

Yes, that certainly is open for discussion. It might even be
possible to bind the built-in functions to the respective
names in the variable namespace.

As that is one layer up from the top layer available to the
user, it would still be possible to just overwrite those names
in existing code.

I believe the only non-compatible change is that you can
still observe this by "if (is_undef(sin))" on top level.

It seems like the existing scheme creates two independent
namespaces for functions and it's sort of clumsy to get
between them.

The other way around. Those separate namespaces already exist
and cause the issues now. The problem is that first class
functions obviously need to be in the variable namespace as
they are really just values.

What would be the disadvantage of writing all of my functions
using the new syntax. Would there be any problems with this?
So always write

funcname = function(...) definition....;

Right now, the only disadvantage I'm aware of is that use<>
will not import them as they are variables, and use<> only
imports (old style) functions and modules.

That's something which needs to be changed of cause.

ciao,
Torsten.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

> But I don't think I'd really miss this. Maybe but people like me would as I have made extensive use of it so changing it would break a lot of code. On Sat, 9 Nov 2019 at 15:47, Torsten Paul <Torsten.Paul@gmx.de> wrote: > On 09.11.19 16:15, adrianv wrote: > > Would it be worth adding a simpler mechanism for passing > > existing functions? > > Yes, that certainly is open for discussion. It might even be > possible to bind the built-in functions to the respective > names in the variable namespace. > > As that is one layer up from the top layer available to the > user, it would still be possible to just overwrite those names > in existing code. > > I believe the only non-compatible change is that you can > still observe this by "if (is_undef(sin))" on top level. > > > It seems like the existing scheme creates two independent > > namespaces for functions and it's sort of clumsy to get > > between them. > > The other way around. Those separate namespaces already exist > and cause the issues now. The problem is that first class > functions obviously need to be in the variable namespace as > they are really just values. > > > What would be the disadvantage of writing all of my functions > > using the new syntax. Would there be any problems with this? > > So always write > > > > funcname = function(...) definition....; > > Right now, the only disadvantage I'm aware of is that use<> > will not import them as they are variables, and use<> only > imports (old style) functions and modules. > > That's something which needs to be changed of cause. > > ciao, > Torsten. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
A
adrianv
Sat, Nov 9, 2019 6:01 PM

I wasn't suggesting that the old syntax be removed.  Rather, I was pondering
how to write code in the future.  It appears like with the new function
syntax that it might make sense to always use the new one.  In other words,
to treat the old syntax like a sort of old-style method and abandon it.

Out of curiosity, is there some particular use case for using variables with
the same name as functions, or you just find it convenient to make variables
named "square" and "cube" and so on?

nophead wrote

But I don't think I'd really miss this.

Maybe but people like me would as I have made extensive use of it so
changing it would break a lot of code.

On Sat, 9 Nov 2019 at 15:47, Torsten Paul <

Torsten.Paul@

> wrote:

On 09.11.19 16:15, adrianv wrote:

Would it be worth adding a simpler mechanism for passing
existing functions?

Yes, that certainly is open for discussion. It might even be
possible to bind the built-in functions to the respective
names in the variable namespace.

As that is one layer up from the top layer available to the
user, it would still be possible to just overwrite those names
in existing code.

I believe the only non-compatible change is that you can
still observe this by "if (is_undef(sin))" on top level.

It seems like the existing scheme creates two independent
namespaces for functions and it's sort of clumsy to get
between them.

The other way around. Those separate namespaces already exist
and cause the issues now. The problem is that first class
functions obviously need to be in the variable namespace as
they are really just values.

What would be the disadvantage of writing all of my functions
using the new syntax. Would there be any problems with this?
So always write

funcname = function(...) definition....;

Right now, the only disadvantage I'm aware of is that use<>
will not import them as they are variables, and use<> only
imports (old style) functions and modules.

That's something which needs to be changed of cause.

ciao,
Torsten.


OpenSCAD mailing list

Discuss@.openscad

Discuss@.openscad

I wasn't suggesting that the old syntax be removed. Rather, I was pondering how to write code in the future. It appears like with the new function syntax that it might make sense to always use the new one. In other words, to treat the old syntax like a sort of old-style method and abandon it. Out of curiosity, is there some particular use case for using variables with the same name as functions, or you just find it convenient to make variables named "square" and "cube" and so on? nophead wrote >> But I don't think I'd really miss this. > > Maybe but people like me would as I have made extensive use of it so > changing it would break a lot of code. > > On Sat, 9 Nov 2019 at 15:47, Torsten Paul &lt; > Torsten.Paul@ > &gt; wrote: > >> On 09.11.19 16:15, adrianv wrote: >> > Would it be worth adding a simpler mechanism for passing >> > existing functions? >> >> Yes, that certainly is open for discussion. It might even be >> possible to bind the built-in functions to the respective >> names in the variable namespace. >> >> As that is one layer up from the top layer available to the >> user, it would still be possible to just overwrite those names >> in existing code. >> >> I believe the only non-compatible change is that you can >> still observe this by "if (is_undef(sin))" on top level. >> >> > It seems like the existing scheme creates two independent >> > namespaces for functions and it's sort of clumsy to get >> > between them. >> >> The other way around. Those separate namespaces already exist >> and cause the issues now. The problem is that first class >> functions obviously need to be in the variable namespace as >> they are really just values. >> >> > What would be the disadvantage of writing all of my functions >> > using the new syntax. Would there be any problems with this? >> > So always write >> > >> > funcname = function(...) definition....; >> >> Right now, the only disadvantage I'm aware of is that use<> >> will not import them as they are variables, and use<> only >> imports (old style) functions and modules. >> >> That's something which needs to be changed of cause. >> >> ciao, >> Torsten. >> >> _______________________________________________ >> OpenSCAD mailing list >> > Discuss@.openscad >> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >> > > _______________________________________________ > OpenSCAD mailing list > Discuss@.openscad > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Sent from: http://forum.openscad.org/
NH
nop head
Sat, Nov 9, 2019 7:43 PM

I often have code like this:

screw = iec_screw(type);

...

screw(screw, screw_length);

screw is both a local variable and a global module. Because it is local it
isn't really appropriate to give it a longer name, like the_screw or
iec_screw. If it was iec_screw then again it is the same name as a
function. In other languages I would have to think up a different name.

On Sat, 9 Nov 2019 at 17:49, adrianv avm4@cornell.edu wrote:

I wasn't suggesting that the old syntax be removed.  Rather, I was
pondering
how to write code in the future.  It appears like with the new function
syntax that it might make sense to always use the new one.  In other words,
to treat the old syntax like a sort of old-style method and abandon it.

Out of curiosity, is there some particular use case for using variables
with
the same name as functions, or you just find it convenient to make
variables
named "square" and "cube" and so on?

nophead wrote

But I don't think I'd really miss this.

Maybe but people like me would as I have made extensive use of it so
changing it would break a lot of code.

On Sat, 9 Nov 2019 at 15:47, Torsten Paul <

Torsten.Paul@

> wrote:

On 09.11.19 16:15, adrianv wrote:

Would it be worth adding a simpler mechanism for passing
existing functions?

Yes, that certainly is open for discussion. It might even be
possible to bind the built-in functions to the respective
names in the variable namespace.

As that is one layer up from the top layer available to the
user, it would still be possible to just overwrite those names
in existing code.

I believe the only non-compatible change is that you can
still observe this by "if (is_undef(sin))" on top level.

It seems like the existing scheme creates two independent
namespaces for functions and it's sort of clumsy to get
between them.

The other way around. Those separate namespaces already exist
and cause the issues now. The problem is that first class
functions obviously need to be in the variable namespace as
they are really just values.

What would be the disadvantage of writing all of my functions
using the new syntax. Would there be any problems with this?
So always write

funcname = function(...) definition....;

Right now, the only disadvantage I'm aware of is that use<>
will not import them as they are variables, and use<> only
imports (old style) functions and modules.

That's something which needs to be changed of cause.

ciao,
Torsten.


OpenSCAD mailing list

Discuss@.openscad

Discuss@.openscad

I often have code like this: screw = iec_screw(type); ... screw(screw, screw_length); screw is both a local variable and a global module. Because it is local it isn't really appropriate to give it a longer name, like the_screw or iec_screw. If it was iec_screw then again it is the same name as a function. In other languages I would have to think up a different name. On Sat, 9 Nov 2019 at 17:49, adrianv <avm4@cornell.edu> wrote: > I wasn't suggesting that the old syntax be removed. Rather, I was > pondering > how to write code in the future. It appears like with the new function > syntax that it might make sense to always use the new one. In other words, > to treat the old syntax like a sort of old-style method and abandon it. > > Out of curiosity, is there some particular use case for using variables > with > the same name as functions, or you just find it convenient to make > variables > named "square" and "cube" and so on? > > > nophead wrote > >> But I don't think I'd really miss this. > > > > Maybe but people like me would as I have made extensive use of it so > > changing it would break a lot of code. > > > > On Sat, 9 Nov 2019 at 15:47, Torsten Paul &lt; > > > Torsten.Paul@ > > > &gt; wrote: > > > >> On 09.11.19 16:15, adrianv wrote: > >> > Would it be worth adding a simpler mechanism for passing > >> > existing functions? > >> > >> Yes, that certainly is open for discussion. It might even be > >> possible to bind the built-in functions to the respective > >> names in the variable namespace. > >> > >> As that is one layer up from the top layer available to the > >> user, it would still be possible to just overwrite those names > >> in existing code. > >> > >> I believe the only non-compatible change is that you can > >> still observe this by "if (is_undef(sin))" on top level. > >> > >> > It seems like the existing scheme creates two independent > >> > namespaces for functions and it's sort of clumsy to get > >> > between them. > >> > >> The other way around. Those separate namespaces already exist > >> and cause the issues now. The problem is that first class > >> functions obviously need to be in the variable namespace as > >> they are really just values. > >> > >> > What would be the disadvantage of writing all of my functions > >> > using the new syntax. Would there be any problems with this? > >> > So always write > >> > > >> > funcname = function(...) definition....; > >> > >> Right now, the only disadvantage I'm aware of is that use<> > >> will not import them as they are variables, and use<> only > >> imports (old style) functions and modules. > >> > >> That's something which needs to be changed of cause. > >> > >> ciao, > >> Torsten. > >> > >> _______________________________________________ > >> OpenSCAD mailing list > >> > > > Discuss@.openscad > > >> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >> > > > > _______________________________________________ > > OpenSCAD mailing list > > > Discuss@.openscad > > > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
RP
Ronaldo Persiano
Wed, Nov 20, 2019 5:09 PM

f = function(a)  a;
echo(f==f); // ECHO: false

???

OpenSCAD version 2019.10.25.ci3851 (git feee8f02)

Torsten Paul Torsten.Paul@gmx.de wrote:

Just to close this thread, function literals are
now available in snapshot versions > 2019-10-24.

The feature is marked experimental, so it needs
to be enabled Preferences -> Features.

ciao,
Torsten.

f = function(a) a; echo(f==f); // ECHO: false ??? OpenSCAD version 2019.10.25.ci3851 (git feee8f02) Torsten Paul <Torsten.Paul@gmx.de> wrote: > Just to close this thread, function literals are > now available in snapshot versions > 2019-10-24. > > The feature is marked experimental, so it needs > to be enabled Preferences -> Features. > > ciao, > Torsten. >
TP
Torsten Paul
Wed, Nov 20, 2019 5:35 PM

Yes, currently you can't compare function literals.

Yes, currently you can't compare function literals.
RP
Ronaldo Persiano
Tue, Nov 26, 2019 12:08 AM

I can't see why the following generates a stack overflow:

function move(a, g) =
function(x) g(x+a) ;

g1 = function(x) x;
g2 = move(0,g1);
g3 = move(0,g2);

echo(g3_x=g3);
echo(g3_2=g3(2));

If g3 is defined as g2 no error is reported.

I can't see why the following generates a stack overflow: function move(a, g) = function(x) g(x+a) ; g1 = function(x) x; g2 = move(0,g1); g3 = move(0,g2); echo(g3_x=g3); echo(g3_2=g3(2)); If g3 is defined as g2 no error is reported.