Author Topic: Replace item in list with new item  (Read 7665 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Replace item in list with new item
« Reply #30 on: February 13, 2008, 06:53:37 PM »
well, this has been a fun challenge. I'll have to check these things out later.

heh, not really useful though!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Replace item in list with new item
« Reply #31 on: February 13, 2008, 07:00:54 PM »
the best performance is achieved using the loops. of course it's fun to play with mapcar, but should someone ever risk using positional-list-pop on huge lists? ;)

daron

  • Guest
Re: Replace item in list with new item
« Reply #32 on: February 13, 2008, 09:49:18 PM »
I think they're useful.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Replace item in list with new item
« Reply #33 on: February 13, 2008, 11:54:48 PM »
My version:
Code: [Select]
  ;;  CAB 11.16.07
  ;;  Remove based on pointer list
  (defun RemoveNlst (nlst lst)
    (setq i -1)
    (vl-remove-if '(lambda (x) (not (null (vl-position (setq i (1+ i)) nlst))))
      lst
    )
  )
 
 
Code: [Select]
  (setq result (RemoveNlst '(1 4) '(0 "A" 2 3 "B" 5 6 7 8 9)))
  ;;  removes A & B
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Replace item in list with new item
« Reply #34 on: February 14, 2008, 12:01:58 AM »
One more:
Code: [Select]
;;  CAB 12/27/2006
;;  (RemoveNth 3 '(0 1 2 3 4 5))
;;  (0 1 2 4 5)
(defun removeNth (i lst)
  (setq i (1+ i))
  (vl-remove-if '(lambda(x) (zerop (setq i (1- i)))) lst)
)
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Replace item in list with new item
« Reply #35 on: February 14, 2008, 08:39:32 AM »
the best performance is achieved using the loops. of course it's fun to play with mapcar, but should someone ever risk using positional-list-pop on huge lists? ;)

My thoughts exactly!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Replace item in list with new item
« Reply #36 on: February 14, 2008, 09:11:03 AM »
A `pop' procedure only removes the first item in a list. It does ONLY that because its only useful for when you dont have the list in your possession (You only have the address for the variable or in some cases the variable name --since we in Autolisp cant really deal in address we have to use variable names).  Didnt you notice that you only pass the variable NAME to my procedure and that variable gets redefined...out of scope no less?

The concept is that if you need to modify that list then you would just go get that list and modify it as you need to... We are playing with variable scope not lists. (SET, SETQ, EVAL) not list modifiers (SUBST, MAPCAR, etc.)

If we want to rehash a remove nth procedure we can do that but a `pop' is NOT a remove nth procedure.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
Re: Replace item in list with new item
« Reply #37 on: February 14, 2008, 09:17:53 AM »
Didn't know that. I think removeNth has been hashed out enough. I guess that's really what I've been looking for, just didn't know there was a difference.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Replace item in list with new item
« Reply #38 on: February 14, 2008, 09:24:20 AM »
 Sorry, I didn't get it.???
Code: [Select]
(defun poplst (lst) (cdr lst))
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Replace item in list with new item
« Reply #39 on: February 14, 2008, 09:38:34 AM »
the variable isnt redefined. (Use SET, EVAL)

Command: *Cancel*

Command: (setq mylist '(1 2 3 4))
(1 2 3 4)

Command: (poplst mylist)
(2 3 4)

Command: !mylist
(1 2 3 4)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Replace item in list with new item
« Reply #40 on: February 14, 2008, 09:40:11 AM »
Code: [Select]
(defun list-push (sym lst)
  (set lst (cons sym (eval lst))) )

(defun list-pop (lst / ~tval )
  (setq ~tval (eval lst))
  (set lst (cdr ~tval))
  (car ~tval) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org