From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Andrea Corallo Newsgroups: gmane.emacs.devel Subject: Re: new function proposal alist-to-hash Date: Sat, 05 Oct 2019 08:18:19 +0000 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="212604"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (berkeley-unix) Cc: emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Oct 05 10:19:01 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iGfHJ-000tA3-Al for ged-emacs-devel@m.gmane.org; Sat, 05 Oct 2019 10:19:01 +0200 Original-Received: from localhost ([::1]:54762 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iGfHI-0001Jh-0R for ged-emacs-devel@m.gmane.org; Sat, 05 Oct 2019 04:19:00 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58134) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iGfGm-0001JY-Qi for emacs-devel@gnu.org; Sat, 05 Oct 2019 04:18:29 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iGfGj-0005Lu-4k for emacs-devel@gnu.org; Sat, 05 Oct 2019 04:18:28 -0400 Original-Received: from ol.sdf.org ([205.166.94.20]:61420 helo=mx.sdf.org) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iGfGi-0005LE-UE for emacs-devel@gnu.org; Sat, 05 Oct 2019 04:18:25 -0400 Original-Received: from sdf.org (IDENT:akrl@sverige.freeshell.org [205.166.94.5]) by mx.sdf.org (8.15.2/8.14.5) with ESMTPS id x958IJIh025099 (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256 bits) verified NO); Sat, 5 Oct 2019 08:18:19 GMT Original-Received: (from akrl@localhost) by sdf.org (8.15.2/8.12.8/Submit) id x958IJo6002718; Sat, 5 Oct 2019 08:18:19 GMT In-Reply-To: (Stefan Monnier's message of "Fri, 04 Oct 2019 15:16:13 -0400") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 205.166.94.20 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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" Xref: news.gmane.org gmane.emacs.devel:240602 Archived-At: Stefan Monnier writes: >> - I think is quite useful to be able to create in a concise and >> explicit way nested hash tables. This is a common feature of many >> "modern" languages. >> Here both solutions compared: >> >> (alist-to-hash '((a . x) >> (b . ((i . j) >> (k . l))) >> (c . y))) >> >> (map-into `((a . x) >> (b . ,(map-into '((i . j) >> (k . l)) >> 'hash-table)) >> (c . y)) >> 'hash-table) > > Not sure I understand: if you only use it for immediate/literal data, > then I guess you could just use the #s(hash-table data (...)) syntax. Sure, my example was just to point out easiness of use from a syntactic point of view. The good of having the list quoted by the user is that he can quasi-quote when needed what he needs. > And for non-literal maps, this needs to somehow distinguish values that > are alists from others, like > > (map-into (mapcar (lambda (x) > (if (and (consp (cdr x)) (consp (cadr x))) > (cons (car x) (map-into (cdr x) 'hash-table)) > x)) > my-alist) > 'hash-table) > > But I suspect that this is not frequently needed. > [ BTW, the above code is screaming for something like `map-values-apply` > but which returns a *map* rather than a list. ] > > And if you really need this to apply recursively, it basically means > you don't have a map but a *tree* where each node is originally > implemented as an alist and which you want to transform into the same > tree where each node is now a hash-table. Again, this is likely not > needed very frequently (and I suspect that each time it's needed, it > will have slightly different needs/constraints). I've maybe used not the correct nomenclature sorry. What I want to say is that being an hash table a key value map it maps 1:1 into alist. That said I think is quite important to be able to express in a clear and short way nested hashes. In python it would be simply something like this: nested_dict = { 'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}} Note that here just litteral are used but also variables can be evaluated while creating the dictionaries. A tree of a-list for the reason I've expressed above would be to me the most natural way to express the same. Given that a recursive implementation that walks this tree like the one in patch I've posted can do the job. Maybe I'm the only that see a value on that but I think is useful :) >> - map-into does not let you tweak make-hash-table parameters. >> This is especially a limitation regarding :test so is effectively a >> solution to say ~50% of the use cases. > > Yes, this is a very serious limitation of `map-into`. > I decided not to try to tackle it when I converted map.el to use > cl-defmethod, but I'd welcome help on this. > > > Stefan Ok I'm looking into it. Bests Andrea -- akrl@sdf.org