2019-10-09 13:13:56 +02:00
|
|
|
;;; stack.el --- Working with stacks in Elisp -*- 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 "25.1"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; A stack is a LIFO queue.
|
|
|
|
;; The design goal here is to expose an intuitive API for working with stacks in
|
|
|
|
;; non-mutative way.
|
|
|
|
;;
|
|
|
|
;; TODO: Consider naming a Functor instance "Mappable."
|
|
|
|
;; TODO: Consider naming a Foldable instance "Reduceable."
|
|
|
|
;;
|
|
|
|
;; TODO: Consider implementing an instance for Mappable.
|
|
|
|
;; TODO: Consider implementing an instance for Reduceable.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2020-09-01 17:26:39 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 17:26:39 +02:00
|
|
|
(require 'list)
|
|
|
|
(require '>)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Create
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 17:26:39 +02:00
|
|
|
(cl-defstruct stack xs)
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun stack-new ()
|
2019-10-09 13:13:56 +02:00
|
|
|
"Create an empty stack."
|
|
|
|
(make-stack :xs '()))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun stack-from-list (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Create a new stack from the list, `XS'."
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-reduce (stack-new) #'stack-push xs))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Read
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun stack-peek (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Look at the top element of `XS' without popping it off."
|
|
|
|
(->> xs
|
|
|
|
stack-xs
|
2022-08-05 21:34:15 +02:00
|
|
|
list-first))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Update
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun stack-push (x xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Push `X' on `XS'."
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-update stack
|
2019-10-09 13:13:56 +02:00
|
|
|
xs
|
2020-09-01 17:26:39 +02:00
|
|
|
(>-> (list-cons x))
|
2019-10-09 13:13:56 +02:00
|
|
|
xs))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
;; TODO: How to return something like {(list-head xs), (list-tail xs)} in Elixir
|
2019-10-09 13:13:56 +02:00
|
|
|
;; TODO: How to handle popping from empty stacks?
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun stack-pop (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the stack, `XS', without the top element.
|
|
|
|
Since I cannot figure out a nice way of return tuples in Elisp, if you want to
|
2020-09-01 11:17:43 +02:00
|
|
|
look at the first element, use `stack-peek' before running `stack-pop'."
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-update stack
|
2019-10-09 13:13:56 +02:00
|
|
|
xs
|
2020-09-01 17:26:39 +02:00
|
|
|
(>-> list-tail)
|
2019-10-09 13:13:56 +02:00
|
|
|
xs))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun stack-map-top (f xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Apply F to the top element of XS."
|
|
|
|
(->> xs
|
2020-09-01 11:17:43 +02:00
|
|
|
stack-pop
|
|
|
|
(stack-push (funcall f (stack-peek xs)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Miscellaneous
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun stack-to-list (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return XS as a list.
|
2020-09-01 11:17:43 +02:00
|
|
|
The round-trip property of `stack-from-list' and `stack-to-list' should hold."
|
2019-10-09 13:13:56 +02:00
|
|
|
(->> xs
|
|
|
|
stack-xs
|
2020-09-01 11:17:43 +02:00
|
|
|
list-reverse))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Predicates
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;; TODO: Create a macro that wraps `cl-defstruct' that automatically creates
|
|
|
|
;; things like `new', `instance?'.
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun stack-instance? (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if XS is a stack."
|
|
|
|
(stack-p xs))
|
|
|
|
|
|
|
|
(provide 'stack)
|
|
|
|
;;; stack.el ends here
|