all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Comparing lists
@ 2023-09-11 18:53 Petteri Hintsanen
  2023-09-12  7:26 ` Emanuel Berg
  2023-09-13 10:34 ` Philip Kaludercic
  0 siblings, 2 replies; 7+ messages in thread
From: Petteri Hintsanen @ 2023-09-11 18:53 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I'm writing tests for Emms.  There we have metadata structures like
this:

  '((identification-header
     (opus-head . "OpusHead")
     (channel-count . 1)
     (opus-version . 1))
    (comment-header
     (opus-tags . "OpusTags")
     (vendor-length . 13)
     (user-comments
      ((length . 38) (user-comment . "ENCODER=opusenc from opus-tools 0.1.10"))
      ((length . 7) (user-comment . "foo=bar"))
      ((length . 5) (user-comment . "Key=x")))
     (vendor-string . "libopus 1.3.1")
     (user-comments-list-length . 3)))

These come from bindat-unpack function.

The problem is that sometimes the same data can be in a reversed or
otherwise different order.  For example, the data above could be
arranged like this:

  '((comment-header
     (opus-tags . "OpusTags")
     (user-comments
      ((length . 7) (user-comment . "foo=bar"))
      ((length . 38) (user-comment . "ENCODER=opusenc from opus-tools 0.1.10"))
      ((length . 5) (user-comment . "Key=x")))
     (vendor-string . "libopus 1.3.1")
     (user-comments-list-length . 3)
     (vendor-length . 13))
    (identification-header
     (channel-count . 1)
     (opus-head . "OpusHead")
     (opus-version . 1)))

So the "siblings" in the hierarchy can switch places, but otherwise it
is the same data.

How to compare such lists for equality?
I devised this recursive func:

  (defun my-equal (x y)
    (cond ((and (not (proper-list-p x))
                (not (proper-list-p y)))
           (equal x y))
          ((and (proper-list-p x)
                (proper-list-p y))
           (seq-every-p (lambda (elt-x)
                          (seq-find (lambda (elt-y)
                                      (my-equal elt-x elt-y))
                                    y))
                        x))))

where the first cond is for comparing anything else but lists, and the
second cond is for comparing (nested) lists.  (Nevermind the complexity
or the fact that the function is not symmetrical.)

I'm not adept with lisp structures so I'm not particularly confident
about this.  Do you see any obvious problems?  Are there better options?

Thanks,
Petteri




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-09-15  2:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-11 18:53 Comparing lists Petteri Hintsanen
2023-09-12  7:26 ` Emanuel Berg
2023-09-12 12:19   ` Rudolf Schlatte
2023-09-13 15:32   ` Petteri Hintsanen
2023-09-13 10:34 ` Philip Kaludercic
2023-09-13 15:41   ` Petteri Hintsanen
2023-09-15  2:12     ` Michael Heerdegen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.