jla
jla

Reputation: 4770

Check occurrence of keyword with value in sublist

I have a data structure, consisting of property lists within lists, which are themselves inside a list. Essentially a 2d matrix of plists. It's of the form:

(((:VALUE 0 :GROUP 0) (:VALUE 0 :GROUP 0))
 ((:VALUE 0 :GROUP 0) (:VALUE 0 :GROUP 0))
 ((:VALUE 0 :GROUP 0) (:VALUE 0 :GROUP 0)))

Each :value and :group will contain an arbitrary integer value. I need to find if any of the :group keywords are 0. I've been trying to achieve this with the member function, but it only ever returns nil.

My code, assuming the data structure is called data, is:

(member '(:group 0) data :test 'equal))

How would I use the member function to check the occurrence of :group 0 in a sublist of a sublist? (if indeed that's possible, or should I just use a couple of loops to iterate through?)

Upvotes: 3

Views: 155

Answers (1)

sds
sds

Reputation: 60054

Try getf as the :key for find/find-if and/or remove/remove-if-not:

Level 1 nesting

Prepare the data

(defparameter *data-1*
  (loop repeat 10 collect (list :value (random 10) :group (random 10))))
*data-1*
((:VALUE 7 :GROUP 4) (:VALUE 9 :GROUP 6) (:VALUE 2 :GROUP 7) (:VALUE 8 :GROUP 2)
 (:VALUE 5 :GROUP 6) (:VALUE 4 :GROUP 7) (:VALUE 3 :GROUP 5) (:VALUE 8 :GROUP 7)
 (:VALUE 0 :GROUP 3) (:VALUE 5 :GROUP 5))

A: Find the 1st occurrence of group 7:

(find 7 *data-1* :key (lambda (pl) (getf pl :group)))
==> (:VALUE 2 :GROUP 7)

B: Find all occurrences of group 7:

(remove 7 *data-1* :key (lambda (pl) (getf pl :group)) :test-not #'eql)
==> ((:VALUE 2 :GROUP 7) (:VALUE 4 :GROUP 7) (:VALUE 8 :GROUP 7))

Level 2 nesting

Prepare the data:

(defparameter *data-2*
  (loop repeat 10 collect
      (loop repeat (random 4) collect
          (list :value (random 10) :group (random 10)))))
*data-2*
(((:VALUE 9 :GROUP 9) (:VALUE 8 :GROUP 2) (:VALUE 6 :GROUP 7)) NIL
 ((:VALUE 9 :GROUP 5)) ((:VALUE 9 :GROUP 1)) NIL
 ((:VALUE 2 :GROUP 7) (:VALUE 6 :GROUP 5) (:VALUE 2 :GROUP 1))
 ((:VALUE 1 :GROUP 4) (:VALUE 5 :GROUP 5))
 ((:VALUE 9 :GROUP 7) (:VALUE 9 :GROUP 8) (:VALUE 7 :GROUP 4))
 ((:VALUE 4 :GROUP 7)) NIL)

C: Find the 1st occurrence of group 7:

(find-if (lambda (l)
           (find 7 l :key (lambda (pl) (getf pl :group))))
         *data-2*)
==> ((:VALUE 9 :GROUP 9) (:VALUE 8 :GROUP 2) (:VALUE 6 :GROUP 7))

D: Find all occurrences of group 7:

(remove-if-not (lambda (l)
                 (find 7 l :key (lambda (pl) (getf pl :group))))
               *data-2*)
==>
(((:VALUE 9 :GROUP 9) (:VALUE 8 :GROUP 2) (:VALUE 6 :GROUP 7))
 ((:VALUE 2 :GROUP 7) (:VALUE 6 :GROUP 5) (:VALUE 2 :GROUP 1))
 ((:VALUE 9 :GROUP 7) (:VALUE 9 :GROUP 8) (:VALUE 7 :GROUP 4))
 ((:VALUE 4 :GROUP 7)))

Define functions:

Depending on your needs, you might want to define functions

(defun get-group (plist)
  (getf plist :group))
(defun find-group (list-of-plists group)
  (find group list-of-plists :key #'get-group))
(defun make-group-finder (group)
  (lambda (list-of-plists) (find-group list-of-plists group)))

Now you can use them for the tasks above:

A: (find-group *data-1* 7)

B: (remove 7 *data-1* :key #'get-group :test-not #'eql)

C: (find-if (make-group-finder 7) *data-2*)

D: (remove-if-not (make-group-finder 7) *data-2*)

PS remove-if-not is the Lisp idiom for "filter".

Upvotes: 6

Related Questions