Lint tuple.el

- add Version, URL, Package-Requires sections
- prefer `tuple-` prefix to `tuple/`
This commit is contained in:
William Carroll 2020-08-31 14:43:50 +01:00
parent 9661a3ff36
commit 1aa4b3a547

View file

@ -1,5 +1,9 @@
;;; tuple.el --- Tuple API for Elisp -*- lexical-binding: t -*- ;;; tuple.el --- Tuple API for Elisp -*- lexical-binding: t -*-
;; Author: William Carroll <wpcarro@gmail.com> ;; Author: William Carroll <wpcarro@gmail.com>
;; Version: 0.0.1
;; URL: https://git.wpcarro.dev/wpcarro/briefcase
;; Package-Requires: ((emacs "25.1"))
;;; Commentary: ;;; Commentary:
;; Work with cons cells with two elements with a familiar API for those who have ;; Work with cons cells with two elements with a familiar API for those who have
@ -7,78 +11,82 @@
;;; Code: ;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cl-defstruct tuple first second) (cl-defstruct tuple first second)
;; Create ;; Create
(defun tuple/new () (defun tuple-new ()
"Return an empty tuple." "Return an empty tuple."
(make-tuple :first nil (make-tuple :first nil
:second nil)) :second nil))
(defun tuple/from (a b) (defun tuple-from (a b)
"Return a new tuple from A and B." "Return a new tuple from A and B."
(make-tuple :first a (make-tuple :first a
:second b)) :second b))
(defun tuple/from-dotted (dp) (defun tuple-from-dotted (dp)
"Convert dotted pair, DP, into a tuple." "Convert dotted pair, DP, into a tuple."
(tuple/from (car dp) (cdr dp))) (tuple-from (car dp) (cdr dp)))
;; Read ;; Read
(defun tuple/first (pair) (defun tuple-first (pair)
"Return the first element of PAIR." "Return the first element of PAIR."
(tuple-first pair)) (tuple-first pair))
(defun tuple/second (pair) (defun tuple-second (pair)
"Return the second element of PAIR." "Return the second element of PAIR."
(tuple-second pair)) (tuple-second pair))
;; Update ;; Update
(defun tuple/map-each (f g pair) (defun tuple-map-each (f g pair)
"Apply F to first, G to second in PAIR." "Apply F to first, G to second in PAIR."
(->> pair (->> pair
(tuple/map-first f) (tuple-map-first f)
(tuple/map-second g))) (tuple-map-second g)))
(defun tuple/map (f pair) (defun tuple-map (f pair)
"Apply F to PAIR." "Apply F to PAIR."
(let ((pair-copy (copy-tuple pair))) (let ((pair-copy (copy-tuple pair)))
(funcall f pair-copy))) (funcall f pair-copy)))
(defun tuple/map-first (f pair) (defun tuple-map-first (f pair)
"Apply function F to the first element of PAIR." "Apply function F to the first element of PAIR."
(let ((pair-copy (copy-tuple pair))) (let ((pair-copy (copy-tuple pair)))
(setf (tuple-first pair-copy) (funcall f (tuple/first pair-copy))) (setf (tuple-first pair-copy) (funcall f (tuple-first pair-copy)))
pair-copy)) pair-copy))
(defun tuple/map-second (f pair) (defun tuple-map-second (f pair)
"Apply function F to the second element of PAIR." "Apply function F to the second element of PAIR."
(let ((pair-copy (copy-tuple pair))) (let ((pair-copy (copy-tuple pair)))
(setf (tuple-second pair-copy) (funcall f (tuple/second pair-copy))) (setf (tuple-second pair-copy) (funcall f (tuple-second pair-copy)))
pair-copy)) pair-copy))
(defun tuple/set-first (a pair) (defun tuple-set-first (a pair)
"Return a new tuple with the first element set as A in PAIR." "Return a new tuple with the first element set as A in PAIR."
(tuple/map-first (lambda (_) a) pair)) (tuple-map-first (lambda (_) a) pair))
(defun tuple/set-second (b pair) (defun tuple-set-second (b pair)
"Return a new tuple with the second element set as B in PAIR." "Return a new tuple with the second element set as B in PAIR."
(tuple/map-second (lambda (_) b) pair)) (tuple-map-second (lambda (_) b) pair))
;; Delete ;; Delete
(defun tuple/delete-first (pair) (defun tuple-delete-first (pair)
"Return PAIR with the first element set to nil." "Return PAIR with the first element set to nil."
(tuple/set-first nil pair)) (tuple-set-first nil pair))
(defun tuple/delete-second (pair) (defun tuple-delete-second (pair)
"Return PAIR with the second element set to nil." "Return PAIR with the second element set to nil."
(tuple/set-second nil pair)) (tuple-set-second nil pair))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Predicates ;; Predicates
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun tuple/instance? (x) (defun tuple-instance? (x)
"Return t if X is a tuple." "Return t if X is a tuple."
(tuple-p x)) (tuple-p x))