[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
09/26: records: Add MATCH-RECORD-LAMBDA.
From: |
guix-commits |
Subject: |
09/26: records: Add MATCH-RECORD-LAMBDA. |
Date: |
Sun, 4 Jun 2023 05:38:32 -0400 (EDT) |
jpoiret pushed a commit to branch master
in repository guix.
commit 4cd529362108086eeec25d4d465261415cff45b3
Author: (unmatched-parenthesis ew syntax <paren@disroot.org>
AuthorDate: Fri Apr 28 20:19:05 2023 +0100
records: Add MATCH-RECORD-LAMBDA.
* guix/records.scm (match-record-lambda): New syntax.
* tests/records.scm ("match-record-lambda"): New test.
Signed-off-by: Josselin Poiret <dev@jpoiret.xyz>
---
.dir-locals.el | 1 +
guix/read-print.scm | 1 +
guix/records.scm | 14 +++++++++++++-
tests/records.scm | 12 ++++++++++++
4 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/read-print.scm b/guix/read-print.scm
index 04dc0dcfbe..25be289d60 100644
--- a/guix/read-print.scm
+++ b/guix/read-print.scm
@@ -319,6 +319,7 @@ expressions and blanks that were read."
('letrec* 2)
('match 2)
('match-record 3)
+ ('match-record-lambda 2)
('when 2)
('unless 2)
('package 1)
diff --git a/guix/records.scm b/guix/records.scm
index cfa46f0d80..2a88cb4b3c 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -31,7 +31,8 @@
alist->record
object->fields
recutils->alist
- match-record))
+ match-record
+ match-record-lambda))
;;; Commentary:
;;;
@@ -640,4 +641,15 @@ an unknown field is queried."
(match-record-inner record type (fields ...) body ...)
(throw 'wrong-type-arg record)))))
+(define-syntax match-record-lambda
+ (syntax-rules ()
+ "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."
+ ((_ type (field ...) body ...)
+ (lambda (record)
+ (if (eq? (struct-vtable record) type)
+ (match-record-inner 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 @@ Description: 1st line,
(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)
- branch master updated (65bce4d9f9 -> cacc0cb6ab), guix-commits, 2023/06/04
- 01/26: services: screen-locker-service-type: Configurable PAM and setuid., guix-commits, 2023/06/04
- 05/26: gnu: sbsigntools: Update to 0.9.5., guix-commits, 2023/06/04
- 13/26: gnu: Add ghc-vector-hashtables., guix-commits, 2023/06/04
- 07/26: records: match-record: Support thunked and delayed fields., guix-commits, 2023/06/04
- 08/26: dir-locals: Fix MATCH-RECORD indentation., guix-commits, 2023/06/04
- 09/26: records: Add MATCH-RECORD-LAMBDA.,
guix-commits <=
- 20/26: build-system: New agda-build-system., guix-commits, 2023/06/04
- 11/26: tests: records: Add test for ellipsis in body., guix-commits, 2023/06/04
- 12/26: gnu: Add ghc-peano., guix-commits, 2023/06/04
- 26/26: channels: Raise an error if dependency's introduction is malformed, guix-commits, 2023/06/04
- 04/26: gnu: proot: Update to 5.4.0 and fix clone3 behavior., guix-commits, 2023/06/04
- 02/26: gnu: heimdal: Run autoreconf., guix-commits, 2023/06/04
- 03/26: gnu: heimdal: Drop obsolete and insecure user tools., guix-commits, 2023/06/04
- 06/26: records: match-record: Raise a syntax error if TYPE is nonexistent., guix-commits, 2023/06/04
- 10/26: records: match-record: Do not show internal form., guix-commits, 2023/06/04
- 14/26: gnu: agda: Update to 2.6.3 and switch to git-fetch., guix-commits, 2023/06/04