From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Ken Raeburn Newsgroups: gmane.emacs.devel Subject: Re: Any interest in a function like this to add to subr.el? Date: Wed, 19 Oct 2016 01:01:36 -0400 Message-ID: <3D8D2EC8-5492-4A3B-8766-E27C17C8537A@raeburn.org> References: <68978e0f-03a3-70ec-c18a-e2d12f91ea5c@yandex.ru> <6d541e6d-f7a1-beba-77f5-94d5a06f93cb@yandex.ru> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 (Mac OS X Mail 9.3 \(3124\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1476853335 5289 195.159.176.226 (19 Oct 2016 05:02:15 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 19 Oct 2016 05:02:15 +0000 (UTC) Cc: Emacs development discussions To: John Wiegley Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Oct 19 07:02:11 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bwj0g-0006fx-Tp for ged-emacs-devel@m.gmane.org; Wed, 19 Oct 2016 07:01:51 +0200 Original-Received: from localhost ([::1]:45541 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bwj0j-0006N0-4o for ged-emacs-devel@m.gmane.org; Wed, 19 Oct 2016 01:01:53 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54549) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bwj0b-0006Lp-0C for emacs-devel@gnu.org; Wed, 19 Oct 2016 01:01:46 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bwj0W-0000GE-B7 for emacs-devel@gnu.org; Wed, 19 Oct 2016 01:01:45 -0400 Original-Received: from mail-qk0-x234.google.com ([2607:f8b0:400d:c09::234]:35174) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bwj0W-0000F0-5o for emacs-devel@gnu.org; Wed, 19 Oct 2016 01:01:40 -0400 Original-Received: by mail-qk0-x234.google.com with SMTP id z190so18343834qkc.2 for ; Tue, 18 Oct 2016 22:01:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raeburn-org.20150623.gappssmtp.com; s=20150623; h=subject:mime-version:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=rHVfkDo3Z4dnverlWPor+vE4LiUiQgnZmyz7AClSytU=; b=aoeynN4Ux5eOxwOyu3OTuI2dQWzY4nGDKJRqxj5OLgNNuWINPG351CZMQSTfFLsMqP cEOURg5iXr/0N8iV+0k40wFzh6rbCA210+WzusxSalwmlQqj+KhK5Nt5sbGaE+WvHxj5 GYruBPiEyZjhO9G5hBrISFTtJaqILizUT/+z8Vmc9ra3vVvqcAAJMOUqfbDsFaJ27m4k Bbvn9ebHA1xx55Q5tWmhTks1p+fvR/E3zzTRx88AvPgQobiBqLNsKUgxYtx8IBJUolIL 6IkPBKOIEZ89N11LHllljT/KK7ZuLsklHpNQjGgTHJ1rd3ZfUudCNdpIPUxN3fROsEXS 34gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:mime-version:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=rHVfkDo3Z4dnverlWPor+vE4LiUiQgnZmyz7AClSytU=; b=R+2BDUzHStxu2qRWki7l7EWonROAziI/uqcKBl3Rixe0tniztjnblmhMY8DZoWUTTx Bv9TWQ+zrDwY4bkbq0YgxplEFxc06kxkscVlhyOGv6BrLL3Ev/eozWq9Y8Wd5o95X9Fj 9nmOSODYludrp/IlpJ9LRjZXsUrbd7mCtvATOMLBqaqpUXcQSg5qPnJ5Y1f1jIa8Yq9Y uIzh/xzUBjoIYjtHE/qBp8N/JZUlBl6wMm7LG0rj+g78ZBEAG1r+jHRcB2BNo0YgkAlI gL/XtmcP6qJZRLrINJwO9fh2jOX/WRW0YpHM4Bn7581tLW3GwYPukMiQprLCOAvUkrl+ yK+g== X-Gm-Message-State: AA6/9RlVb0divJwF4OHPLa7g+1v6GoSH9vywMVXXeJCBVvoCFXrqpQWI0OlI9fQQOlYPQg== X-Received: by 10.55.114.131 with SMTP id n125mr4098055qkc.17.1476853298979; Tue, 18 Oct 2016 22:01:38 -0700 (PDT) Original-Received: from [192.168.23.52] (c-50-138-183-136.hsd1.ma.comcast.net. [50.138.183.136]) by smtp.gmail.com with ESMTPSA id p58sm19913346qte.49.2016.10.18.22.01.37 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Oct 2016 22:01:38 -0700 (PDT) In-Reply-To: X-Mailer: Apple Mail (2.3124) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400d:c09::234 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 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:208453 Archived-At: > On Oct 18, 2016, at 17:25, John Wiegley wrote: >=20 >>>>>> "DG" =3D=3D Dmitry Gutov writes: >=20 > GD> Then I agree that we want a function like that. We'd want a = destructive > GD> version of it as well, though. >=20 > Yes, good idea! How about something like this: >=20 > (defun sort-on* (seq predicate accessor) > "Sort SEQ using PREDICATE applied to values returned by ACCESSOR. > This is a destructive version of `sort-on', which attempts to > reuse storage as much as possible." > (let ((seq2 seq)) > (while seq2 > (setcar seq2 (cons (funcall accessor (car seq2)) (car seq2))) > (setq seq2 (cdr seq2)))) Is there a shorthand function for this bit? I.e., mutate every element = of the list according to some supplied function, but reusing the same = list storage. If so, the body of sort-on* becomes about as short as = your original sort-on. Logically it=E2=80=99d be a variant of mapcar but =E2=80=9Cmapcar*=E2=80=9D= is something else=E2=80=A6 > (setq seq (sort* seq #'(lambda (x y) (funcall predicate (car x) (car = y))))) Is there any benefit to sort* instead of sort here? As far as I can = see, sort* just calls sort after (1) checking whether the list we just = iterated over is a list, and (2) checking whether we supplied a = =E2=80=9C:key=E2=80=9D keyword, which we didn=E2=80=99t. Some other observations: =E2=80=9Csort=E2=80=9D and =E2=80=9Csort*=E2=80=9D will work on vectors, = too, returning the now-sorted vector; your sort-on, using mapcar, will = make it into a list, and sort-on* will fail trying to apply car and cdr = to vectors. Were these intended to work on lists only? As far as your doc strings go, you say sort-on will apply the accessor = =E2=80=9Cat most once per element in the list=E2=80=9D, but it=E2=80=99s = going to be exactly once per element, isn=E2=80=99t it? The =E2=80=9Ccdr=E2=80=9D pass before returning could walk the = intermediate sorted list in the sort-on case too, making it more = space-efficient. Since the inner =E2=80=9Cmapcar=E2=80=9D call has = allocated us some unshared storage, we can just alter it in place and = return it to the caller. Ken=