Lint maybe.el

This change had rippling implications.
This commit is contained in:
William Carroll 2020-08-31 14:59:48 +01:00
parent 1c87082648
commit a35f723d92
12 changed files with 53 additions and 49 deletions

View file

@ -135,7 +135,7 @@ Returns the first occurrence of K in XS since alists support multiple entries."
"Apply F to the value stored at K in XS. "Apply F to the value stored at K in XS.
If `K' is not in `XS', this function errors. Use `alist/upsert' if you're If `K' is not in `XS', this function errors. Use `alist/upsert' if you're
interested in inserting a value when a key doesn't already exist." interested in inserting a value when a key doesn't already exist."
(if (maybe/nil? (alist/get k xs)) (if (maybe-nil? (alist/get k xs))
(error "Refusing to update: key does not exist in alist") (error "Refusing to update: key does not exist in alist")
(alist/set k (funcall f (alist/get k xs)) xs))) (alist/set k (funcall f (alist/get k xs)) xs)))
@ -183,11 +183,11 @@ Mutative variant of `alist/delete'."
(defun alist/has-key? (k xs) (defun alist/has-key? (k xs)
"Return t if XS has a key `equal' to K." "Return t if XS has a key `equal' to K."
(maybe/some? (assoc k xs))) (maybe-some? (assoc k xs)))
(defun alist/has-value? (v xs) (defun alist/has-value? (v xs)
"Return t if XS has a value of V." "Return t if XS has a value of V."
(maybe/some? (rassoc v xs))) (maybe-some? (rassoc v xs)))
(defun alist/count (xs) (defun alist/count (xs)
"Return the number of entries in XS." "Return the number of entries in XS."

View file

@ -90,7 +90,7 @@ This will ignore Emacs-generated buffers, like *Messages*. It will also ignore
(defun buffer/exists? (name) (defun buffer/exists? (name)
"Return t if buffer, NAME, exists." "Return t if buffer, NAME, exists."
(maybe/some? (buffer/find name))) (maybe-some? (buffer/find name)))
(defun buffer/new (name) (defun buffer/new (name)
"Return a newly created buffer NAME." "Return a newly created buffer NAME."
@ -100,7 +100,7 @@ This will ignore Emacs-generated buffers, like *Messages*. It will also ignore
"Find or create buffer, NAME. "Find or create buffer, NAME.
Return a reference to that buffer." Return a reference to that buffer."
(let ((x (buffer/find name))) (let ((x (buffer/find name)))
(if (maybe/some? x) (if (maybe-some? x)
x x
(buffer/new name)))) (buffer/new name))))
@ -163,7 +163,7 @@ This function ignores Emacs-generated buffers, i.e. the ones that look like
(interactive) (interactive)
(let* ((xs (buffer/source-code-buffers)) (let* ((xs (buffer/source-code-buffers))
(candidate (list/get 1 xs))) (candidate (list/get 1 xs)))
(prelude/assert (maybe/some? candidate)) (prelude/assert (maybe-some? candidate))
(switch-to-buffer candidate))) (switch-to-buffer candidate)))
(when buffer/install-kbds? (when buffer/install-kbds?

View file

@ -21,7 +21,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defconst constants/ci? (defconst constants/ci?
(maybe/some? (getenv "CI")) (maybe-some? (getenv "CI"))
"True when Emacs is running in CI.") "True when Emacs is running in CI.")
(defconst constants/briefcase (defconst constants/briefcase

View file

@ -73,7 +73,7 @@
(defun cycle/previous-focus (cycle) (defun cycle/previous-focus (cycle)
"Return the previously focused entry in CYCLE." "Return the previously focused entry in CYCLE."
(let ((i (cycle-previous-index cycle))) (let ((i (cycle-previous-index cycle)))
(if (maybe/some? i) (if (maybe-some? i)
(nth i (cycle-xs cycle)) (nth i (cycle-xs cycle))
nil))) nil)))
@ -84,7 +84,7 @@
This will error when previous-index is nil. This function mutates the This will error when previous-index is nil. This function mutates the
underlying struct." underlying struct."
(let ((i (cycle-previous-index xs))) (let ((i (cycle-previous-index xs)))
(if (maybe/some? i) (if (maybe-some? i)
(progn (progn
(cycle/jump i xs) (cycle/jump i xs)
(cycle/current xs)) (cycle/current xs))
@ -148,7 +148,7 @@ ITEM is the first item in XS that t for `equal'."
(defun cycle/focused? (xs) (defun cycle/focused? (xs)
"Return t if cycle XS has a non-nil value for current-index." "Return t if cycle XS has a non-nil value for current-index."
(maybe/some? (cycle-current-index xs))) (maybe-some? (cycle-current-index xs)))
(defun cycle/append (x xs) (defun cycle/append (x xs)
"Add X to the left of the focused element in XS. "Add X to the left of the focused element in XS.
@ -196,7 +196,7 @@ If X is the currently focused value, after it's deleted, current-index will be
(when cycle/enable-tests? (when cycle/enable-tests?
(let ((xs (cycle/new 1 2 3))) (let ((xs (cycle/new 1 2 3)))
(prelude/assert (maybe/nil? (cycle/previous-focus xs))) (prelude/assert (maybe-nil? (cycle/previous-focus xs)))
(prelude/assert (= 1 (cycle/current xs))) (prelude/assert (= 1 (cycle/current xs)))
(prelude/assert (= 2 (cycle/next xs))) (prelude/assert (= 2 (cycle/next xs)))
(prelude/assert (= 1 (cycle/previous-focus xs))) (prelude/assert (= 1 (cycle/previous-focus xs)))

View file

@ -86,7 +86,7 @@
(defun fonts/set (font &optional size) (defun fonts/set (font &optional size)
"Change the font to `FONT' with option integer, SIZE, in pixels." "Change the font to `FONT' with option integer, SIZE, in pixels."
(if (maybe/some? size) (if (maybe-some? size)
(set-frame-font (string/format "%s %s" font size) nil t) (set-frame-font (string/format "%s %s" font size) nil t)
(set-frame-font font nil t))) (set-frame-font font nil t)))

View file

@ -59,7 +59,7 @@
"Resolve an IRC server from a given CHANNEL." "Resolve an IRC server from a given CHANNEL."
(let ((result (alist/find (lambda (k v) (cycle/contains? channel v)) (let ((result (alist/find (lambda (k v) (cycle/contains? channel v))
server->channels))) server->channels)))
(prelude/assert (maybe/some? result)) (prelude/assert (maybe-some? result))
result)) result))
(defun irc/channel->cycle (server->channels channel) (defun irc/channel->cycle (server->channels channel)

View file

@ -32,7 +32,7 @@
(defun laptop-battery/available? () (defun laptop-battery/available? ()
"Return t if battery information is available." "Return t if battery information is available."
(maybe/some? battery-status-function)) (maybe-some? battery-status-function))
(defun laptop-battery/percentage () (defun laptop-battery/percentage ()
"Return the current percentage of the battery." "Return the current percentage of the battery."

View file

@ -28,13 +28,13 @@
(cl-defun math/triangle-of-power (&key base power result) (cl-defun math/triangle-of-power (&key base power result)
;; TODO: Assert two of three are set. ;; TODO: Assert two of three are set.
(cond (cond
((maybe/somes? base power result) ((maybe-somes? base power result)
(error "All three arguments should not be set")) (error "All three arguments should not be set"))
((maybe/somes? power result) ((maybe-somes? power result)
(message "power and result")) (message "power and result"))
((maybe/somes? base result) ((maybe-somes? base result)
(log result base)) (log result base))
((maybe/somes? base power) ((maybe-somes? base power)
(expt base power)) (expt base power))
(t (t
(error "Two of the three arguments must be set")))) (error "Two of the three arguments must be set"))))

View file

@ -1,5 +1,9 @@
;;; maybe.el --- Library for dealing with nil values -*- lexical-binding: t -*- ;;; maybe.el --- Library for dealing with nil values -*- lexical-binding: t -*-
;; Author: William Carroll <wpcarro@gmail.com> ;; Author: William Carroll <wpcarro@gmail.com>
;; Version: 0.0.1
;; Package-Requires: ((emacs "24"))
;; Homepage: https://user.git.corp.google.com/wpcarro/briefcase
;;; Commentary: ;;; Commentary:
;; Inspired by Elm's Maybe library. ;; Inspired by Elm's Maybe library.
@ -39,36 +43,36 @@
;; Constants ;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar maybe/test? t (defvar maybe--run-tests? t
"When t, run the test suite defined herein.") "When t, run the test suite defined herein.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library ;; Library
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun maybe/nil? (x) (defun maybe-nil? (x)
"Return t if X is nil." "Return t if X is nil."
(eq nil x)) (eq nil x))
(defun maybe/some? (x) (defun maybe-some? (x)
"Return t when X is non-nil." "Return t when X is non-nil."
(not (maybe/nil? x))) (not (maybe-nil? x)))
(defun maybe/nils? (&rest xs) (defun maybe-nils? (&rest xs)
"Return t if all XS are nil." "Return t if all XS are nil."
(list/all? #'maybe/nil? xs)) (list/all? #'maybe-nil? xs))
(defun maybe/somes? (&rest xs) (defun maybe-somes? (&rest xs)
"Return t if all XS are non-nil." "Return t if all XS are non-nil."
(list/all? #'maybe/some? xs)) (list/all? #'maybe-some? xs))
(defun maybe/default (default x) (defun maybe-default (default x)
"Return DEFAULT when X is nil." "Return DEFAULT when X is nil."
(if (maybe/nil? x) default x)) (if (maybe-nil? x) default x))
(defun maybe/map (f x) (defun maybe-map (f x)
"Apply F to X if X is not nil." "Apply F to X if X is not nil."
(if (maybe/some? x) (if (maybe-some? x)
(funcall f x) (funcall f x)
x)) x))
@ -76,27 +80,27 @@
;; Tests ;; Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(when maybe/test? (when maybe--run-tests?
;; nil? ;; nil?
(prelude/assert (maybe/nil? nil)) (prelude/assert (maybe-nil? nil))
(prelude/refute (maybe/nil? t)) (prelude/refute (maybe-nil? t))
;; some? ;; some?
(prelude/assert (maybe/some? 10)) (prelude/assert (maybe-some? 10))
(prelude/refute (maybe/some? nil)) (prelude/refute (maybe-some? nil))
;; nils? ;; nils?
(prelude/assert (maybe/nils? nil nil nil nil)) (prelude/assert (maybe-nils? nil nil nil nil))
(prelude/refute (maybe/nils? nil t nil t)) (prelude/refute (maybe-nils? nil t nil t))
;; somes? ;; somes?
(prelude/assert (maybe/somes? t 10 '(1 2 3) "some")) (prelude/assert (maybe-somes? t 10 '(1 2 3) "some"))
(prelude/refute (maybe/somes? t nil '(1 2 3) "some")) (prelude/refute (maybe-somes? t nil '(1 2 3) "some"))
;; default ;; default
(prelude/assert (prelude/assert
(and (= 0 (maybe/default 5 0)) (and (= 0 (maybe-default 5 0))
(= 5 (maybe/default 5 nil)))) (= 5 (maybe-default 5 nil))))
;; map ;; map
(prelude/assert (prelude/assert
(and (= 2 (maybe/map #'1+ 1)) (and (= 2 (maybe-map #'1+ 1))
(eq nil (maybe/map #'1+ nil))))) (eq nil (maybe-map #'1+ nil)))))
(provide 'maybe) (provide 'maybe)
;;; maybe.el ends here ;;; maybe.el ends here

View file

@ -95,7 +95,7 @@ These are strict assertions and purposely do not rely on truthiness."
(add-hook 'linum-mode-hook (add-hook 'linum-mode-hook
(lambda () (lambda ()
(setq linum/safe? t) (setq linum/safe? t)
(when (maybe/some? linum/mru-color) (when (maybe-some? linum/mru-color)
(set-face-foreground 'linum linum/mru-color)))) (set-face-foreground 'linum linum/mru-color))))
(defun prelude/set-line-number-color (color) (defun prelude/set-line-number-color (color)
@ -137,7 +137,7 @@ This is a wrapper around `start-process' that has an API that resembles
"Return t if CLI tool NAME exists according to `exec-path'." "Return t if CLI tool NAME exists according to `exec-path'."
(let ((file (locate-file name exec-path))) (let ((file (locate-file name exec-path)))
(require 'maybe) (require 'maybe)
(if (maybe/some? file) (if (maybe-some? file)
(f-exists? file) (f-exists? file)
nil))) nil)))

View file

@ -69,7 +69,7 @@ Depth-first traversals have the advantage of typically consuming less memory
(cl-labels ((do-reduce-depth (cl-labels ((do-reduce-depth
(acc f node depth) (acc f node depth)
(let ((acc-new (funcall f node acc depth))) (let ((acc-new (funcall f node acc depth)))
(if (or (maybe/nil? node) (if (or (maybe-nil? node)
(tree-leaf? node)) (tree-leaf? node))
acc-new acc-new
(list/reduce (list/reduce
@ -98,7 +98,7 @@ Depth-first traversals have the advantage of typically consuming less memory
(tree-reduce-depth (tree-reduce-depth
'() '()
(lambda (node acc depth) (lambda (node acc depth)
(if (or (maybe/nil? node) (if (or (maybe-nil? node)
(tree-leaf? node)) (tree-leaf? node))
(list/cons depth acc) (list/cons depth acc)
acc)) acc))
@ -139,7 +139,7 @@ generating test data. Warning this function can overflow the stack."
(defun tree-leaf? (node) (defun tree-leaf? (node)
"Return t if NODE has no children." "Return t if NODE has no children."
(maybe/nil? (node-children node))) (maybe-nil? (node-children node)))
(defun tree-balanced? (n xs) (defun tree-balanced? (n xs)
"Return t if the tree, XS, is balanced. "Return t if the tree, XS, is balanced.

View file

@ -25,7 +25,7 @@
(defun window-find (name) (defun window-find (name)
"Find a window by the NAME of the buffer it's hosting." "Find a window by the NAME of the buffer it's hosting."
(let ((buffer (get-buffer name))) (let ((buffer (get-buffer name)))
(if (maybe/some? buffer) (if (maybe-some? buffer)
(get-buffer-window buffer) (get-buffer-window buffer)
nil))) nil)))