Author Topic: How do I filter a list?  (Read 100 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1066
How do I filter a list?
« on: November 12, 2019, 12:44:51 PM »
I am clueless as how to use vl-remove-if vl-member, etc. I would like to know how to filter a list based on an item. For instance. If the list contains "test" I want to remove it from the list and return a list without it.
Code: [Select]
(setq lst (list '("1" "2" "3" "test") '("a" "b" "c" "finished") '("a" "b" "c" "test")))
desired result to filter out "test"
Code: [Select]
_$ lst
(("a" "b" "c" "finished"))

desired result to filter out "finished"
Code: [Select]
_$ lst
(("1" "2" "3" "test")("a" "b" "c" "test"))

MP

  • Seagull
  • Posts: 17467
Re: How do I filter a list?
« Reply #1 on: November 12, 2019, 01:18:32 PM »
If case is not a concern perhaps ...

Code: [Select]
(defun foo ( item-to-remove lst )
    (if (atom (car lst))
        (vl-remove item-to-remove lst)
        (mapcar (function (lambda (x) (foo item-to-remove x))) lst)
    )
)

(foo "test"
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)

>>

(
    ("1" "2" "3")
    ("a" "b" "c" "finished")
    ("a" "b" "c")
)


Cheers.
« Last Edit: November 12, 2019, 01:22:30 PM by MP »
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

dubb

  • Swamp Rat
  • Posts: 1066
Re: How do I filter a list?
« Reply #2 on: November 12, 2019, 01:29:34 PM »
Like the the function but it only removes the string for me. I want to remove an entire list from the list.

Desired list if "finished" was filtered.

Code: [Select]
(("1" "2" "3" "test")("a" "b" "c" "test"))


If case is not a concern perhaps ...

Code: [Select]
(defun foo ( item-to-remove lst )
    (if (atom (car lst))
        (vl-remove item-to-remove lst)
        (mapcar (function (lambda (x) (foo item-to-remove x))) lst)
    )
)

(foo "test"
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)

>>

(
    ("1" "2" "3")
    ("a" "b" "c" "finished")
    ("a" "b" "c")
)


Cheers.

MP

  • Seagull
  • Posts: 17467
Re: How do I filter a list?
« Reply #3 on: November 12, 2019, 01:34:27 PM »
Wow,  I misunderstood.

Code: [Select]
(vl-remove-if
   '(lambda (x) (member "test" x))
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)
 

>> (("a" "b" "c" "finished"))

Code: [Select]
(vl-remove-if
   '(lambda (x) (member "finished" x))
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)
 

>> (("1" "2" "3" "test") ("a" "b" "c" "test"))

\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

dubb

  • Swamp Rat
  • Posts: 1066
Re: How do I filter a list?
« Reply #4 on: November 12, 2019, 01:39:42 PM »
You're a genius!
It works!

Here is my failed attempt.
Code: [Select]
(mapcar '(lambda (x)
(if (/= (vl-position "test" (nth x lst)) nil)
     (vl-remove (nth x lst) lst)
     (princ)
   )
)lst)


Thanks!
Wow,  I misunderstood.

Code: [Select]
(vl-remove-if
   '(lambda (x) (member "test" x))
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)
 

>> (("a" "b" "c" "finished"))

Code: [Select]
(vl-remove-if
   '(lambda (x) (member "finished" x))
   '(   ("1" "2" "3" "test")   
        ("a" "b" "c" "finished")
        ("a" "b" "c" "test")
    )
)
 

>> (("1" "2" "3" "test") ("a" "b" "c" "test"))

MP

  • Seagull
  • Posts: 17467
Re: How do I filter a list?
« Reply #5 on: November 12, 2019, 02:05:11 PM »
Once you get comfortable with the docs youll realize Im not. Sorry for the scenic route, I frequently read WAY too fast and think Ive the gist ...

Cheers.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Lee Mac

  • Seagull
  • Posts: 12283
  • London, England
Re: How do I filter a list?
« Reply #6 on: November 12, 2019, 05:25:04 PM »
Whilst by far the easiest method would be to make use of the vl-remove-if function, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo1 ( itm lst )
  2.     (vl-remove-if '(lambda ( x ) (member itm x)) lst)
  3. )

There are several other ways to tackle this particular task - for example, using basic iteration:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo2 ( itm lst / rtn )
  2.     (foreach x lst (or (member itm x) (setq rtn (cons x rtn))))
  3.     (reverse rtn)
  4. )

Or recursion:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo3 ( itm lst )
  2.     (if lst
  3.         (if (member itm (car lst))
  4.             (foo3 itm (cdr lst))
  5.             (cons (car lst) (foo3 itm (cdr lst)))
  6.         )
  7.     )
  8. )

dubb

  • Swamp Rat
  • Posts: 1066
Re: How do I filter a list?
« Reply #7 on: November 12, 2019, 06:00:15 PM »
Awesome! Thank you guys very much. I can understand vl-remove a little better now.

Greg B

  • Seagull
  • Posts: 12337
  • Tell me a Joke!
Re: How do I filter a list?
« Reply #8 on: Today at 08:52:31 AM »
I was thinking the answer was "alphabetically".