Jump to content

Mapcar + lambda Description


Sweety

Recommended Posts

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.

Link to comment
Share on other sites

  • Replies 22
  • Created
  • Last Reply

Top Posters In This Topic

  • Sweety

    10

  • Lee Mac

    8

  • BlackBox

    4

  • muthu123

    1

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Link to comment
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...