# Update proposal to get things nice( or alternatively decorator ? )

I am programming for OpenSCAD (www.openscad.org) a python interface.
One sample code is:
obj1 = linear_extrude(obj=lambda h:[[0,0],[1,0],[1,1],[0,1+hh/100]],height=10)
This creates an extrusion of a cross section with a “dynamic” cross-section.
It uses a lambda function in order not to define an extra function.
But it does not yet look very nice(or mathematical). A mathematics would write:
obj1 = linear_extrude(obj(h) =[[0,0],[1,0],[1,1],[0,1+h
h/100]],height=10)
I tested this boldly, but python did not understand my intent
is it possible to make get this working with a decorator ?
BTW: it could be a common method to use lambda functions as function arguments …
all the information is there, its shorter to write and more easy to read.

Thank you for your hints

I am programming for OpenSCAD
(www.openscad.org) a python interface.
One sample code is:
obj1 = linear_extrude(obj=lambda h:[[0,0],[1,0],[1,1],[0,1+h*h/100]],height=10)

Please wrap these in “code fences”; it makes things easier to read and
also preserves various formatting and punctuation, eg:

`````` ```
obj1 = linear_extrude(obj=lambda h:[[0,0],[1,0],[1,1],[0,1+h*h/100]],height=10)
```
``````

Anyway, to your suggestion:

This creates an extrusion of a cross section with a “dynamic” cross-section.
It uses a lambda function in order not to define an extra function.

Aside: this does define a function, but it doesn’t have a name

But it does not yet look very nice(or mathematical). A mathematics would write:
obj1 = linear_extrude(obj(h) =[[0,0],[1,0],[1,1],[0,1+h*h/100]],height=10)

Yes.

I tested this boldly, but python did not understand my intent
is it possible to make get this working with a decorator ?

Unfortunately, no.

The primary issue is that function arguments are evaluated before the
function is called. CHanging that would break, well, almost everything.
So in your preferred expression with `obj(h)=` the right hand sid is
always evaluated. As you’re discovered.

To prevent that you’d need a language level syntactic construct to say
“prepare this expression, but do not run it; instead, let me run it
later”. And that is inherently what a function definition does.

We’ve got two ways to do this: the `def f(x)` form and the `lambda x:`
form.

BTW: it could be a common method to use lambda functions as function arguments …
all the information is there, its shorter to write and more easy to read.

I think almost everyone agrees that writing the word `lambda` is very
cumbersome, and it doesn’t look much like a nice clean mathematical
expression.

There have been many calls to allow the greek letter lambda: λ
(GREEK SMALL LETTER LAMDA Unicode: U+019B), though I suppose technically
we should prefer: ƛ (LATIN SMALL LETTER LAMBDA WITH STROKE Unicode:
U+03BB) which I think is the one used in math.

These have generally fizzled, but would have allowed:

`````` obj1 = linear_extrude(obj=ƛ h:[[0,0],[1,0],[1,1],[0,1+h*h/100]],height=10)
``````

Another alternative is JavaScript like “arrow functions”:

`````` (h) -> ([[0,0],[1,0],[1,1],[0,1+h*h/100]])
``````

which are also not part of Python.

It may be better to go with a named function; at least they have the
benefit of having their name evoke the operation semantics and also let
you put in a docstring!

`````` def extrude_nice_adjective_here(h):
''' An extrusion of such and such a type.
'''
return [[0,0],[1,0],[1,1],[0,1+h*h/100]]
``````

Wordier, but then you can write:

`````` obj1 = linear_extrude(obj=extrude_nice_adjective_here, height=10)
``````

which is at least expressive.

Cheers,
Cameron Simpson cs@cskk.id.au

I’m curious how you think a decorator will help.

We can use decorators like this:

``````@decorator
def function():
...

@decorator
class MyClass:
...
``````

How is that going to help you write an expression `linear_extrude(obj(h) = [[0, 0], [1, 0], [1, 1], [0, 1+h*h/100]], height=10)` ? Where is the decorator supposed to be used?

What you are asking is if there is a way to write functions using this syntax:

``````func(x) = expression
``````

As you have already discovered, no, Python doesn’t support that syntax.

Hi Cameron,

Thank you for your reply. I appreciate very much the time which you spent to phrase your answer.
Very informative and I learned some things.
Now understanding, that there is way, that python can do natively, i can now approach plan B:

Is there something like a python preprocessor or typedef statement like:

#define varname(litereral)=expression varname=lambda literal:expression

which would be processed before python execution starts ?

If that existed I could benefit from the python LEX parser recognizing the word boundaries ?

if not i can imagine plan C:

I could write a regular expression replace on my source code and feed the result into the embedded python interpreter.
People will definitely recognize the python syntax and in some time maybe someone is wondering why native python does’t have such a
thing …
Just joking, but I will definitely keep the regexp thing in mind