2019-10-09 13:13:56 +02:00
|
|
|
;;; struct.el --- Helpers for working with structs -*- lexical-binding: t -*-
|
2020-09-01 00:28:47 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2022-08-02 22:25:30 +02:00
|
|
|
;; Version: 1.0.0
|
2020-09-01 00:28:47 +02:00
|
|
|
;; Package-Requires: ((emacs "24.3"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Provides new macros for working with structs. Also provides adapter
|
|
|
|
;; interfaces to existing struct macros, that should have more intuitive
|
|
|
|
;; interfaces.
|
|
|
|
;;
|
|
|
|
;; Sometimes `setf' just isn't enough.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2020-01-09 14:43:37 +01:00
|
|
|
;; Library
|
2019-10-09 13:13:56 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defmacro struct-update (type field f xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Apply F to FIELD in XS, which is a struct of TYPE.
|
|
|
|
This is immutable."
|
2022-08-02 22:21:06 +02:00
|
|
|
(let ((copier (struct--copier-for type))
|
|
|
|
(accessor (struct--accessor-for type field)))
|
2019-10-09 13:13:56 +02:00
|
|
|
`(let ((copy (,copier ,xs)))
|
|
|
|
(setf (,accessor copy) (funcall ,f (,accessor copy)))
|
|
|
|
copy)))
|
|
|
|
|
2022-08-02 22:10:10 +02:00
|
|
|
(defmacro struct-update! (type field f xs)
|
|
|
|
"Mutably apply F to FIELD in XS."
|
2022-08-02 22:21:06 +02:00
|
|
|
(let ((accessor (struct--accessor-for type field)))
|
2022-08-02 22:10:10 +02:00
|
|
|
`(progn
|
|
|
|
(setf (,accessor ,xs) (funcall ,f (,accessor ,xs)))
|
|
|
|
,xs)))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defmacro struct-set (type field x xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Immutably set FIELD in XS (struct TYPE) to X."
|
2022-08-02 22:21:06 +02:00
|
|
|
(let ((copier (struct--copier-for type))
|
|
|
|
(accessor (struct--accessor-for type field)))
|
2019-10-09 13:13:56 +02:00
|
|
|
`(let ((copy (,copier ,xs)))
|
|
|
|
(setf (,accessor copy) ,x)
|
|
|
|
copy)))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defmacro struct-set! (type field x xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Set FIELD in XS (struct TYPE) to X mutably.
|
|
|
|
This is an adapter interface to `setf'."
|
2022-08-02 22:21:06 +02:00
|
|
|
(let ((accessor (struct--accessor-for type field)))
|
2019-10-09 13:13:56 +02:00
|
|
|
`(progn
|
2020-01-09 14:43:37 +01:00
|
|
|
(setf (,accessor ,xs) ,x)
|
|
|
|
,xs)))
|
|
|
|
|
2022-08-02 22:21:06 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Helper Functions
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defun struct--copier-for (type)
|
2022-08-02 22:25:30 +02:00
|
|
|
(intern (format "copy-%s" (symbol-name type))))
|
2022-08-02 22:21:06 +02:00
|
|
|
|
|
|
|
(defun struct--accessor-for (type field)
|
2022-08-02 22:25:30 +02:00
|
|
|
(intern (format "%s-%s"
|
|
|
|
(symbol-name type)
|
|
|
|
(symbol-name field))))
|
2022-08-02 22:21:06 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(provide 'struct)
|
|
|
|
;;; struct.el ends here
|