From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Drew Adams Newsgroups: gmane.emacs.help Subject: RE: [External] : How to create a higher order function? Date: Tue, 21 Sep 2021 15:49:02 +0000 Message-ID: References: <87k0jawotx.fsf@mbork.pl> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31329"; mail-complaints-to="usenet@ciao.gmane.io" To: Marcin Borkowski , Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Sep 21 17:51:03 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSi2w-0007xC-Uy for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 21 Sep 2021 17:51:03 +0200 Original-Received: from localhost ([::1]:60240 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mSi2v-0002ur-Cl for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 21 Sep 2021 11:51:01 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38594) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSi1D-0001bV-Rc for help-gnu-emacs@gnu.org; Tue, 21 Sep 2021 11:49:16 -0400 Original-Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:48750) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSi1B-0007CY-AS for help-gnu-emacs@gnu.org; Tue, 21 Sep 2021 11:49:15 -0400 Original-Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 18LEY3CD017241; Tue, 21 Sep 2021 15:49:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=corp-2021-07-09; bh=mu8ScDmSVZyDHtn2o7XIK8mimOPCN2o8ZWWeLZRivJ8=; b=RHEIC/a9iXklYP7odOwFK0s+MRCaniiXSWpBrRXP1XSKlKzLyQgjN+Zv/WDt9iQQTF8l t9W5mJu79VVyuI6qjOcuchnoy4awAsg4kzbZqIVG3WrkBEOHRQlZ2sLtakKF+E7BoOYT FSEVocS433+eoSOjtdYIzmfKEdw0grezzb11tI4VPcFuO6ckJaqvenPmKOMtmLgP4t4G nXnmzqE0FFk6G2wrFOmGwvOSZ/O+sTf3dhhJLWHz5Uv0FZL5IduGpuUZtncCFFJToaLO sviJLMAFqS/cuadHwFVJRoakO5Si/PAIGh7jZPoIzs29KrPfvG6Yj70Sg7N7jZ4M/Se4 IQ== Original-Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3b79adjkmv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 21 Sep 2021 15:49:07 +0000 Original-Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 18LFR46f046386; Tue, 21 Sep 2021 15:49:06 GMT Original-Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam08lp2049.outbound.protection.outlook.com [104.47.73.49]) by aserp3020.oracle.com with ESMTP id 3b57x5pfeu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 21 Sep 2021 15:49:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IuXZIOp8uBWW7/FMg4lNT85ouMc9c9irBTXIFVtzEc8IYssozEcGfcqNLTRVgm9j1KKJkKskrFx6bnJ5O5HndQeD6YOGa3Qm+kK93/AQuYfgw1k/EVDBWMgV+NXqU9/xlPbAsgbNW5FDsjQrnVANZZnuLsqCATyLf4fkVo1aI+TqSx/ob9bWGnlXS/mfbO2E2OrV2zmcZW9ZW/eGvbkIxVTlQRWbZtMzcZic3NOX8PBvJupfzVAd0Kk1SNYMamIisU5MASfAcK3smYEYqJkYzW9bx5KjYqpCwIjdsVttgLbz9ArPFS/g5VS8510KrhTQxYJmX3CZKWZCWHkUB2V/Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=mu8ScDmSVZyDHtn2o7XIK8mimOPCN2o8ZWWeLZRivJ8=; b=kgmmsIwKnJKh7cTKmbsftE64Ms/9+d16g18MA3wYlIkC/wecQkAn6/EPJWcknu5V+Ov7Ob76aIL6N5l8MvLf3hVodd5WmEgWHrUxKJNUWofd1tihZYLX/EwJnJqLp7xmIBGP3xB8pcYD2TCcYoG9HjWl49VCpBxDLx+xdgZ1uFPi+8Bq+09WFOj1xjaWm/SgoZDbwI2K64emdPfPA+ITHuLUDvkXCK9fQ7MvamyFPXfbanTdBinqOVQOUc5zY+g6W0VmcnkWBjmpWE2ytEEsKEfYsS1Up67IodCh+/Oifhp7DYEfYTeYVNg5a3Q7vJ42h/eiD3tDfOC/FzYxSLEq2w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mu8ScDmSVZyDHtn2o7XIK8mimOPCN2o8ZWWeLZRivJ8=; b=i8DsBb2mKaXRqUE8txQQc36iAfblFTbEFIl8aerg/FuSusVDEMS/lhKtTrlnwBEvoxBrv9wBQYVAQ0ryfRp82+BuYQKVK1qjY5EKCTxYc5I63UECDydg5bMZs6Yhvsd+oGytg6sKfqIUmLZ0eDBGJKvWZ3NwKNKIWhYH1wSqsmw= Original-Received: from SJ0PR10MB5488.namprd10.prod.outlook.com (2603:10b6:a03:37e::19) by BYAPR10MB2871.namprd10.prod.outlook.com (2603:10b6:a03:83::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.16; Tue, 21 Sep 2021 15:49:03 +0000 Original-Received: from SJ0PR10MB5488.namprd10.prod.outlook.com ([fe80::c0bc:7c3a:292f:8a82]) by SJ0PR10MB5488.namprd10.prod.outlook.com ([fe80::c0bc:7c3a:292f:8a82%9]) with mapi id 15.20.4523.018; Tue, 21 Sep 2021 15:49:03 +0000 Thread-Topic: [External] : How to create a higher order function? Thread-Index: AQHXrp66HSec3QYbx0q7374ONlsnKauumAfQ In-Reply-To: <87k0jawotx.fsf@mbork.pl> Accept-Language: en-US Content-Language: en-US authentication-results: mbork.pl; dkim=none (message not signed) header.d=none;mbork.pl; dmarc=none action=none header.from=oracle.com; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 5d5d8196-5413-44b0-386e-08d97d17560e x-ms-traffictypediagnostic: BYAPR10MB2871: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:8882; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: y477uwSc+m9xPJY+Uk9EUGhG9o7LzJ7LfSfCZN8e2t4J/Ef4+2vriSTVZBH08UujuUXSjA356k3/86j+MXsfcsdoTLlYRvdPZWie/ynGeI4PUKjZa7j1v5Obsy88puSs38KHpqU7poKnL3bMzKLTEMzaSCoMil/iTtCQzdW2l0Yf5ZH7v6cePbLfGt2JtiMyy9679dzcIe1DPziJgu1iYTPIKJn/8Qt0Aby4a7C3K3H/YGefTQGdsf+iSdNoBuM29zoSmf21U1xjqqhPuVRkE3MV8TQWZlD47c1M1e1gMcNRzi6Yi/RBNSEpDEIwFFyIqXIn8jp04fnRWonfGWcVPwBR++Rl6B+EXMqy7yaeEbc19wVe9tmqL6jWv7gdzOg4Q2SNO7lPuowh516qn3QtcJSL6eyERDTjp0fWzq9xUqoEA3TwhRWdMuw27la71nsFCVcKl1kba0Gl65hAV73ijlRLYcGrO87/OOMqaZYKMX0RYL2bPOA8bM53AohPpPNK3RGp2hVjtVRt6EzfqX3J2/kP9VmOrZDJFci2A/RnQaz31FAuSlC6GvIjrFvdv/6FBY9uuwUO7nL19eid3nOWcmMTQ/jwZLnPK6gRNSCYSqg4C1rsQmf9v2AuAYPzmEHa2YQ/R209OLBf/nDklwMSqUNvqdFY1iPaYHpleaw9451wwjBVolVPd/89zQl5GUTrMOK4fAXbugtOZBV7fSoUQX+JVkMRY+tl7Y+BUMA4fOynwLh8wxoZHT8ZG5m1n 0UcjJDUwHG+jikG0+5wcNivcpnQkykicpWCIt8cN+6OdcBYmVQaOLmM+0xtLt83tU/xvG6rkhXUCNASh88IdOUVVQ== x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SJ0PR10MB5488.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(366004)(38070700005)(5660300002)(66556008)(6506007)(66946007)(66476007)(52536014)(8936002)(316002)(8676002)(7696005)(64756008)(66446008)(186003)(122000001)(76116006)(110136005)(33656002)(38100700002)(86362001)(83380400001)(26005)(2906002)(44832011)(508600001)(966005)(9686003)(55016002)(71200400001); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?NVqxq53Wzv8OtflgsOo+Y+Y0JgiKnQh2cLAn3pe9/w7AU+05Rl+G6+z2Ypfd?= =?us-ascii?Q?v9bfX0NUElnEW5444hyFxRC2otWQnHDYKgTC3xvigbIvkKKmyiJhPmmieaQj?= =?us-ascii?Q?5wF0YBhsBooF04u/jg5Ykhx/v2n8uyOPW7T6JYgwulwP3qQXtGRTaTgAxAzL?= =?us-ascii?Q?BCgYJ5wLgVy+9srCJODpsrbPtmrr4GpSVqcQOsREexuiFkdhq0k3ZGwx6lrK?= =?us-ascii?Q?aNSAzXsF3UwvypK4TRaEBIh+QBUo8FMeJhZt5jaB2aCt93+GQgdwODn5YQJL?= =?us-ascii?Q?zkD0I0TrczA92ojUKqDCw29QaR3C/9xLUhcnSLfhjYQ4A9L2wSwsrUXFN8Wk?= =?us-ascii?Q?On8m/STYrU34nmSqJegZenPyHeaL9cISuWfzsD0pxmKrxrrhp7YTav5ZUg/2?= =?us-ascii?Q?JHuyrwPz4NuNpSZe8Qr3i/dZi4C2vHx5w0Y9P31v2F3yZpWX7UvLSciOsV9o?= =?us-ascii?Q?AOTk4waNPLRmeHQQmMYPqDcUxuytJyI5tMwKABzm4ef56o894l33mSpUgfdI?= =?us-ascii?Q?jr+Vwsu2p0CI2TtZcK7r4GVR7LgxXdjNgYpzAApRa1V4rMU9wFdgnVLzOUCx?= =?us-ascii?Q?Ogg13dgXmlevvJYrV7fjENZv8ivViJg28vzvQRaLSleHhlb+Db/aq59xSErP?= =?us-ascii?Q?vB x-ms-exchange-transport-forked: True X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5488.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5d5d8196-5413-44b0-386e-08d97d17560e X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Sep 2021 15:49:02.9803 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: OCKNkALeuM3mZRi135UA0iEr8UDRCuWwyRdqUmbtv4mk98ZtRvgp3zlJMrtegNf+J/PGE6TwvW8KWuSkMimxsA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR10MB2871 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10114 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxscore=0 adultscore=0 phishscore=0 spamscore=0 bulkscore=0 mlxlogscore=999 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109030001 definitions=main-2109210095 X-Proofpoint-GUID: DZIFkA2B7TS2tyYkDBjKXXRoFsLU2Sa9 X-Proofpoint-ORIG-GUID: DZIFkA2B7TS2tyYkDBjKXXRoFsLU2Sa9 Received-SPF: pass client-ip=205.220.177.32; envelope-from=drew.adams@oracle.com; helo=mx0b-00069f02.pphosted.com X-Spam_score_int: -7 X-Spam_score: -0.8 X-Spam_bar: / X-Spam_report: (-0.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URI_DOTEDU=1.999 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:133138 Archived-At: > My question is: which of (B) and (C) is better? (C) is definitely > simpler, and with lexical scope becoming the default (perhaps at some > point in time in the future) or the recommended (even now, I guess) > setting, is probably the way to go. But maybe (B) is better under some > circumstances? Is it faster? (I don't think so, and my simple > benchmarks were inconclusive.) Is it more memory-efficient? (Could > be.) Does it have any other advantage? (C) is definitely better, in general. (That's really the takeaway answer.) The lexical environment, i.e., the one where the function is defined, is the environment you generally want for the function - it's, well, the environment that _defines_ the function. Generally speaking that's the only proper environment for defining the function. Of course, this is Lisp, and nothing says you're obliged to use the "proper", static, definition of anything. ___ Two counter cases to (C), of sorts: 1. If you specifically want to allow other code to be able to _change the behavior_ of the function by simply providing an arbitrary runtime value for some variable, then you might want to dynamically bind that variable. 2. If a free variable in the function body is never used _as a variable_, so that only its _value_ at the point of the function definition is needed, then you can do what you did, which is to just substitute the value for the var. This means there's no carrying around that var binding in the closure, no need to look up its value, etc. But see next: that generally doesn't mean a benefit in performance - the contrary. Wrt #2: Be aware that, at least with current Elisp and its byte-compiler, #2 essentially _loses_ the function definition at the point where it's defined. Instead, it substitutes a _list_, a constructed lambda form, for the function. It's only later, in some other context/environment, that that list can get interpreted as a function. Among other things, this means that that representation of a function can't be compiled as a function. It can only be interpreted (any number of times, in any contexts). It can't in any way be treated or understood by Emacs as a _function_ - it's just a list with a lambda form, until it's eval'd. Put differently, it's just a _mention_ till it's explicitly _used_. It's a template function definition, to be filled in and interpreted as a function wherever and whenever. On the other hand, for better and worse, it can be treated as a list (it is one). IOW, instead of a function, you have essentially the _name_, a representation, of a function. If you want to do things to or with such a representation, you can. But this is rare. ___ Wrt the advantages and uses of dynamic binding in the context of _Emacs_ - i.e., the reason why Emacs has, and should continue to have, dynamic binding (along with lexical binding), see RMS's arguments here: https://www.gnu.org/software/emacs/emacs-paper.html#SEC17 https://www.gnu.org/software/emacs/emacs-paper.html#SEC18 Those arguments are as valid today (& tomorrow) as they were when written in 1981. Wrt the uses and behavior of dynamic & lexical binding in _Lisp_ (not particular to Emacs), I recommend reading the relevant parts of "Common Lisp The Language". https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node43.html Here's my SO post on reading that section in relation to Emacs: https://stackoverflow.com/a/7135315/729907