* Consider adding hash-keys and hash-values
@ 2013-10-24 14:16 Bozhidar Batsov
2013-10-30 9:36 ` Bozhidar Batsov
0 siblings, 1 reply; 13+ messages in thread
From: Bozhidar Batsov @ 2013-10-24 14:16 UTC (permalink / raw)
To: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 696 bytes --]
Hi everyone,
I’ve seen the following functions (or their equivalents) inlined in many packages:
(defun hash-keys (hashtable)
"Return a list of keys in HASHTABLE."
(let ((keys '()))
(maphash (lambda (k v) (setq keys (cons k keys))) hashtable)
keys))
(defun hash-values (hashtable)
"Return a list of values in HASHTABLE."
(let ((values '()))
(maphash (lambda (k v) (setq keys (cons k values))) hashtable)
keys))
Is there any particular reason why we don’t have them as part of the standard set of hash functions? They are pretty useful and just about every programming language has them in its standard library.
--
Cheers,
Bozhidar
[-- Attachment #2: Type: text/html, Size: 1273 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-10-24 14:16 Consider adding hash-keys and hash-values Bozhidar Batsov
@ 2013-10-30 9:36 ` Bozhidar Batsov
2013-10-30 11:06 ` Nathan Trapuzzano
2013-10-30 13:11 ` Stefan Monnier
0 siblings, 2 replies; 13+ messages in thread
From: Bozhidar Batsov @ 2013-10-30 9:36 UTC (permalink / raw)
To: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 933 bytes --]
Any comments for or against the proposal? Should I could consider your
silence a sign of consent and submit a patch?
On 24 October 2013 17:16, Bozhidar Batsov <bozhidar@batsov.com> wrote:
> Hi everyone,
>
> I’ve seen the following functions (or their equivalents) inlined in many
> packages:
>
> (defun hash-keys (hashtable)
> "Return a list of keys in HASHTABLE."
> (let ((keys '()))
> (maphash (lambda (k v) (setq keys (cons k keys))) hashtable)
> keys))
>
> (defun hash-values (hashtable)
> "Return a list of values in HASHTABLE."
> (let ((values '()))
> (maphash (lambda (k v) (setq keys (cons k values))) hashtable)
> keys))
>
> Is there any particular reason why we don’t have them as part of the
> standard set of hash functions? They are pretty useful and just about every
> programming language has them in its standard library.
>
> --
> Cheers,
> Bozhidar
>
>
[-- Attachment #2: Type: text/html, Size: 1802 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-10-30 9:36 ` Bozhidar Batsov
@ 2013-10-30 11:06 ` Nathan Trapuzzano
2013-10-30 13:11 ` Stefan Monnier
1 sibling, 0 replies; 13+ messages in thread
From: Nathan Trapuzzano @ 2013-10-30 11:06 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: emacs-devel
Bozhidar Batsov <bozhidar@batsov.com> writes:
> Any comments for or against the proposal? Should I could consider
> your silence a sign of consent and submit a patch?
What about a third function that returns the key/value pairs as an
alist?
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-10-30 9:36 ` Bozhidar Batsov
2013-10-30 11:06 ` Nathan Trapuzzano
@ 2013-10-30 13:11 ` Stefan Monnier
2013-10-31 12:55 ` Bozhidar Batsov
2013-11-02 3:05 ` Josh
1 sibling, 2 replies; 13+ messages in thread
From: Stefan Monnier @ 2013-10-30 13:11 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: emacs-devel
>> (defun hash-keys (hashtable)
It would have to be "hash-table-keys".
But again, I'm not sure it provides a useful enough abstraction to
justify adding this one-liner in every Emacs executable.
Tho, maybe we could introduce a new file "oneliners.el" where we could
collect such functions (probably most/all defined as `defsubst' so that
you can (eval-when-compile (require 'oneliners)).
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-10-30 13:11 ` Stefan Monnier
@ 2013-10-31 12:55 ` Bozhidar Batsov
2013-10-31 13:37 ` Stefan Monnier
2013-11-02 3:05 ` Josh
1 sibling, 1 reply; 13+ messages in thread
From: Bozhidar Batsov @ 2013-10-31 12:55 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 619 bytes --]
Sounds reasonable. Maybe we can use another name, though? Something in the
lines of `helpers`, `extras`, etc.
On 30 October 2013 15:11, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> >> (defun hash-keys (hashtable)
>
> It would have to be "hash-table-keys".
>
> But again, I'm not sure it provides a useful enough abstraction to
> justify adding this one-liner in every Emacs executable.
>
> Tho, maybe we could introduce a new file "oneliners.el" where we could
> collect such functions (probably most/all defined as `defsubst' so that
> you can (eval-when-compile (require 'oneliners)).
>
>
> Stefan
>
[-- Attachment #2: Type: text/html, Size: 1064 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-10-31 12:55 ` Bozhidar Batsov
@ 2013-10-31 13:37 ` Stefan Monnier
2013-11-02 15:27 ` Bozhidar Batsov
0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2013-10-31 13:37 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: emacs-devel
> Sounds reasonable. Maybe we can use another name, though? Something in the
> lines of `helpers`, `extras`, etc.
I don't have a preference.
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-10-31 13:37 ` Stefan Monnier
@ 2013-11-02 15:27 ` Bozhidar Batsov
2013-11-03 2:38 ` Leo Liu
2013-11-03 14:00 ` Stefan Monnier
0 siblings, 2 replies; 13+ messages in thread
From: Bozhidar Batsov @ 2013-11-02 15:27 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
[-- Attachment #1.1: Type: text/plain, Size: 298 bytes --]
I've attached a patch for your consideration.
On 31 October 2013 15:37, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> > Sounds reasonable. Maybe we can use another name, though? Something in
> the
> > lines of `helpers`, `extras`, etc.
>
> I don't have a preference.
>
>
> Stefan
>
[-- Attachment #1.2: Type: text/html, Size: 719 bytes --]
[-- Attachment #2: 0001-lisp-helpers.el-Add-new-library-for-helper-functions.patch --]
[-- Type: application/octet-stream, Size: 2250 bytes --]
From df44b0223a67f519a61a54b39f34014374cd0760 Mon Sep 17 00:00:00 2001
From: Bozhidar Batsov <bozhidar@batsov.com>
Date: Sat, 2 Nov 2013 17:22:33 +0200
Subject: [PATCH] * lisp/helpers.el: Add new library for helper functions
---
lisp/ChangeLog | 4 ++++
lisp/helpers.el | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+)
create mode 100644 lisp/helpers.el
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 7716ed1..6558e5d 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,9 @@
2013-11-02 Bozhidar Batsov <bozhidar@batsov.com>
+ * helpers.el: Add new functions `hash-table-keys' and `hash-table-values'.
+
+2013-11-02 Bozhidar Batsov <bozhidar@batsov.com>
+
* emacs-lisp/package.el (package-version-join): Recognize
snapshot versions.
diff --git a/lisp/helpers.el b/lisp/helpers.el
new file mode 100644
index 0000000..203a378
--- /dev/null
+++ b/lisp/helpers.el
@@ -0,0 +1,40 @@
+;;; helpers.el --- Some non-essential library extensions
+
+;; Copyright (C) 2013 Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: convenience
+;; Package: emacs
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(defun hash-table-keys (hash-table)
+ "Return a list of keys in HASH-TABLE."
+ (let ((keys '()))
+ (maphash (lambda (k v) (setq keys (cons k keys))) hash-table)
+ keys))
+
+(defun hash-table-values (hash-table)
+ "Return a list of values in HASH-TABLE."
+ (let ((values '()))
+ (maphash (lambda (k v) (setq values (cons k values))) hash-table)
+ values))
+
+;;; helpers.el ends here
--
1.8.4
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-11-02 15:27 ` Bozhidar Batsov
@ 2013-11-03 2:38 ` Leo Liu
2013-11-03 7:28 ` Bozhidar Batsov
2013-11-03 14:00 ` Stefan Monnier
1 sibling, 1 reply; 13+ messages in thread
From: Leo Liu @ 2013-11-03 2:38 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: Stefan Monnier, emacs-devel
On 2013-11-02 23:27 +0800, Bozhidar Batsov wrote:
> I've attached a patch for your consideration.
Personally I think such one-liners won't be of much use. The designer of
the hash table API would have considered such use cases and decided to
rule them out for good reasons.
Maybe start out as your personal project and see how people react to it
first? WDYT?
Leo
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-11-03 2:38 ` Leo Liu
@ 2013-11-03 7:28 ` Bozhidar Batsov
2013-11-04 5:14 ` Leo Liu
0 siblings, 1 reply; 13+ messages in thread
From: Bozhidar Batsov @ 2013-11-03 7:28 UTC (permalink / raw)
To: Leo Liu; +Cc: Stefan Monnier, emacs-devel
[-- Attachment #1: Type: text/plain, Size: 1622 bytes --]
On 3 November 2013 04:38, Leo Liu <sdl.web@gmail.com> wrote:
> On 2013-11-02 23:27 +0800, Bozhidar Batsov wrote:
> > I've attached a patch for your consideration.
>
> Personally I think such one-liners won't be of much use. The designer of
> the hash table API would have considered such use cases and decided to
> rule them out for good reasons.
>
I think this is the wrong kind of reasoning. The fact that something exists
(or doesn't exist) doesn't mean necessary that it was carefully thought
through. In the end of the day everyone makes design mistakes from time to
time. I guess the original reasoning was to provide as minimalistic API as
possible (or more likely - the API was modelled after Common Lisp, which
doesn't feature those functions as well). APIs, however, should evolve and
designers should always take account the way the API is actually used by
its clients.
Common Lisp doesn't have those functions, either, but they're included in
the Alexandria library, that most Common Lisp projects use. The Common Lisp
core is beyond extension for various unfortunate reasons, but Emacs is
not...
As I've originally said - I can't recall another programming language that
doesn't have such methods in its standard library.
>
> Maybe start out as your personal project and see how people react to it
> first? WDYT?
>
I've seen these functions inlined quite often into Emacs packages -
obviously people use them. Don't think that a research paper is needed to
warrant the demand for their inclusion. :-) Plus, as Stefan said - other
non-essential utilities might be moved to the same library.
>
> Leo
>
[-- Attachment #2: Type: text/html, Size: 2607 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-11-03 7:28 ` Bozhidar Batsov
@ 2013-11-04 5:14 ` Leo Liu
0 siblings, 0 replies; 13+ messages in thread
From: Leo Liu @ 2013-11-04 5:14 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: Stefan Monnier, emacs-devel
On 2013-11-03 15:28 +0800, Bozhidar Batsov wrote:
> I think this is the wrong kind of reasoning. The fact that something exists
> (or doesn't exist) doesn't mean necessary that it was carefully thought
> through. In the end of the day everyone makes design mistakes from time to
> time. I guess the original reasoning was to provide as minimalistic API as
> possible (or more likely - the API was modelled after Common Lisp, which
> doesn't feature those functions as well). APIs, however, should evolve and
> designers should always take account the way the API is actually used by
> its clients.
>
> Common Lisp doesn't have those functions, either, but they're included in
> the Alexandria library, that most Common Lisp projects use. The Common Lisp
> core is beyond extension for various unfortunate reasons, but Emacs is
> not...
You know I am not disagreeing with the principles. Yes sure, it should
but few are prepared to go through the same rigorous process as the
forerunner did.
Just saying...
Leo
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-11-02 15:27 ` Bozhidar Batsov
2013-11-03 2:38 ` Leo Liu
@ 2013-11-03 14:00 ` Stefan Monnier
2013-11-04 11:47 ` Bozhidar Batsov
1 sibling, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2013-11-03 14:00 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: emacs-devel
> I've attached a patch for your consideration.
Looks good, tho I'd use `push' in there, and please define them
as defsubst.
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Consider adding hash-keys and hash-values
2013-10-30 13:11 ` Stefan Monnier
2013-10-31 12:55 ` Bozhidar Batsov
@ 2013-11-02 3:05 ` Josh
1 sibling, 0 replies; 13+ messages in thread
From: Josh @ 2013-11-02 3:05 UTC (permalink / raw)
To: Stefan Monnier; +Cc: Daniel Colascione, Bozhidar Batsov, emacs-devel
On Wed, Oct 30, 2013 at 6:11 AM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>>> (defun hash-keys (hashtable)
>
> It would have to be "hash-table-keys".
>
> But again, I'm not sure it provides a useful enough abstraction to
> justify adding this one-liner in every Emacs executable.
>
> Tho, maybe we could introduce a new file "oneliners.el" where we could
> collect such functions (probably most/all defined as `defsubst' so that
> you can (eval-when-compile (require 'oneliners)).
In my experience the usual reason to want a set of hash table keys
or values is to iterate over them. Perhaps this would be a good
time to revisit last year's discussion[0] of Daniel's elisp-generators
library[1]?
[0] http://lists.gnu.org/archive/html/emacs-devel/2012-11/msg00502.html
[1] https://github.com/dcolascione/elisp-generators
Josh
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2013-11-04 11:47 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-24 14:16 Consider adding hash-keys and hash-values Bozhidar Batsov
2013-10-30 9:36 ` Bozhidar Batsov
2013-10-30 11:06 ` Nathan Trapuzzano
2013-10-30 13:11 ` Stefan Monnier
2013-10-31 12:55 ` Bozhidar Batsov
2013-10-31 13:37 ` Stefan Monnier
2013-11-02 15:27 ` Bozhidar Batsov
2013-11-03 2:38 ` Leo Liu
2013-11-03 7:28 ` Bozhidar Batsov
2013-11-04 5:14 ` Leo Liu
2013-11-03 14:00 ` Stefan Monnier
2013-11-04 11:47 ` Bozhidar Batsov
2013-11-02 3:05 ` Josh
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).