Jump to content
Sweety

Mapcar + lambda Description

Recommended Posts

Sweety

Hi GUYS. :)

 

I have read the Help which attached with Autocad and Afralisp explainations about

mapcar and lambda but without a chance to understand. :lol:

 

So if someone could help me with it, It would be very kind favor.

 

Thankxxxxxxx

 

Sweety.

Share this post


Link to post
Share on other sites
BlackBox

Mapcar combines lists, or can take an action on a list - in either event it (mapcar) returns the result as a list.

 

Lambda is an anonymous function within itself, which does not require a defun.

 

Often times, mapcar and lambda are used together to yield a desired result... Apply and lambda can also be combined, but lambda can be used alone as well.

 

Do you have something specific you want to code, or was this just a general inquiry?

Share this post


Link to post
Share on other sites
Sweety

Thanks Renderman.

 

You are always in the first . :D

 

I have a code but it's not mine and do not want to post to avoid any screw up from anyone. :lol:

 

So in general like this one .... I hope it has written right .

 

(mapcar '(lambda (x y) (+ (x y) 1.0) '(pt1 pt2)))

 

Question. how (x y) would be added to each other since they have no values .

 

If you add more explainations, It would be a very kind of you as usual.

 

Thankxxxxxxxxxxxxxxxxxxxxxxxxxx.

Share this post


Link to post
Share on other sites
Lee Mac

The way I see it, here is the crux of it, mapcar evaluates a function on every element of a list and returns a list containing the result of each such evaluation. The function can be any function, taking any number of arguments. lambda describes an anonymous function (usually used for a one-off usage, hence not warranting the overhead of a 'defun'), and, as a function, can be supplied as an argument to mapcar.

 

Examples are the best way to learn:

 

;; Add 1 to every element of a list:
(mapcar '1+ '(1 2 3 4 5))
==>  '(2 3 4 5 6)

Note that the function argument is quoted (using the apostrophe), as it is to be taken as an argument for mapcar and hence not evaluated. The apostrophe means, in laymans terms: take this statement at face value and don't evaluate it.

 

More Examples:

 

(defun AddTwo ( x ) (+ x 2))

(mapcar 'AddTwo '(1 2 3 4 5))
==> (3 4 5 6 7)

(mapcar '(lambda ( x ) (+ x 2)) '(1 2 3 4 5))
==> (3 4 5 6 7)

(mapcar '+ '(1 2 3 4 5) '(3 4 5 6 7))
==> (4 6 8 10 12) = ((+ 1 3) (+ 2 4) (+ 3 5) (+ 4 6) (+ 5 7))

(defun foo ( string number ) (strcat string (itoa number)))

(mapcar 'foo '("a" "b" "c") '(1 2 3))
("a1" "b2" "c3")

(mapcar '(lambda ( string number ) (strcat string (itoa number))) '("a" "b" "c") '(1 2 3))
("a1" "b2" "c3")

And using Apply:

 

(defun averageof2 ( num1 num2 ) (/ (+ num1 num2) 2.))

(apply 'averageof2 '( 1 2 ))
==> 1.5

(apply '(lambda ( num1 num2 ) (/ (+ num1 num2) 2.)) '( 1 2 ))
==> 1.5

Or, for a generic average function perhaps:

 

(defun average ( lst ) (/ (apply '+ lst) (float (length lst))))

(average '( 1 2 3 4 ))
==>  2.5

Or, here's one to mess with your head, average of a list of points:

 

(defun AveragePoint ( l ) (mapcar '(lambda ( x ) (/ x (float (length l)))) (apply 'mapcar (cons '+ l))))

(averagepoint '(( 2 3 1 ) ( 2 4 1 ) ( 1 5 1 )))
==> (1.66667 4.0 1.0)

Lee

Edited by Lee Mac

Share this post


Link to post
Share on other sites
BlackBox

Great examples, Lee (as usual). :wink:

 

The only thing I would add, is that another major 'appeal' to using mapcar is that it can handle a list of any length.

 

Sometimes you might only have a handful of items in your list, other times it could be +1000... in either case, mapcar (along with apply) can handle the job.

Share this post


Link to post
Share on other sites
Sweety

Wow :shock:

 

I was shocked with these examples, they are really very helpful.:)

 

So I have to clean up my mind to start studing these examples.

 

Thank you so much LEE.

Share this post


Link to post
Share on other sites
Lee Mac

Happy to help out, if you have any questions about my examples, just ask :)

Share this post


Link to post
Share on other sites
muthu123
Happy to help out, if you have any questions about my examples, just ask :)

 

Dear Mr.Lee,

 

This is perfect examples and you are really great and it will helpful to everybody even if they know about mapcar.

 

Thank you once again for your great effort.

Share this post


Link to post
Share on other sites
Lee Mac
Dear Mr.Lee,

 

This is perfect examples and you are really great and it will helpful to everybody even if they know about mapcar.

 

Thank you once again for your great effort.

 

Thanks Muthu for your kind words, I'm happy that many members may benefit from my post :)

Share this post


Link to post
Share on other sites
Sweety
Happy to help out, if you have any questions about my examples, just ask :)

THANK YOU LEE.

 

But a question come up while studying and coding. :lol: 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))))
     (setq  contents (cdr (assoc 1 (entget ent)))
	 )
    (command "_.text" ..........) 
        ))
  -1
     )
 )

 

Hope that questions won't be too much annoying. :unsure:

 

Thanks and Thanks again.

Share this post


Link to post
Share on other sites
Lee Mac

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:

 

([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:

 

(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

 

[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

Share this post


Link to post
Share on other sites
Sweety

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 ? 8)

 

Thank you so much.

Share this post


Link to post
Share on other sites
Lee Mac

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.

Share this post


Link to post
Share on other sites
Sweety

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:

Share this post


Link to post
Share on other sites
Lee Mac
With Lambda it would check all its related codes to reach to (1-), and would start looping in help with While function.

 

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.

Share this post


Link to post
Share on other sites
Lee Mac
I found to be very useful:

 

That does look pretty interesting :)

Share this post


Link to post
Share on other sites
Sweety
I found to be very useful:

 

It's a very kind of you to upload that precious book.:thumbsup:

 

Mapcar + Lambda are mentioned a lot in different examples within that book. :)

 

It's greatly appreciated.

 

Sweety.

Share this post


Link to post
Share on other sites
BlackBox
That does look pretty interesting :)

 

It's a very kind of you to upload that precious book.:thumbsup:

 

Mapcar + Lambda are mentioned a lot in different examples within that book. :)

 

It's greatly appreciated.

 

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! :beer:

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...