2019-10-09 13:13:56 +02:00
|
|
|
;;; maybe.el --- Library for dealing with nil values -*- lexical-binding: t -*-
|
2020-08-31 15:59:48 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-08-31 15:59:48 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; Package-Requires: ((emacs "24"))
|
|
|
|
;; Homepage: https://user.git.corp.google.com/wpcarro/briefcase
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Inspired by Elm's Maybe library.
|
|
|
|
;;
|
|
|
|
;; For now, a Nothing value will be defined exclusively as a nil value. I'm
|
|
|
|
;; uninterested in supported falsiness in this module even at risk of going
|
|
|
|
;; against the LISP grain.
|
|
|
|
;;
|
|
|
|
;; I'm avoiding introducing a struct to handle the creation of Just and Nothing
|
|
|
|
;; variants of Maybe. Perhaps this is a mistake in which case this file would
|
|
|
|
;; be more aptly named nil.el. I may change that. Because of this limitation,
|
|
|
|
;; functions in Elm's Maybe library like andThen, which is the monadic bind for
|
|
|
|
;; the Maybe type, doesn't have a home here since we cannot compose multiple
|
|
|
|
;; Nothing or Just values without a struct or some other construct.
|
|
|
|
;;
|
|
|
|
;; Possible names for the variants of a Maybe.
|
|
|
|
;; None | Some
|
|
|
|
;; Nothing | Something
|
|
|
|
;; None | Just
|
|
|
|
;; Nil | Set
|
|
|
|
;;
|
|
|
|
;; NOTE: In Elisp, values like '() (i.e. the empty list) are aliases for nil.
|
|
|
|
;; What else in Elisp is an alias in this way?
|
|
|
|
;; Examples:
|
|
|
|
;; TODO: Provide examples of other nil types in Elisp.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(require 'prelude)
|
|
|
|
(require 'list)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Constants
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-08-31 15:59:48 +02:00
|
|
|
(defvar maybe--run-tests? t
|
2019-10-09 13:13:56 +02:00
|
|
|
"When t, run the test suite defined herein.")
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-08-31 15:59:48 +02:00
|
|
|
(defun maybe-nil? (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if X is nil."
|
|
|
|
(eq nil x))
|
|
|
|
|
2020-08-31 15:59:48 +02:00
|
|
|
(defun maybe-some? (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t when X is non-nil."
|
2020-08-31 15:59:48 +02:00
|
|
|
(not (maybe-nil? x)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-08-31 15:59:48 +02:00
|
|
|
(defun maybe-nils? (&rest xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if all XS are nil."
|
2020-08-31 15:59:48 +02:00
|
|
|
(list/all? #'maybe-nil? xs))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-08-31 15:59:48 +02:00
|
|
|
(defun maybe-somes? (&rest xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if all XS are non-nil."
|
2020-08-31 15:59:48 +02:00
|
|
|
(list/all? #'maybe-some? xs))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-08-31 15:59:48 +02:00
|
|
|
(defun maybe-default (default x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return DEFAULT when X is nil."
|
2020-08-31 15:59:48 +02:00
|
|
|
(if (maybe-nil? x) default x))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-08-31 15:59:48 +02:00
|
|
|
(defun maybe-map (f x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Apply F to X if X is not nil."
|
2020-08-31 15:59:48 +02:00
|
|
|
(if (maybe-some? x)
|
2019-10-09 13:13:56 +02:00
|
|
|
(funcall f x)
|
|
|
|
x))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Tests
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-08-31 15:59:48 +02:00
|
|
|
(when maybe--run-tests?
|
2019-10-09 13:13:56 +02:00
|
|
|
;; nil?
|
2020-08-31 15:59:48 +02:00
|
|
|
(prelude/assert (maybe-nil? nil))
|
|
|
|
(prelude/refute (maybe-nil? t))
|
2019-10-09 13:13:56 +02:00
|
|
|
;; some?
|
2020-08-31 15:59:48 +02:00
|
|
|
(prelude/assert (maybe-some? 10))
|
|
|
|
(prelude/refute (maybe-some? nil))
|
2019-10-09 13:13:56 +02:00
|
|
|
;; nils?
|
2020-08-31 15:59:48 +02:00
|
|
|
(prelude/assert (maybe-nils? nil nil nil nil))
|
|
|
|
(prelude/refute (maybe-nils? nil t nil t))
|
2019-10-09 13:13:56 +02:00
|
|
|
;; somes?
|
2020-08-31 15:59:48 +02:00
|
|
|
(prelude/assert (maybe-somes? t 10 '(1 2 3) "some"))
|
|
|
|
(prelude/refute (maybe-somes? t nil '(1 2 3) "some"))
|
2019-10-09 13:13:56 +02:00
|
|
|
;; default
|
|
|
|
(prelude/assert
|
2020-08-31 15:59:48 +02:00
|
|
|
(and (= 0 (maybe-default 5 0))
|
|
|
|
(= 5 (maybe-default 5 nil))))
|
2019-10-09 13:13:56 +02:00
|
|
|
;; map
|
|
|
|
(prelude/assert
|
2020-08-31 15:59:48 +02:00
|
|
|
(and (= 2 (maybe-map #'1+ 1))
|
|
|
|
(eq nil (maybe-map #'1+ nil)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
(provide 'maybe)
|
|
|
|
;;; maybe.el ends here
|