乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: Sweety

[编程交流] Mapcar + lambda Description

[复制链接]

26

主题

149

帖子

127

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 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 ?
 
  1. (if (setq ss1 (ssget '((0 . "TEXT"))))     ((lambda (i / ent contents)  (while (setq ent (ssname ss1 (setq i (1+ i))))     (setq  contents (cdr (assoc 1 (entget ent)))         )    (command "_.text" ..........)         ))  -1     ) )

 
Hope that questions won't be too much annoying.
 
Thanks and Thanks again.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 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. ([color=blue][b]1+[/b][/color] -1) = 0 = ([b][color=blue]+[/color][/b] 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:
 
  1. (if (setq ss (ssget "_X")) [color=blue][b](   (lambda ( [color=red]i[/color] )     (while (setq e (ssname ss (setq [color=red]i[/color] (1+ [color=red]i[/color]))))       (princ (cdr (assoc 0 (entget e))))     )   )   [color=red]-1[/color] )[/b][/color])
As
 
  1. [color=blue][b](defun IterateSelSet ( [color=red]i[/color] ) (while (setq e (ssname ss (setq [color=red]i[/color] (1+ [color=red]i[/color]))))   (princ (cdr (assoc 0 (entget e)))) ))[/b][/color](if (setq ss (ssget "_X")) [color=blue][b](IterateSelSet [color=red]-1[/color])[/b][/color])
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
回复

使用道具 举报

26

主题

149

帖子

127

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 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.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 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:
 
  1. (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.
回复

使用道具 举报

26

主题

149

帖子

127

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 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:
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 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.
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 11:22:46 | 显示全部楼层
I found this to be very useful:
 
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 11:25:53 | 显示全部楼层
 
That does look pretty interesting
回复

使用道具 举报

26

主题

149

帖子

127

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 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.
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 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!
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-6 21:59 , Processed in 0.345680 second(s), 70 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表