From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Garklein Newsgroups: gmane.emacs.devel Subject: Re: Elisp threading macros Date: Wed, 28 Aug 2024 15:43:51 -0400 Message-ID: References: <87v7zksety.fsf@posteo.net> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11505"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Philip Kaludercic Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Aug 29 06:33:29 2024 Return-path: Envelope-to: ged-emacs-devel@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 1sjWqd-0002lx-Po for ged-emacs-devel@m.gmane-mx.org; Thu, 29 Aug 2024 06:33:27 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sjWpx-0000WE-F4; Thu, 29 Aug 2024 00:32:45 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sjOaM-0004uN-Ne for emacs-devel@gnu.org; Wed, 28 Aug 2024 15:44:06 -0400 Original-Received: from mail-io1-xd2c.google.com ([2607:f8b0:4864:20::d2c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sjOaL-000125-0N for emacs-devel@gnu.org; Wed, 28 Aug 2024 15:44:06 -0400 Original-Received: by mail-io1-xd2c.google.com with SMTP id ca18e2360f4ac-81f96ea9ff7so354403239f.3 for ; Wed, 28 Aug 2024 12:44:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724874243; x=1725479043; darn=gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=T8PMlIVTIJeeigBbExl0mccIipsQkzXWThpqxINH/NY=; b=GsFzdUuM0y+OszXbQP9kpk8bm+w+qbpJt3jpsaNcUYoPlUzfjngPldxHWC1ij1SuhD zgqFoMyJ6+Awb6ozf5CzFZr2dxBtJhWrKY+9PGVJaW8giE84/MAI7yoCJftCZcdLYqZc giuDK0ysy3GmiUKft6Oef9xe66N81xOOFKHyUF6kb3oDwAyckADMK9r1jjTeNSq82K4A Ty1WhkK2vxkzYcDq6OaXAjWHPpR7b+qOS/75ejVbf3f9r9i0WnAMLtLA+QahNfg6wzd/ m7Kg6ctfHaI9dhau0ZSze2SA6zmpwlo3FVYKe+2s3AO/ghLoGJX9qSxtu0F19z92WDhU acOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724874243; x=1725479043; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T8PMlIVTIJeeigBbExl0mccIipsQkzXWThpqxINH/NY=; b=b2WNuU1TZGJL1pOvuzJqCjQrvs5s408DEdi9cEnRAK2mlet4tosJQnU+ozfcXQPwi+ mIApDKq3GJfddtueGZhnuzNFRdKCw1DpAl80/65ScYjkX3+X/NmPU5RS4ITSRNAvUtan TfPffMVm19nDlcIsrWQBpNiqyxk7A3g5puEicEuZdM+DGjFH0XDb7irrX1mfzWefLZqq TtQfbeHSu+9BK8wlKLkhKOrkzFkX1qGLvFp6uz7btQZ6ZmU06eUkC6/DVwrPvOML686w iIvGMhzzyAn8rFJ+bBxMJD/+738Ho0y80s62Txf55JDfzHJPRPWogTZlLkwldWiLKooV xF0g== X-Gm-Message-State: AOJu0Ywgt0uoSIS33UT16uPBJHNizZQH6Kawx1cOvMzdo1G3pMY6I/43 efhSIgk/xXjkDS8/f9SUKTzV/Wcnmeca5lhoVEgPLB8bwpYXsQwtz/tCgbPQWZ5Ktxq7JFNgKSX AIG5EwD8xknYRGkQV+O+MHmG4yIU1SBodUhY= X-Google-Smtp-Source: AGHT+IEpwe+vaMFOgN6pafbtI/sp5AGRvgCzRUqN8012wUUanb8kHnG0J0Yv3hVLzaScR2H72iGdR1NBiK/wXj+y5I0= X-Received: by 2002:a05:6602:6d04:b0:824:d752:986 with SMTP id ca18e2360f4ac-82a1109b912mr65902839f.16.1724874242754; Wed, 28 Aug 2024 12:44:02 -0700 (PDT) In-Reply-To: <87v7zksety.fsf@posteo.net> Received-SPF: pass client-ip=2607:f8b0:4864:20::d2c; envelope-from=garklein97@gmail.com; helo=mail-io1-xd2c.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 29 Aug 2024 00:32:44 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:323167 Archived-At: > Can you explain what, ->>, --> and -some-> do generally? Of course! `->' takes in a value and a variable amount of forms, "threading" the value through them by inserting it as the first argument in the first form, inserting the result of that as the first argument in the next form, etc. For example, `(-> foo x (y 3) z)' is equivalent to `(z (y (x foo) 3))'. `->>' does the same thing, but inserts each value as the last argument of each form, instead of the first. `-->' substitutes the symbol `it' in each form as the previous value. `(-> x (* it it) (+ it 9) (sqrt it))' is equivalent to `(sqrt (+ (* x x) 9))', and can be read as "take `x', multiply it by itself, add nine to it, and take its square root". `-some->' is like `->', except that it will short-circuit the whole `-some->' expression with `nil' whenever any form returns `nil'. On Wed, Aug 28, 2024 at 2:05=E2=80=AFPM Philip Kaludercic wrote: > > Garklein writes: > > > Hello, > > > > Would it be possible to have the dash.el macros `->', `->>', `-->', > > and `-some->' in subr-x.el? I find them to be really useful, and would > > love to see them in the base language. > > > > I know that `thread-first' and `thread-last' exist, but I find that my > > eyes can recognize `->' and `->>' much easier. `-->' is really nice > > for more complex threading. > > This sounds more like a matter of habit. FWIW I find a symbol like ->> > or --> very unusual for Elisp. All in all, there are not that many if I > run this snippet > > (mapatoms > (lambda (sym) > (when (string-match-p (rx bos (+ (not alnum)) eos) > (symbol-name sym)) > (message "Note %S" sym)))) > > and most of the appear to be part of some DSL like rx, and not Lisp > functions (in my case only 18 out of 57). > > > `-some->' is useful in cases like this: > > Can you explain what, ->>, --> and -some-> do generally? > > > > (let (foo (function-which-may-return-nil)) > > (when foo > > (bar foo))) > > > > which can be rewritten, much cleaner, as > > > > (-some-> foo function-which-may-return-nil bar) > > [citation needed] > > > Thank you, > > Garklein > > > > > > -- > Philip Kaludercic on peregrine