Author Topic: Perform operation to a single item in list  (Read 4216 times)

0 Members and 1 Guest are viewing this topic.

Binky

  • Guest
Perform operation to a single item in list
« on: July 16, 2009, 02:57:40 PM »
Hello folks,

New State, City, Job, Home, and everything else.  Took nearly a year but things have settled down enough to get back to coding so here I am again asking for your guidence.

I am kind of surprised that I have not needed/wanted to do this yet, but here I am trying to find a way to add a value to an existing value inside a list.

original list
(1 2 3 4 5)

I want to add 7 to the  3rd element something like this(only a version that works)
(setq (nth 2 list) (+(nth 2 list) 7))

list afterwards
(1 2 10 4 5 )

I can imagine ways to do this but they all involve creating a new list with the adjusted value.  Hoping for a easier solution, just drawing a blank today.

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Perform operation to a single item in list
« Reply #1 on: July 16, 2009, 03:03:42 PM »
I think I am drawing a blank too...

this is the best I have so far...    :|

Code: [Select]
(defun add (lst i)
  (mapcar
    (function
      (lambda (x)
        (if (eq i (vl-position x lst))
          (+ 7 x) x))) lst))

(add '(1 2 3 4 5) 2)
(1 2 10 4 5)


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Perform operation to a single item in list
« Reply #2 on: July 16, 2009, 03:12:47 PM »
You may want to change the order of the arguments, so they flow better.

Code: [Select]
(defun AddAt ( inList loc num / cnt )
   
    (setq cnt -1)
    (mapcar
        (function
            (lambda ( x )
                (setq cnt (1+ cnt))
                (if (equal cnt loc)
                    (+ num x)
                    x
                )
            )
        )
        inList
    )
)

Command: (AddAt '(1 2 3 4 5) 2 7)

(1 2 10 4 5)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Perform operation to a single item in list
« Reply #3 on: July 16, 2009, 03:21:58 PM »
Another, more versatile, function.

Code: [Select]
(defun PerformAt ( func loc inList / cnt )
   
    (setq cnt -1)
    (mapcar
        (function
            (lambda ( x )
                (setq cnt (1+ cnt))
                (if (equal cnt loc)
                    (func x)
                    x
                )
            )
        )
        inList
    )
)

Command: (performat (lambda (x) (+ 3 x)) 3 '(1 2 3 4 5 6 7))
(1 2 3 7 5 6 7)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Perform operation to a single item in list
« Reply #4 on: July 16, 2009, 03:40:53 PM »
Code: [Select]
(defun test (lst n func)
  (if lst
    (if (zerop n)
      (cons (func (car lst)) (cdr lst))
      (cons (car lst) (test (cdr lst) (1- n) func))
    )
  )
)
;;;(test '(1 2 3 4 5) 2 (lambda (e) (+ e 7)))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Perform operation to a single item in list
« Reply #5 on: July 16, 2009, 03:49:06 PM »
Code: [Select]
;; variation by CAB
(defun adjust_nth (opr num lst i)
  (setq i (1+ i))
  (mapcar '(lambda (x) (if (zerop (setq i (1- i))) (opr num x) x)) lst)
)

Code: [Select]
_$ (adjust_nth + 3 '(1 2 3 4 5 6 7) 4)
(1 2 3 4 8 6 7)
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.

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Perform operation to a single item in list
« Reply #6 on: July 16, 2009, 03:57:55 PM »
Nice Alan, I've seen you use that method before, in your remove_nth function  :-)

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Perform operation to a single item in list
« Reply #7 on: July 16, 2009, 03:59:13 PM »
You may want to change the order of the arguments, so they flow better.

I don't understand what you mean Tim  :?

Binky

  • Guest
Re: Perform operation to a single item in list
« Reply #8 on: July 16, 2009, 04:09:06 PM »
Humbled and grateful

Thanks tons!!!!


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Perform operation to a single item in list
« Reply #9 on: July 16, 2009, 04:10:41 PM »
You may want to change the order of the arguments, so they flow better.

I don't understand what you mean Tim  :?

The way it is written is ' add at list location number '.  It might be better written ' add at location number list '.  Slight modification, but will sound better when you say it, or think about it.  At least I think so.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Perform operation to a single item in list
« Reply #10 on: July 16, 2009, 04:39:39 PM »
You may want to change the order of the arguments, so they flow better.

I don't understand what you mean Tim  :?

The way it is written is ' add at list location number '.  It might be better written ' add at location number list '.  Slight modification, but will sound better when you say it, or think about it.  At least I think so.

Ahh, I see  :-)

curmudgeon

  • Newt
  • Posts: 194
Re: Perform operation to a single item in list
« Reply #11 on: July 16, 2009, 04:47:41 PM »
Code: [Select]
;; variation by CAB
(defun adjust_nth (opr num lst i)
  (setq i (1+ i))
  (mapcar '(lambda (x) (if (zerop (setq i (1- i))) (opr num x) x)) lst)
)

Code: [Select]
_$ (adjust_nth + 3 '(1 2 3 4 5 6 7) 4)
(1 2 3 4 8 6 7)

man!
that is clean. bravo.
Never express yourself more clearly than you are able to think.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Perform operation to a single item in list
« Reply #12 on: July 16, 2009, 04:51:04 PM »
A different twist on a generic solution, Q+D, coded lean, no error checking etc.

Code: [Select]
(defun ApplyFooToNth ( [color=red]foo[/color] [color=green]n[/color] [color=blue]lst[/color] )
    (   (lambda ( bar i ) (mapcar 'bar lst))
        (lambda ( a )
            (if (eq n (setq i (1+ i)))
                (progn (defun bar (a) a) (foo a))
                a
            )
        )
        -1
    )    
)

Looks verbose but should perform reasonably.

(ApplyFooToNth (lambda (x) (+ 7 x)) 2 '(1 2 3 4 5))

=> (1 2 10 4 5)

Edit: Revised specifically to tease se7en. :)

« Last Edit: July 16, 2009, 05:45:45 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Perform operation to a single item in list
« Reply #13 on: July 16, 2009, 04:57:53 PM »
    (defun bar ( a )
        (if (eq n (setq i (+ i j)))
            (progn (defun bar (a) a) (foo a))
            a
        )
    )

I think my head just exploded...   :laugh:

Nice use of colours though  :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10653
Re: Perform operation to a single item in list
« Reply #14 on: July 16, 2009, 05:00:47 PM »
Binky, where da-hell you been?! Ive tried several times to get a hold of you.
Hows things?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org