GNU bug report logs -
#63135
[PATCH 0/3] MATCH-RECROD improvements
Previous Next
Reported by: "(" <paren <at> disroot.org>
Date: Thu, 27 Apr 2023 22:06:01 UTC
Severity: normal
Tags: patch
Done: Josselin Poiret <dev <at> jpoiret.xyz>
Bug is archived. No further changes may be made.
Full log
Message #26 received at 63135 <at> debbugs.gnu.org (full text, mbox):
* 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
This bug report was last modified 1 year and 352 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.