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
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(require 'string)
|
|
|
|
(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
|
|
|
(defvar struct--enable-tests? t
|
2020-01-09 14:43:37 +01:00
|
|
|
"When t, run the test suite defined herein.")
|
|
|
|
|
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
|
2020-09-01 00:28:47 +02:00
|
|
|
(string-prepend "copy-")
|
2019-10-09 13:13:56 +02:00
|
|
|
intern))
|
|
|
|
(accessor (->> field
|
|
|
|
symbol-name
|
2020-09-01 00:28:47 +02:00
|
|
|
(string-prepend (string-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)))
|
|
|
|
|
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
|
2020-09-01 00:28:47 +02:00
|
|
|
(string-prepend "copy-")
|
2019-10-09 13:13:56 +02:00
|
|
|
intern))
|
|
|
|
(accessor (->> field
|
|
|
|
symbol-name
|
2020-09-01 00:28:47 +02:00
|
|
|
(string-prepend (string-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
|
2020-09-01 00:28:47 +02:00
|
|
|
(string-prepend (string-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)))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Tests
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(when struct--enable-tests?
|
2020-01-09 14:43:37 +01:00
|
|
|
(cl-defstruct dummy name age)
|
2020-09-01 00:28:47 +02:00
|
|
|
(defvar struct--test-dummy (make-dummy :name "Roofus" :age 19))
|
|
|
|
(struct-set! dummy name "Doofus" struct--test-dummy)
|
|
|
|
(prelude-assert (string= "Doofus" (dummy-name struct--test-dummy)))
|
|
|
|
(let ((result (struct-set dummy name "Shoofus" struct--test-dummy)))
|
|
|
|
;; Test the immutability of `struct-set'
|
|
|
|
(prelude-assert (string= "Doofus" (dummy-name struct--test-dummy)))
|
2020-08-31 18:05:31 +02:00
|
|
|
(prelude-assert (string= "Shoofus" (dummy-name result)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
(provide 'struct)
|
|
|
|
;;; struct.el ends here
|