Sweety 发表于 2022-7-6 11:02:08

THANK YOU LEE.
 
But a question come up while studying and coding.And I could not find the answer to it.
 
How does the lambda function work in general and specially in the attached codes below . ??
And how could the variable ( i )start counting from ( -1 ) since it has supported with ( 1+ ) in index counter ?
 

(if (setq ss1 (ssget '((0 . "TEXT"))))   ((lambda (i / ent contents)(while (setq ent (ssname ss1 (setq i (1+ i))))   (setqcontents (cdr (assoc 1 (entget ent)))       )    (command "_.text" ..........)         ))-1   ) )
 
Hope that questions won't be too much annoying.
 
Thanks and Thanks again.

Lee Mac 发表于 2022-7-6 11:07:34

Your questions are not annoying, you are at least demonstrating a willingness to learn which is not to be frowned upon.
 
Firstly, the function 1+ will just increment any numerical argument by one:
 

(1+ -1) = 0 = (+ 1 -1)Think of the lambda function appearing on its own as just an anonymous function definition, i.e. a 'defun' without a name, hence we could rewrite this:
 

(if (setq ss (ssget "_X")) (   (lambda ( i )   (while (setq e (ssname ss (setq i (1+ i))))       (princ (cdr (assoc 0 (entget e))))   )   )   -1 ))As
 

(defun IterateSelSet ( i ) (while (setq e (ssname ss (setq i (1+ i))))   (princ (cdr (assoc 0 (entget e)))) ))(if (setq ss (ssget "_X")) (IterateSelSet -1))But, the selection set is likely to be only iterated once, hence it doesn't really warrant the overhead of defining a separate function just to iterate through it.
 
Hope this helps,
 
Lee

Sweety 发表于 2022-7-6 11:08:34

It's very kind of you to help me with your hard works and multiple examples as well.
 
With your last example of the Defun function I did understand the way it goes with Lambda function.
 
I mean, first I couldn't get it well because of the start in the Index with ssname function( 1+ ) , So now can I say that
lambda in the first example would test all the rest of codes and after that would start doing the job that related to it. ?
 
I guess all that due to ( 1- ) in the Lambda function which made me get it that way.
 
Am I right ?
 
Thank you so much.

Lee Mac 发表于 2022-7-6 11:13:52

You're welcome
 
The lambda function in my first example, and the IterateSelSet in my second are identical in the way they function. They are both doing the same thing, both taking a single numerical argument. But the lambda function can only be used in this once instance, as it is anonymous and hence cannot be 'called' later on.
 
Both these functions take one argument 'i', this is used to iterate through the SelectionSet using the ssname function, this is equivalent to doing this:
 

(setq i -1)(while (setq e (ssname ss (setq i (1+ i)))) ...)Except, as we are only using 'i' this once, there is no point perhaps to define it as a variable, but rather just pass it as an argument to an anonymous function, - that way, everything is kept as one block if you like. Of course, there are many ways to approach this, and I'm not proclaiming that any way is the right way, just my preference.

Sweety 发表于 2022-7-6 11:16:22

Exactly. That's what I meant.
 
With Lambda it would check all its related codes to reach to (1-), and would start looping in help with While function.
 
But if we supported (setq i -1) at the top of the While function, we could do instead of it with While function.
 
Thank you sweet hearted for your major help.
 
Sweety. :wink:

Lee Mac 发表于 2022-7-6 11:18:38

 
Not quite (unless I'm misunderstanding you), all of the examples I have provided work in the same way, just two of them are functions taking one argument, whereas in the last example a local variable is used in place of the argument. The functions are not "checking for the code to reach 1-", but rather the argument 'i' has data of -1 bound to it and it then used in the loop.

BlackBox 发表于 2022-7-6 11:22:46

I found this to be very useful:
 

Lee Mac 发表于 2022-7-6 11:25:53

 
That does look pretty interesting

Sweety 发表于 2022-7-6 11:27:23

 
It's a very kind of you to upload that precious book.
 
Mapcar + Lambda are mentioned a lot in different examples within that book.
 
It's greatly appreciated.
 
Sweety.

BlackBox 发表于 2022-7-6 11:32:38

 
</blockquote> 
You're welcome.
 
I've edited my earlier post, to include a link to the AU course, as well as the course handout. I saved the handout for reference, but originally watched the screen cast. Very informative, but much of it made more sense to me after seeing Lee's above examples.
 
Cheers!
页: 1 [2]
查看完整版本: Mapcar + lambda Description