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>
|
2020-09-01 00:28:47 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; 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:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2022-07-29 05:29:07 +02:00
|
|
|
(require 's)
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'dash)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
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."
|
|
|
|
(let ((copier (->> type
|
|
|
|
symbol-name
|
2022-07-29 05:29:07 +02:00
|
|
|
(s-prepend "copy-")
|
2019-10-09 13:13:56 +02:00
|
|
|
intern))
|
|
|
|
(accessor (->> field
|
|
|
|
symbol-name
|
2022-07-29 05:29:07 +02:00
|
|
|
(s-prepend (s-concat (symbol-name type) "-"))
|
2019-10-09 13:13:56 +02:00
|
|
|
intern)))
|
|
|
|
`(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."
|
|
|
|
(let ((accessor (->> field
|
|
|
|
symbol-name
|
|
|
|
(s-prepend (s-concat (symbol-name type) "-"))
|
|
|
|
intern)))
|
|
|
|
`(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."
|
|
|
|
(let ((copier (->> type
|
|
|
|
symbol-name
|
2022-07-29 05:29:07 +02:00
|
|
|
(s-prepend "copy-")
|
2019-10-09 13:13:56 +02:00
|
|
|
intern))
|
|
|
|
(accessor (->> field
|
|
|
|
symbol-name
|
2022-07-29 05:29:07 +02:00
|
|
|
(s-prepend (s-concat (symbol-name type) "-"))
|
2019-10-09 13:13:56 +02:00
|
|
|
intern)))
|
|
|
|
`(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'."
|
|
|
|
(let ((accessor (->> field
|
|
|
|
symbol-name
|
2022-07-29 05:29:07 +02:00
|
|
|
(s-prepend (s-concat (symbol-name type) "-"))
|
2019-10-09 13:13:56 +02:00
|
|
|
intern)))
|
|
|
|
`(progn
|
2020-01-09 14:43:37 +01:00
|
|
|
(setf (,accessor ,xs) ,x)
|
|
|
|
,xs)))
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(provide 'struct)
|
|
|
|
;;; struct.el ends here
|