unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Artur Malabarba <bruce.connor.am@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: "Stephen J. Turnbull" <stephen@xemacs.org>,
	David Kastrup <dak@gnu.org>, emacs-devel <emacs-devel@gnu.org>
Subject: Re: Merging nth, aref, and elt.
Date: Sat, 13 Dec 2014 04:09:51 +0000	[thread overview]
Message-ID: <CAAdUY-+Wd5_jZEW04atayjokMtGx5tdj04SuNvS_+gvE=xw4RQ@mail.gmail.com> (raw)
In-Reply-To: <jwva94b7gus.fsf-monnier+emacs@gnu.org>

Here's an initial diff for hash-tables. Should it use `get' instead of `elt'?

    (fns.c) elt: Also work on hash-tables

    (lisp/emacs-lisp/gv.el): Fix setf on `elt'

    (bytecode.c) exec_byte_code: Add nil arg to Felt call.

3 files changed, 26 insertions(+), 14 deletions(-)
 lisp/emacs-lisp/gv.el |  7 ++++---
 src/bytecode.c        |  2 +-
 src/fns.c             | 31 +++++++++++++++++++++----------

    Modified   lisp/emacs-lisp/gv.el
diff --git a/lisp/emacs-lisp/gv.el b/lisp/emacs-lisp/gv.el
index a0f92a5..11c35a4 100644
--- a/lisp/emacs-lisp/gv.el
+++ b/lisp/emacs-lisp/gv.el
@@ -302,7 +302,8 @@ The return value is the last VAL in the list.
 (gv-define-setter cdar (val x) `(setcdr (car ,x) ,val))
 (gv-define-setter cddr (val x) `(setcdr (cdr ,x) ,val))
-(gv-define-setter elt (store seq n)
-  `(if (listp ,seq) (setcar (nthcdr ,n ,seq) ,store)
-     (aset ,seq ,n ,store)))
+(gv-define-setter elt (store set index)
+  `(if (listp ,set) (setcar (nthcdr ,index ,set) ,store)
+     (if (arrayp ,set) (aset ,set ,index ,store)
+       (puthash ,index ,store ,set))))
 (gv-define-simple-setter get put)
 (gv-define-setter gethash (val k h &optional _d) `(puthash ,k ,val ,h))
    Modified   src/bytecode.c
diff --git a/src/bytecode.c b/src/bytecode.c
index d3c8b47..456f23d 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -1843,5 +1843,5 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object
vector, Lisp_Object maxdepth,
         BEFORE_POTENTIAL_GC ();
         v1 = POP;
-        TOP = Felt (TOP, v1);
+        TOP = Felt (TOP, v1, Qnil);
         AFTER_POTENTIAL_GC ();
           }
    Modified   src/fns.c
diff --git a/src/fns.c b/src/fns.c
index e891fdb..aabe757 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -1355,15 +1355,26 @@ N counts from zero.  If LIST is not that long,
nil is returned.  */)
 }

-DEFUN ("elt", Felt, Selt, 2, 2, 0,
-       doc: /* Return element of SEQUENCE at index N.  */)
-  (register Lisp_Object sequence, Lisp_Object n)
-{
-  CHECK_NUMBER (n);
-  if (CONSP (sequence) || NILP (sequence))
-    return Fcar (Fnthcdr (n, sequence));
+DEFUN ("elt", Felt, Selt, 2, 3, 0,
+       doc: /* Return element of SET at INDEX.
+If SET is a list or an array, INDEX is a number and DFLT is ignored.
+If SET is a hash table, INDEX is a key.  If INDEX is not found, return
+DFLT which defaults to nil.  */)
+  (register Lisp_Object set, Lisp_Object index, Lisp_Object dflt)
+{
+  if (CONSP (set) || NILP (set))
+    {
+      CHECK_NUMBER (index);
+      return Fcar (Fnthcdr (index, set));
+    }
+
+  if (ARRAYP (set))
+    {
+      CHECK_NUMBER (index);
+      return Faref (set, index);
+    }

-  /* Faref signals a "not array" error, so check here.  */
-  CHECK_ARRAY (sequence, Qsequencep);
-  return Faref (sequence, n);
+  /* Fgethash signals a "not hash-table" error, so check here.  */
+  CHECK_TYPE (HASH_TABLE_P (set), Qsequencep, set);
+  return Fgethash (index, set, dflt);
 }



  reply	other threads:[~2014-12-13  4:09 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-29 21:40 Merging nth, aref, and elt Artur Malabarba
2014-10-30  1:52 ` Stefan Monnier
2014-10-30  9:51   ` David Kastrup
2014-10-30 13:30     ` Artur Malabarba
2014-10-31 19:13       ` Stefan Monnier
2014-10-31 19:56         ` Stefan Monnier
2014-11-01  1:08           ` Stephen J. Turnbull
2014-11-01  3:19             ` Stefan Monnier
2014-12-13  4:09               ` Artur Malabarba [this message]
2014-12-14  3:57                 ` Stefan Monnier
2014-12-14 11:23                   ` Nicolas Petton
2014-12-14 13:47                     ` Stefan Monnier
2014-12-18 16:01                       ` Artur Malabarba
2014-12-18 19:12                         ` Stefan Monnier
2014-12-19  1:37                           ` Ted Zlatanov
2015-01-11 13:58                             ` Nicolas Petton
2015-01-12  5:28                               ` Stefan Monnier
2015-01-12  9:29                                 ` Nicolas Petton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAdUY-+Wd5_jZEW04atayjokMtGx5tdj04SuNvS_+gvE=xw4RQ@mail.gmail.com' \
    --to=bruce.connor.am@gmail.com \
    --cc=dak@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=stephen@xemacs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).