From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Klaus-Dieter Bauer Newsgroups: gmane.emacs.devel Subject: Re: RFC: make maphash return a list Date: Tue, 18 Jun 2013 08:17:42 +0000 (UTC) Message-ID: References: <87txmjcnz5.fsf@uwakimon.sk.tsukuba.ac.jp> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1371543923 25408 80.91.229.3 (18 Jun 2013 08:25:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 18 Jun 2013 08:25:23 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Jun 18 10:25:24 2013 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UorEB-0004hz-1E for ged-emacs-devel@m.gmane.org; Tue, 18 Jun 2013 10:25:23 +0200 Original-Received: from localhost ([::1]:48424 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UorEA-00075k-IE for ged-emacs-devel@m.gmane.org; Tue, 18 Jun 2013 04:25:22 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UorE2-00075F-VV for emacs-devel@gnu.org; Tue, 18 Jun 2013 04:25:19 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UorE1-0005UX-CV for emacs-devel@gnu.org; Tue, 18 Jun 2013 04:25:14 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:49572) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UorE1-0005US-67 for emacs-devel@gnu.org; Tue, 18 Jun 2013 04:25:13 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1UorDq-00047Q-TY for emacs-devel@gnu.org; Tue, 18 Jun 2013 10:25:07 +0200 Original-Received: from 178-191-5-167.adsl.highway.telekom.at ([178.191.5.167]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 18 Jun 2013 10:25:02 +0200 Original-Received: from bauer.klaus.dieter by 178-191-5-167.adsl.highway.telekom.at with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 18 Jun 2013 10:25:02 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 94 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 178.191.5.167 (Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:160556 Archived-At: Wilfred Hughes wilfred.me.uk> writes: > > > > > > maphash is nice because it doesn't cons a list. > So you're saying that maphash returning nil is a desirable property because it doesn't build a list if you don't want it? OK, I understand. I think it might be helpful for the docstring of maphash to say 'for side effects only' the same way mapc does. My (incorrect) intuition was that any mapFOO function would return a sequence unless otherwise stated.> (defun choose-name-to-taste (callable table) > >   "Left as an exercise for the reader." > >   (let ((retlist nil))>    (maphash (lambda (key value) > >               (setq retlist (cons (funcall callable key value) retlist)))>             table))>    retlist)) > > Yep, exactly. I don't want to turn this into bikeshedding -- I only felt it was a nice to have, and of course I can build a list myself. Since both mapcar and mapc exist in core Emacs, would it be worthwhile to have a blessed chose-name-to-taste implementation in core?> Isn't this [adding functionality to loop] the right place to improve elisp? > > That's an excellent idea, I'll try to put together a patch for that. > > Thanks for your feedback, I appreciate it. > Wilfred > > “maphash is nice because it doesn't cons a list.” While I understand the performance considerations (regarding possibly high memory use in some cases), I find having some variant that DOES return values producing less readable code. If all “map...” functions returned values unless explicitly told not to this would allow for a more consistently functional style of programming. I'm not sure how style conventions usually are for emacs lisp, but personally I find code much more readable that avoids changing the value of variables. E.g. I found my own code easiert to read and expand if I can do a list-operation like (mapcar 'modification-2 (mapcar 'modifification-1 (maphash (lambda (k v) (return-something k v))))) compared to (let (mapped-hash) (maphash (lambda (k v) (push (return-something k v) mapped-hash))) (mapcar 'modification-2 (mapcar 'modification-1 mapped-hash))) For full backward compatibility (some code may depend on maphash returning nil) and preserving the performance advantage, why not introduce an optional argument to all those builtin “map...” functions or an alternative version that returns the values (sort of like the mapc, mapcar pair). As for cl-loop: I use that macro alot. However, it is unnecessarily limiting, as there isn't a corespondence for all “map...” functions that don't return values. Then you're back to some (let (x) (map... (lambda (..) (push .. x))) x) again in order to get the input-value for the loop. Also, when I see “mapcar” I know a list will be returned -- with “cl-loop” there is no such clarity. Also, unlike general “map...” functions, cl-loop is like a separate language to be learned. While of course everyone can define a “hashmap” function as described or similiarily a “atommap” or “keymapmap”, I'd expect code-readability to suffer from having custom functions for such things, unless they are implemented in a widely used package like “cl-lib”, though then again, such functions wouldn't have anything todo with common lisp. To be fair though, the standard indentation scheme doesn't promote this style of programming either. In standard indentation, the above example would read either (ignoring that maphash doesn't return the list of values) (mapcar 'modification-2 (mapcar 'modifification-1 (maphash (lambda (k v) (return-something k v))))) or (mapcar 'modification-2 (mapcar 'modifification-1 (maphash (lambda (k v) (return-something k v))))) kind regards, Klaus