From patchwork Thu Sep 26 10:09:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herman Rimm X-Patchwork-Id: 31504 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 3DE6827BBEA; Thu, 26 Sep 2024 11:12:59 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE,SPF_HELO_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id C4BED27BBE2 for ; Thu, 26 Sep 2024 11:12:58 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1stlTK-0002za-NA; Thu, 26 Sep 2024 06:11:42 -0400 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 1stlTH-0002xU-Sq for guix-patches@gnu.org; Thu, 26 Sep 2024 06:11:39 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1stlTH-0007Pj-GU; Thu, 26 Sep 2024 06:11:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=WHUatHCEKa7jz6Gfk1bYsIZYqcMbf7e0yhQ/cl/jGpE=; b=OwzAEQJNQSGBH/IvXNgwukX1UapW3qxtwYhgGhMrAGXOQCsgjBGyRTZZaqFZ6mrDF2FkQEv4x+y+IYMX9vjXYXEkVZMZuy9ZCqs5zAP/k+vUgNxrW/5rkg4V0nBIwlnPKZYuxYZ2O/j32T7VSeSCQw3S4SoW1VjfXWZ8lCJif2fU+jilMCYN0TOpeqkcXtEP5//Ycz0RlnftLlgKnx28nfyjtI61whxnXn0ND33ikHV+Skndtdn/4Iueh2mFZvU+LjbA1ocxzAmyvO9URtD5q3lWUxtadCm/MWyCe2N7wSbdipvqE4b2VS/pkfibe78dAobfuZng2XmUDP/LqGHl8w==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1stlTg-0003UP-PF; Thu, 26 Sep 2024 06:12:04 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#73202] [PATCH v3 06/14] guix: utils: Add flatten and flat-map from haunt. Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Thu, 26 Sep 2024 10:12:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 73202 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 73202@debbugs.gnu.org Cc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 73202-submit@debbugs.gnu.org id=B73202.172734548813259 (code B ref 73202); Thu, 26 Sep 2024 10:12:04 +0000 Received: (at 73202) by debbugs.gnu.org; 26 Sep 2024 10:11:28 +0000 Received: from localhost ([127.0.0.1]:55493 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1stlT5-0003Rg-Jp for submit@debbugs.gnu.org; Thu, 26 Sep 2024 06:11:27 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:53171 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1stlSy-0003PN-Av for 73202@debbugs.gnu.org; Thu, 26 Sep 2024 06:11:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1727345439; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WHUatHCEKa7jz6Gfk1bYsIZYqcMbf7e0yhQ/cl/jGpE=; b=jA2rOqcgFQ8XX0jzW8cUH4iuSav+kEPsioL9/L8t/FpAJtwimJJHuVJPVhLa1Rl3Ej1zlU K6tIKhdyXsGOvb7wVy93FziPNLeVeYBY0HeqgT0YB/cmPo0k6iOyKrdn364J6iolonahN9 CRKQ1+2PjfB8Cex7gH2uaqaJ85IBuJk2edEMPLZLYgvP1C82594pt5Jg/PyErpZ8VU27pP keNrugRrwP0yTFjwKJhu0g8uRcj1LcCC1b9a6NfaHCVGvsZBIBPkcR4xwGxPmBra1xxsM1 j5nI+1H7h/GuxD/dhpPXoAcIdYDcTt0LdQV3iEmn9U8GOagcQPTJv3RJe06i6Q== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id 972bb699 (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO) for <73202@debbugs.gnu.org>; Thu, 26 Sep 2024 10:10:39 +0000 (UTC) Date: Thu, 26 Sep 2024 12:09:03 +0200 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: <74c789e74594d538308d33633ed8540283dcde49.1727345067.git.herman@rimm.ee> References: <74c789e74594d538308d33633ed8540283dcde49.1727345067.git.herman@rimm.ee> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Herman Rimm X-ACL-Warn: , Herman Rimm via Guix-patches X-Patchwork-Original-From: Herman Rimm via Guix-patches via From: Herman Rimm Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/utils.scm (flatten, flat-map): Add procedures. Change-Id: I1d7d49fd02115e3de09ed69bcf5f55a10423162e --- guix/utils.scm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/guix/utils.scm b/guix/utils.scm index f161cb4ef3..2740552a75 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -139,6 +139,9 @@ (define-module (guix utils) with-environment-variables arguments-from-environment-variable + flatten + flat-map + config-directory cache-directory @@ -1028,6 +1031,29 @@ (define (with-atomic-file-output file proc) (false-if-exception (delete-file template)) (close-port out))))) +;; TODO: bring over other utility procedures from (haunt utils). +(define* (flatten lst #:optional depth) + "Return a list that recursively concatenates the sub-lists of LST, +up to DEPTH levels deep. When DEPTH is #f, the entire tree is +flattened." + (if (and (number? depth) (zero? depth)) + lst + (fold-right (match-lambda* + (((sub-list ...) memo) + (append (flatten sub-list (and depth (1- depth))) + memo)) + ((elem memo) + (cons elem memo))) + '() + lst))) + +(define (flat-map proc . lsts) + "Apply PROC to each element of each list in LSTS and return a new +list in which nested lists are concatenated into the result. + +For example, the list (1 2 (3)) would be flattened to (1 2 3)." + (flatten (apply map proc lsts) 1)) + (define* (xdg-directory variable suffix #:key (ensure? #t)) "Return the name of the XDG directory that matches VARIABLE and SUFFIX, after making sure that it exists if ENSURE? is true. VARIABLE is an