all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Update assq-delete-all to support keys of string type
@ 2015-05-12 14:57 Kaushal
  2015-05-12 17:02 ` John Mastro
  2015-05-13  0:13 ` Stephen J. Turnbull
  0 siblings, 2 replies; 4+ messages in thread
From: Kaushal @ 2015-05-12 14:57 UTC (permalink / raw)
  To: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 1165 bytes --]

Hi,

I wanted to use assq-delete-all to remove all elements in an alist whose
car matched a string.

Example alist:

(setq temp-alist '(("a" . 1) ("b" . 2)))

Currently I cannot do

(setq temp-alist (assq-delete-all "a" temp-alist))

Is there a right way to do this?

For now, I just created a new function by simply replacing eq with string=

(defun assq-delete-all-string (key-str alist)
  "Delete from ALIST all elements whose car is a string and matches KEY-STR.
Return the modified alist.
Elements of ALIST that are not conses are ignored."
  (while (and (consp (car alist))
      (string= (car (car alist)) key-str))
    (setq alist (cdr alist)))
  (let ((tail alist) tail-cdr)
    (while (setq tail-cdr (cdr tail))
      (if (and (consp (car tail-cdr))
       (string= (car (car tail-cdr)) key-str))
  (setcdr tail (cdr tail-cdr))
(setq tail tail-cdr))))
  alist)

Can the assq-delete-all be updated to use string= when the KEY type is
string and eq otherwise (if those two cases cover everything?)

In general I am requesting assq-delete-all to use

- `string=` if KEY is string
- `SOME-OTHER-FN` if KEY is SOME-OTHER-TYPE
- `eq` otherwise

--
Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 4011 bytes --]

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

* Re: Update assq-delete-all to support keys of string type
  2015-05-12 14:57 Update assq-delete-all to support keys of string type Kaushal
@ 2015-05-12 17:02 ` John Mastro
  2015-05-12 17:09   ` Kaushal
  2015-05-13  0:13 ` Stephen J. Turnbull
  1 sibling, 1 reply; 4+ messages in thread
From: John Mastro @ 2015-05-12 17:02 UTC (permalink / raw)
  To: Emacs developers; +Cc: Kaushal

Kaushal <kaushal.modi@gmail.com> wrote:
> I wanted to use assq-delete-all to remove all elements in an alist whose car
> matched a string.
>
> Example alist:
>
> (setq temp-alist '(("a" . 1) ("b" . 2)))
>
> Currently I cannot do
>
> (setq temp-alist (assq-delete-all "a" temp-alist))
>
> Is there a right way to do this?

You can do this with `cl-remove':

(cl-remove "a"
           '(("a" . 1) ("b" . 2))
           :key #'car
           :test #'string=) ;=> (("b" . 2))

-- 
john



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

* Re: Update assq-delete-all to support keys of string type
  2015-05-12 17:02 ` John Mastro
@ 2015-05-12 17:09   ` Kaushal
  0 siblings, 0 replies; 4+ messages in thread
From: Kaushal @ 2015-05-12 17:09 UTC (permalink / raw)
  To: John Mastro; +Cc: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 634 bytes --]

Thanks!


--
Kaushal Modi

On Tue, May 12, 2015 at 1:02 PM, John Mastro <john.b.mastro@gmail.com>
wrote:

> Kaushal <kaushal.modi@gmail.com> wrote:
> > I wanted to use assq-delete-all to remove all elements in an alist whose
> car
> > matched a string.
> >
> > Example alist:
> >
> > (setq temp-alist '(("a" . 1) ("b" . 2)))
> >
> > Currently I cannot do
> >
> > (setq temp-alist (assq-delete-all "a" temp-alist))
> >
> > Is there a right way to do this?
>
> You can do this with `cl-remove':
>
> (cl-remove "a"
>            '(("a" . 1) ("b" . 2))
>            :key #'car
>            :test #'string=) ;=> (("b" . 2))
>
> --
> john
>

[-- Attachment #2: Type: text/html, Size: 1496 bytes --]

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

* Update assq-delete-all to support keys of string type
  2015-05-12 14:57 Update assq-delete-all to support keys of string type Kaushal
  2015-05-12 17:02 ` John Mastro
@ 2015-05-13  0:13 ` Stephen J. Turnbull
  1 sibling, 0 replies; 4+ messages in thread
From: Stephen J. Turnbull @ 2015-05-13  0:13 UTC (permalink / raw)
  To: Kaushal; +Cc: Emacs developers

Kaushal writes:

 > I wanted to use assq-delete-all to remove all elements in an alist whose
 > car matched a string.

Aside: "assq" means to use the test `eq' when comparing keys.  You
would name this function `assoc-delete-all' if you just want to permit
strings as well as symbols and numbers (as the suggestion to use
cl-remove does).

 > (defun assq-delete-all-string (key-str alist)

In this version, where you restrict to string, I would name it
`string-assoc-delete-all' or `assoc-string-delete-all'.

I don't know if it makes sense to perpetuate the traditional names
with the availabilty of cl-lib.




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

end of thread, other threads:[~2015-05-13  0:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-12 14:57 Update assq-delete-all to support keys of string type Kaushal
2015-05-12 17:02 ` John Mastro
2015-05-12 17:09   ` Kaushal
2015-05-13  0:13 ` Stephen J. Turnbull

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.