2019-10-09 13:13:56 +02:00
|
|
|
;;; vector.el --- Working with Elisp's Vector data type -*- lexical-binding: t -*-
|
2020-08-31 15:42:06 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-08-31 15:42:06 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; URL: https://git.wpcarro.dev/wpcarro/briefcase
|
|
|
|
;; Package-Requires: ((emacs "25.1"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; It might be best to think of Elisp vectors as tuples in languages like
|
|
|
|
;; Haskell or Erlang.
|
|
|
|
;;
|
|
|
|
;; Not surprisingly, this API is modelled after Elixir's Tuple API.
|
|
|
|
;;
|
|
|
|
;; Some Elisp trivia:
|
|
|
|
;; - "Array": Usually means vector or string.
|
|
|
|
;; - "Sequence": Usually means list or "array" (see above).
|
|
|
|
;;
|
|
|
|
;; It might be a good idea to think of Array and Sequence as typeclasses in
|
|
|
|
;; Elisp. This is perhaps more similar to Elixir's notion of the Enum protocol.
|
|
|
|
;;
|
|
|
|
;; Intentionally not supporting a to-list function, because tuples can contain
|
|
|
|
;; heterogenous types whereas lists should contain homogenous types.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;; TODO: Consider supporting an alias named tuple for vector.
|
|
|
|
|
2020-01-20 11:15:48 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-08-31 15:42:06 +02:00
|
|
|
(defconst vector-enable-tests? t
|
2020-01-20 11:15:48 +01:00
|
|
|
"When t, run the tests defined herein.")
|
|
|
|
|
2020-08-31 15:42:06 +02:00
|
|
|
;; TODO: Consider labelling variadic functions like `vector-concat*'
|
|
|
|
;; vs. `vector-concat'.
|
|
|
|
(defun vector-concat (&rest args)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a new vector composed of all vectors in `ARGS'."
|
|
|
|
(apply #'vconcat args))
|
|
|
|
|
|
|
|
;; TODO: Here's a sketch of a protocol macro being consumed.
|
|
|
|
;; (definstance monoid vector
|
|
|
|
;; :empty (lambda () []))
|
|
|
|
|
2020-08-31 15:42:06 +02:00
|
|
|
(defun vector-prepend (x xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Add `X' to the beginning of `XS'."
|
2020-08-31 15:42:06 +02:00
|
|
|
(vector-concat `[,x] xs))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-08-31 15:42:06 +02:00
|
|
|
(defun vector-append (x xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Add `X' to the end of `XS'."
|
2020-08-31 15:42:06 +02:00
|
|
|
(vector-concat xs `[,x]))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-08-31 15:42:06 +02:00
|
|
|
(defun vector-get (i xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the value in `XS' at index, `I'."
|
|
|
|
(aref xs i))
|
|
|
|
|
2020-08-31 15:42:06 +02:00
|
|
|
(defun vector-set (i v xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Set index `I' to value `V' in `XS'.
|
|
|
|
Returns a copy of `XS' with the updates."
|
|
|
|
(let ((copy (vconcat [] xs)))
|
|
|
|
(aset copy i v)
|
|
|
|
copy))
|
|
|
|
|
2020-08-31 15:42:06 +02:00
|
|
|
(defun vector-set! (i v xs)
|
2020-01-20 11:15:48 +01:00
|
|
|
"Set index `I' to value `V' in `XS'.
|
|
|
|
This function mutates XS."
|
|
|
|
(aset xs i v))
|
|
|
|
|
2020-08-31 15:42:06 +02:00
|
|
|
(when vector-enable-tests?
|
2020-01-20 11:15:48 +01:00
|
|
|
(let ((xs [1 2 3])
|
|
|
|
(ys [1 2 3]))
|
2020-08-31 15:42:06 +02:00
|
|
|
(prelude/assert (= 1 (vector-get 0 ys)))
|
|
|
|
(vector-set 0 4 ys)
|
|
|
|
(prelude/assert (= 1 (vector-get 0 ys)))
|
|
|
|
(prelude/assert (= 1 (vector-get 0 xs)))
|
|
|
|
(vector-set! 0 4 xs)
|
|
|
|
(prelude/assert (= 4 (vector-get 0 xs)))))
|
2020-01-20 11:15:48 +01:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; TODO: Decide between "remove" and "delete" as the appropriate verbs.
|
|
|
|
;; TODO: Implement this.
|
|
|
|
;; (defun vector/delete (i xs)
|
|
|
|
;; "Remove the element at `I' in `XS'.")
|
|
|
|
|
|
|
|
(provide 'vector)
|
|
|
|
;;; vector.el ends here
|