From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.devel Subject: Re: [SUSPECTED SPAM] Re: `thunk-let'? Date: Thu, 09 Nov 2017 15:34:30 +0100 Message-ID: <87efp7cyh5.fsf@web.de> References: <87infp9z6j.fsf@web.de> <87zi90eehg.fsf@web.de> <87o9ocd6s4.fsf@web.de> <83zi7wr6jc.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1510238101 2293 195.159.176.226 (9 Nov 2017 14:35:01 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 9 Nov 2017 14:35:01 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: nicolas@petton.fr, monnier@iro.umontreal.ca, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Nov 09 15:34:54 2017 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 1eCnuv-00005A-AX for ged-emacs-devel@m.gmane.org; Thu, 09 Nov 2017 15:34:53 +0100 Original-Received: from localhost ([::1]:37205 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eCnux-000195-4E for ged-emacs-devel@m.gmane.org; Thu, 09 Nov 2017 09:34:55 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55041) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eCnum-00018f-0I for emacs-devel@gnu.org; Thu, 09 Nov 2017 09:34:49 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eCnul-0007fU-0i for emacs-devel@gnu.org; Thu, 09 Nov 2017 09:34:44 -0500 Original-Received: from mout.web.de ([212.227.17.11]:52744) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eCnuh-0007dY-5G; Thu, 09 Nov 2017 09:34:39 -0500 Original-Received: from drachen.dragon ([94.217.120.125]) by smtp.web.de (mrweb102 [213.165.67.124]) with ESMTPSA (Nemesis) id 0MGzXS-1eQX931VNi-00DrJP; Thu, 09 Nov 2017 15:34:31 +0100 In-Reply-To: <83zi7wr6jc.fsf@gnu.org> (Eli Zaretskii's message of "Wed, 08 Nov 2017 20:04:23 +0200") X-Provags-ID: V03:K0:EDFk1FYwXXL1BrnA7jrnCr8qRH/lYTYdhEZx/vBkw/dkNCbV7Ni vfN1b/aESubx/+JKessMs6GXj4Ww07eyVkTA4BtM0VkeaaMrexifAKvaNf4y/uNshiGFdgz kPB1vBSri1IaTVtTDX2PfMLBHAoa+aAeIjHRtdaZep++btFEx0Mjpr7tifHkSnQgS9pvG5Z KGOjHPqNS/HJJDVzULNlw== X-UI-Out-Filterresults: notjunk:1;V01:K0:JqNzqiZOQyI=:3U53fEhdG4hO8v24ZJqY+G aoYkYVeDHhAvGHWQq1JsdJ9hFWCekrVeiGtGqRk+3aMbPrZGU1LmG90Ca9ln1cLjpAyQqiKDQ PUjOuqNG+yeCaqpP5i3AmztuYFa9nb6xBHU2L42zN27tesPpqB1PFb0coHB79jGm3GwOtlulh HFVDwwsHhLN4EQufQ9+l+iP7hZbbQfH2Cpm5u8wsAhblBfxDMKNPemxw3tA9p94i/dcI2BrJF vcuScX/OncSoZwpbSkOY2l6LBestrYu4kswoPYQa8lZWPlmRlDZ+ReIpw6uzUOs8urQCNWlpb rWThxkdwOL8bq4ruVdEigfZSQhAJxcIbGLhicWfqBKcq4svlQ4vRnB1hzckDeW0sLL1HCw9Nl EpnDZK6RGFXsFSCo9I6gZa47Qc+JyAwH5UTbnB6ktEIgZUJxXK4HYiO+S2knmi7+E9aHFzq8q XJcz4WgQT6NYEv6Em+8oxFuJGOcrIQjjobkt8ACbqaJvdp7UJGwAhCv06SGXRaXv4gyCegBMb 5b0Ebp8/nRit9uKUfOKMTunnz4DpquHADpnkhO+WaeOsjliLU1nXZETQdmxIg9Twym3jUtdcd 98mEcgu//hqubv1HWNu/oFzg0U0Gqif6YhczVGlcvNdqTgMsI/TBiTCVf98P8MsDufMOITNy4 rNPg67mtEMRTJQv5q5O8zBBW84olLgBfx4GYAb8RZPeMjPojPUzm45W2ShIQDnEPjIuiVCjX0 8OXS6DrxmPSruwfPHFCIeF6nIfQnZJgwv66c0GQkEv55IJpvW/S3gnPuhoUnZASrSAayPfTG X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.17.11 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:219991 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: > Given that "lazy evaluation" seems not to be described anywhere in the > ELisp manual, I think we cannot get away with "---" here, and will > have to add at least something to the manual. Ok, here is a first draft of how I could imagine an addition to the manual (as plain txt): --=-=-= Content-Type: text/plain Content-Disposition: inline; filename=lazy-let-doc.txt Deferred and lazy evaluation Sometimes it is useful to delay the evaluation of an expression, for example if you want to avoid to perform a time-consuming calculation in the case that it turns out that the result is not needed in the future of the program. -- Macro: thunk-delay FORMS... Return a thunk for evaluating the FORMS. A thunk is a closure that evaluates the FORMS in the lexical environment present when `thunk-delay' has been called. -- Function: thunk-force THUNK Force a thunk to perform the evaluation of the FORMS specified to the `thunk-delay' that created the thunk. The result of the evaluation of the last form is returned. The THUNK also "remembers" that it has been forced: Any further calls of `thunk-force' on the same THUNK will just return the same result without evaluating the FORMS again. -- Macro: lazy-let (bindings...) forms... This macro is analogue to `let' but creates "lazy" variable bindings. Any binding has the form (SYMBOL VALUE-FORM). Unlike `let', the evaluation of any VALUE-FORM is deferred until the binding of the according SYMBOL is used for the first time when evaluating the FORMS. Any VALUE-FORM is evaluated maximally once. Example: (defun f (number) (lazy-let ((derived-number (progn (message "Calculating 1 plus 2 times %d" number) (1+ (* 2 number))))) (if (> number 10) derived-number number))) (f 5) ==> 5 (f 12) |--> "Calculating 1 plus 2 times 12" 25 Because of the special nature of lazily bound variables, it is an error to set them (e.g. with `setq'). -- Macro: lazy-let* (bindings...) forms... This is like `lazy-let' but any expression in BINDINGS is allowed to refer to preceding bindings in this `lazy-let*' form. Example: (lazy-let* ((x (prog2 (message "Calculating x...") (+ 1 1) (message "Finished calculating x"))) (y (prog2 (message "Calculating y...") (+ x 1) (message "Finished calculating y"))) (z (prog2 (message "Calculating z...") (+ y 1) (message "Finished calculating z"))) (a (prog2 (message "Calculating a...") (+ z 1) (message "Finished calculating a")))) (* z x)) |--> Calculating z... |--> Calculating y... |--> Calculating x... |--> Finished calculating x |--> Finished calculating y |--> Finished calculating z ==> 8 `lazy-let' and `lazy-let*' use thunks implicitly: their expansion creates helper symbols and binds them to thunks wrapping the binding expressions. All rerences to the original variables in the FORMS are then replaced by an expression that calls `thunk-force' on the according helper variable. So, any code using `lazy-let' or `lazy-let*' could be rewritten to use thunks, but in many cases using these macros results in nicer code than using thunks explicitly. --=-=-= Content-Type: text/plain WDYT? I would definitely need a bit of help to turn this into texinfo with good Enlish. Thanks, Michael. --=-=-=--