mapcar / lambda help please

Discussion in 'AutoCAD' started by Daron Denton, Apr 2, 2004.

  1. Daron Denton

    Daron Denton Guest

    (mapcar '(lambda (X)
    (cdr X)
    )
    '((1 . "AAA")(1 . "BBB")(2 . "CCC")(2 . "DDD")(3 . "EEE")(3 . "FFF"))
    )
    returns ("AAA" "BBB" "CCC" "DDD" "EEE" "FFF")


    how do i get all the assoc 2 only? = ("CCC" "DDD")

    thanks,
    daron
     
    Daron Denton, Apr 2, 2004
    #1
  2. Daron Denton

    Josh Guest

    (MASSOC 2 '((1 . "AAA")(1 . "BBB")(2 . "CCC")(2 . "DDD")(3 . "EEE")(3 .
    "FFF")))
    returns ("CCC" "DDD")

    ;from www.acadx.com
    (defun massoc (key alist / x nlist)
    (foreach x alist
    (if (eq key (car x))
    (setq nlist (cons (cdr x) nlist))
    )
    )
    (reverse nlist)
    )
     
    Josh, Apr 2, 2004
    #2
  3. Another way ...

    (mapcar 'cdr
    (vl-remove-if-not
    '(lambda (pair) (eq 2 (car pair)))
    lst
    )
    )

    eg

    (mapcar 'cdr
    (vl-remove-if-not
    '(lambda (pair) (eq 2 (car pair)))
    '((1 . "AAA")(1 . "BBB")(2 . "CCC")(2 . "DDD")(3 . "EEE")(3 . "FFF"))
    )
    )

    ==> ("CCC" "DDD")

    You could wrap it up in a function, but it's already pretty succinct.

    But if you REALLY need to ...

    (defun cdrs ( key lst )
    (mapcar 'cdr
    (vl-remove-if-not
    '(lambda (pair) (eq key (car pair)))
    lst
    )
    )
    )

    (cdrs 2
    '( (1 . "AAA")
    (1 . "BBB")
    (2 . "CCC")
    (2 . "DDD")
    (3 . "EEE")
    (3 . "FFF")
    )
    )

    ==> ("CCC" "DDD")

    Cheers.

    "Daron Denton" <ask, and i'll give it to you> wrote in message (mapcar '(lambda (X)
    (cdr X)
    )
    '((1 . "AAA")(1 . "BBB")(2 . "CCC")(2 . "DDD")(3 . "EEE")(3 . "FFF"))
    )
    returns ("AAA" "BBB" "CCC" "DDD" "EEE" "FFF")


    how do i get all the assoc 2 only? = ("CCC" "DDD")

    thanks,
    daron
     
    michael puckett, Apr 2, 2004
    #3
  4. First, (mapcar '(lambda (x) (cdr x)) <list>)
    is the same as (mapcar 'cdr <list>)

    To do what you want:

    (defun getcdrs (key alist / res item)
    (while (setq item (assoc key alist))
    (setq res (cons (cdr item) res)
    alist (cdr (member item alist))
    )
    )
    res
    )

    (getcdrs 2 '((1 . "aaa") (2 . "bbb") (3 . "ccc") (2 . "ddd")))

    -> ("ddd" "bbb")



    AcadX for AutoCAD 2004 Beta 1
    http://mysite.verizon.net/~vze2vjds/acadx/AcadX16.zip
     
    Tony Tanzillo, Apr 2, 2004
    #4
  5. Daron Denton

    Daron Denton Guest

    (setq aa
    (mapcar '(lambda (X)
    (if (= (car x) 2) (cdr x))
    )
    '((1 . "AAA")(1 . "BBB")(2 . "CCC")(2 . "DDD")(3 . "EEE")(3 . "FFF"))
    )
    )returns = (nil nil "CCC" "DDD" nil nil)

    ---now *THIS* is interesting-----
    (setq aa
    (mapcar '(lambda (X)
    (if (= (car x) 2) (cdr x) (princ))
    )
    '((1 . "AAA")(1 . "BBB")(2 . "CCC")(2 . "DDD")(3 . "EEE")(3 . "FFF"))
    )
    )returns = ( "CCC" "DDD" ) <note the 2 spaces at the beginning and end>


    Command: (nth 0 aa)
    Command: (nth 1 aa)
    Command: (nth 2 aa) "CCC"
    Command: (nth 3 aa) "DDD"
    Command: (nth 4 aa)
    Command: (nth 5 aa)
    Command: (nth 6 aa) nil

    princ is returning something
    if i can get rid of those, it's golden
     
    Daron Denton, Apr 2, 2004
    #5
  6. Daron Denton

    Daron Denton Guest

    (nth 1 aa) returns nothing, not even nil
    (type (nth 1 aa)) returns SYM

    explanation anyone??
     
    Daron Denton, Apr 2, 2004
    #6
  7. Daron Denton

    Daron Denton Guest

    thanks for the explanation Tony.
    daron


     
    Daron Denton, Apr 2, 2004
    #7
  8. Daron Denton

    Daron Denton Guest

    thanks Michael. i was trying to stay away from foreach and while... well...
    just because i wanted to do it different this time. ;o)

    cheers to you,
    daron
     
    Daron Denton, Apr 2, 2004
    #8
  9. Daron Denton

    Doug Broad Guest

    Daron,

    Mapcar will give the slowest results. This question has been posed quite often in
    various forms. Some solutions are posted below:

    ;;Approximately as penned by Michael Puckett - fastest.
    (defun cdrsw (key lst / item return)
    (while (setq item (assoc key lst))
    (setq lst (cdr(member item lst))
    return (cons (cdr item) return)))
    (reverse return))


    ;;Recursive form of above - slower by about 20%.
    (defun cdrsr (key lst / item)
    (setq item (assoc key lst))
    (if item
    (cons (cdr item) (cdrs key (cdr (member item lst))))))


    ;;About the same speed as recursive form above
    (defun cdrsri2 (key lst)
    (mapcar 'cdr
    (vl-remove-if-not '(lambda (x) (= key (car x))) lst)))
     
    Doug Broad, Apr 2, 2004
    #9
  10. Daron Denton

    Don Butler Guest

    Sorry, I should have tried it first. Didn't think of the NIL evaluation.

    Don
     
    Don Butler, Apr 2, 2004
    #10
  11. Daron Denton

    Daron Denton Guest

    thanks Jon.

     
    Daron Denton, Apr 2, 2004
    #11
  12. Daron Denton

    Daron Denton Guest

    i had one similar to the top by Michael, but was interested in trying
    something different yesterday. speed isn't an issue in this particular case,
    but it's good to know for larger processing. thanks for the lesson Doug.
    :eek:)
    daron
     
    Daron Denton, Apr 2, 2004
    #12
  13. Otra manera Toni:

    (defun GetCdr (lst code)
    (vl-remove-if 'null
    (mapcar '(lambda (X) (if (= (car x) code) (cdr x))) lst)
    )
    )
    ;;;Example:
    ;;;(setq lst '((1 . "AAA")(1 . "BBB")(2 . "CCC")(2 . "DDD")(3 . "EEE")(3 .
    "FFF")))
    ;;;(getCdr lst 1) = ("AAA" "BBB")
    ;;;(getCdr lst 2) = ("CCC" "DDD")
    ;;;(getCdr lst 3) = ("EEE" "FFF")
    ;;;(getCdr lst 4) = nil
     
    José Luis Garcia Galán, Apr 2, 2004
    #13
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.