Author Topic: (discussion) Costruct procedures with Lambda  (Read 10310 times)

0 Members and 1 Guest are viewing this topic.

whdjr

  • Guest
Re: (discussion) Costruct procedures with Lambda
« Reply #15 on: March 02, 2006, 11:19:39 AM »
I really like using lambda however I rarely use it on it's own.  Here are two functions I used alot:
Quote
(defun *layout_list* (/ lst)
  (vlax-map-collection
    (vla-get-layouts
      (vla-get-activedocument (vlax-get-acad-object))
    )
    '(lambda (x) (setq lst (cons x lst)))
  )
  (cdr
    (*sort* lst 'vla-get-taborder)
  )
)

(defun *sort* (lst func)
  (vl-sort lst
      '(lambda (e1 e2)
         (< ((eval func) e1) ((eval func) e2))
       )
  )
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (discussion) Costruct procedures with Lambda
« Reply #16 on: March 02, 2006, 11:42:15 AM »
How about this?

Code: [Select]
(defun sum-squares-lst (lst)
  (apply '+ (MAPCAR '(lambda (x) (* x x)) lst))
)

;;(sum-squares-lst '(2 3)); => 13
;;(sum-squares-lst '(2 3 4)); => 29
;;(sum-squares-lst '(1 2 3 4 5 6 7 8 9 10)); => 385
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

whdjr

  • Guest
Re: (discussion) Costruct procedures with Lambda
« Reply #17 on: March 02, 2006, 12:51:24 PM »
WOOHOO!!!!

Finally someone used Mapcar, Lambda, and Apply all in the same routine.   :evil:





In case your wondering I do like your version. :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: (discussion) Costruct procedures with Lambda
« Reply #18 on: March 02, 2006, 04:29:09 PM »
I will admit that I was going to respond with the same old "*sigh*" or a "What are you guys talking about" but instead; Ive thought, and thought about some of your responces. I was forced to read, and re-read. And I think I may finaly have an understanding. Are you ready for this. (This is what it all amounts down to.) I dosent matter. Not one bit. There is (or should be) any loss in prformance, speed, etc in the end.

As far as I can tell from my readings, the interpretor does not see a named abstraction as a 'name' during the evaluation process.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: (discussion) Costruct procedures with Lambda
« Reply #19 on: March 02, 2006, 05:20:57 PM »
As far as I can tell from my readings, the interpretor does not see a named abstraction as a 'name' during the evaluation process.

Maybe not, but the reader/maintainer does, and THAT is where a great deal of its value lies.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (discussion) Costruct procedures with Lambda
« Reply #20 on: March 02, 2006, 05:25:09 PM »
As far as I can tell from my readings, the interpretor does not see a named abstraction as a 'name' during the evaluation process.

Maybe not, but the reader/maintainer does, and THAT is where a great deal of its value lies.

[nodding.gif]
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: (discussion) Costruct procedures with Lambda
« Reply #21 on: March 02, 2006, 06:35:15 PM »
STOP THE PRESS! I will have to do more thinking on this subject.



> Readability
So is that an agrement towards the use of lambda vs a name or visa-versa? Because one could argue both ways.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: (discussion) Costruct procedures with Lambda
« Reply #22 on: March 02, 2006, 09:52:52 PM »
I suppose it comes down to personal preference in the end.  I generally find named abstractions to be easier to follow, though there is the disadvantage of having to flip back and forth between the function definition and the places where it is used.  However, for very short functions that are used only in one place, I can see the argument for using a lambda function inline.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #23 on: March 03, 2006, 03:12:47 AM »
How about this?

Code: [Select]
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '(lambda (x) (* x x)) lst)))
Compare...
Code: [Select]
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '* lst lst)))

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (discussion) Costruct procedures with Lambda
« Reply #24 on: March 03, 2006, 03:23:50 AM »
How about this?

Code: [Select]
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '(lambda (x) (* x x)) lst)))
Compare...
Code: [Select]
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '* lst lst)))


Hello ElpanovEvgeniy, Welcome to theSwamp !
Quote
Sorry, my English it is awful...

 Your LISP is VERY good ! Nice code.   :-)



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #25 on: March 03, 2006, 03:42:19 AM »
But here no LAMBDA  :-(

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (discussion) Costruct procedures with Lambda
« Reply #26 on: March 03, 2006, 03:44:45 AM »
 :cry:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #27 on: March 03, 2006, 03:59:34 AM »
:cry:
Here is lambda!
Code: [Select]
;| A library function
; Multiple changing one elements of a list
; On other.
; Function usables in all events when it is possible
; Check on correspondence, through "EQUAL"
; Writer Evgeniy Elpanov.
; ***********************************************************
; Arguments:
; lst - a list in which it is necessary to make changings
; lst-i - correspondences ((changed replacer)... ())
; ***********************************************************
; An example of a call
; (multi-subst '(1 2 3 4 5)' ((1 "a") (3 "b")))
; Function will return ("a" 2 "b" 45)
|;
(defun multi-subst (lst lst-i)
  (eval
    (list
      (function mapcar)
      (list
        (function function)
        (list
          (function lambda)
          '(a)
          (cons
            (function cond)
              (reverse
               (cons
                '(t a)
                  (mapcar
                    (function
                      (lambda (a)
                        (list
                          (list
                            (function equal)
                            (car a)
                            'a
                          ) ; _ list
                          (cadr a)
                        ) ; _ list
                      ) ; _ lambda
                    ) ; _ function
                    lst-i
                  ) ; _ mapcar
                ) ; _ cons
             ) ; _ reverse
          ) ; _ cons
        ) ; _ cons
      ) ; _ list
      'lst
    ) ; _ list
  ) ; _ eval
) ; _ defun
« Last Edit: March 05, 2006, 04:09:30 PM by ElpanovEvgeniy »

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: (discussion) Costruct procedures with Lambda
« Reply #28 on: March 03, 2006, 06:59:43 AM »
Here is lambda!

Indeed it is.  :-o

Nice stuff Evgeniy, welcome to theswamp.
TheSwamp.org  (serving the CAD community since 2003)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (discussion) Costruct procedures with Lambda
« Reply #29 on: March 03, 2006, 07:19:03 AM »
good fun Evgeniy .. that will keep John < Se7en > busy   :lol:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.