unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: "\( via Guix-patches" via <guix-patches@gnu.org>
To: 63135@debbugs.gnu.org
Cc: "\(" <paren@disroot.org>, Josselin Poiret <dev@jpoiret.xyz>
Subject: [bug#63135] [PATCH v2 5/5] records: Add MATCH-RECORD-LAMBDA.
Date: Fri, 28 Apr 2023 20:19:05 +0100	[thread overview]
Message-ID: <20230428191905.13860-6-paren@disroot.org> (raw)
In-Reply-To: <20230428191905.13860-1-paren@disroot.org>

* guix/records.scm (match-record-lambda): New syntax.
* tests/records.scm ("match-record-lambda"): New test.
---
 .dir-locals.el    |  1 +
 guix/records.scm  | 15 ++++++++++++++-
 tests/records.scm | 12 ++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/.dir-locals.el b/.dir-locals.el
index 3ffd25ee94..d79b5c9d7e 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -71,6 +71,7 @@
    (eval . (put 'lambda* 'scheme-indent-function 1))
    (eval . (put 'substitute* 'scheme-indent-function 1))
    (eval . (put 'match-record 'scheme-indent-function 3))
+   (eval . (put 'match-record-lambda 'scheme-indent-function 2))
 
    ;; TODO: Contribute these to Emacs' scheme-mode.
    (eval . (put 'let-keywords 'scheme-indent-function 3))
diff --git a/guix/records.scm b/guix/records.scm
index 041eb2f297..504a023e87 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -31,7 +31,8 @@ (define-module (guix records)
             alist->record
             object->fields
             recutils->alist
-            match-record))
+            match-record
+            match-record-lambda))
 
 ;;; Commentary:
 ;;;
@@ -642,4 +643,16 @@ (define-syntax match-record
              (match-record-inner #,s record type (fields ...) body ...)
              (throw 'wrong-type-arg record))))))
 
+(define-syntax match-record-lambda
+  (lambda (s)
+    "Return a procedure accepting a single record of the given TYPE for which each
+FIELD will be bound to its FIELD name within the returned procedure.  A syntax error
+is raised if an unknown field is queried."
+    (syntax-case s ()
+      ((_ type (field ...) body ...)
+       #`(lambda (record)
+           (if (eq? (struct-vtable record) type)
+               (match-record-inner #,s record type (field ...) body ...)
+               (throw 'wrong-type-arg record)))))))
+
 ;;; records.scm ends here
diff --git a/tests/records.scm b/tests/records.scm
index 4f0aeb3903..8ee306bddc 100644
--- a/tests/records.scm
+++ b/tests/records.scm
@@ -590,4 +590,16 @@ (define-record-type* <with-thunked> with-thunked make-with-thunked
       (match-record rec <with-thunked> (normal thunked)
         (list normal thunked)))))
 
+(test-equal "match-record-lambda"
+  '("thing: foo" "thing: bar")
+  (begin
+    (define-record-type* <with-text> with-text make-with-text
+      with-text?
+      (text with-text-text))
+
+    (map (match-record-lambda <with-text> (text)
+           (string-append "thing: " text))
+         (list (with-text (text "foo"))
+               (with-text (text "bar"))))))
+
 (test-end)
-- 
2.39.2





  parent reply	other threads:[~2023-04-28 19:20 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-27 22:04 [bug#63135] [PATCH 0/3] MATCH-RECROD improvements ( via Guix-patches via
2023-04-27 22:06 ` [bug#63135] [PATCH 1/3] records: match-record: Raise a syntax error if TYPE is nonexistent ( via Guix-patches via
2023-04-27 22:06   ` [bug#63135] [PATCH 2/3] records: match-record: Display more helpful field-not-found error ( via Guix-patches via
2023-04-27 22:06   ` [bug#63135] [PATCH 3/3] records: match-record: Support thunked and delayed fields ( via Guix-patches via
2023-04-28 19:19 ` [bug#63135] [PATCH v2 0/5] MATCH-RECORD improvements ( via Guix-patches via
2023-04-28 19:19   ` [bug#63135] [PATCH v2 1/5] records: match-record: Raise a syntax error if TYPE is nonexistent ( via Guix-patches via
2023-05-19 15:22     ` [bug#63135] [PATCH 0/3] MATCH-RECROD improvements Ludovic Courtès
2023-04-28 19:19   ` [bug#63135] [PATCH v2 2/5] records: match-record: Display more helpful field-not-found error ( via Guix-patches via
2023-05-19 15:25     ` [bug#63135] [PATCH 0/3] MATCH-RECROD improvements Ludovic Courtès
2023-04-28 19:19   ` [bug#63135] [PATCH v2 3/5] records: match-record: Support thunked and delayed fields ( via Guix-patches via
2023-05-19 15:25     ` [bug#63135] [PATCH 0/3] MATCH-RECROD improvements Ludovic Courtès
2023-04-28 19:19   ` [bug#63135] [PATCH v2 4/5] dir-locals: Fix MATCH-RECORD indentation ( via Guix-patches via
2023-05-19 15:27     ` [bug#63135] [PATCH 0/3] MATCH-RECROD improvements Ludovic Courtès
2023-05-20 18:02       ` ( via Guix-patches via
2023-05-24 14:11         ` Ludovic Courtès
2023-05-24 15:49           ` ( via Guix-patches via
2023-05-26 16:41             ` Ludovic Courtès
2023-05-27  0:55               ` ( via Guix-patches via
2023-04-28 19:19   ` ( via Guix-patches via [this message]
2023-05-19 15:28     ` Ludovic Courtès
2023-06-04  9:47   ` bug#63135: [PATCH v2 0/5] MATCH-RECORD improvements Josselin Poiret via Guix-patches via
2023-06-04 10:48     ` [bug#63135] " Josselin Poiret via Guix-patches via
2023-06-04 19:11     ` ( via Guix-patches via

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230428191905.13860-6-paren@disroot.org \
    --to=guix-patches@gnu.org \
    --cc=63135@debbugs.gnu.org \
    --cc=dev@jpoiret.xyz \
    --cc=paren@disroot.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).