diff options
author | William Carroll <wpcarro@gmail.com> | 2020-08-31T13·51+0100 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-08-31T13·51+0100 |
commit | 1ea996b6765c6ca0bf0705ce5faade8326c0c93b (patch) | |
tree | d142b1d97cd69238b93780756f5c5bbf78305a96 /emacs/.emacs.d/wpc/random.el | |
parent | 5b50e34e12a33520c0d0f42fca25238513511eac (diff) |
Lint random.el
Usual lints... fixes usage in tree.el.
Diffstat (limited to 'emacs/.emacs.d/wpc/random.el')
-rw-r--r-- | emacs/.emacs.d/wpc/random.el | 64 |
1 files changed, 36 insertions, 28 deletions
diff --git a/emacs/.emacs.d/wpc/random.el b/emacs/.emacs.d/wpc/random.el index 148506c04d4e..6ffdbd4de04a 100644 --- a/emacs/.emacs.d/wpc/random.el +++ b/emacs/.emacs.d/wpc/random.el @@ -1,5 +1,9 @@ ;;; random.el --- Functions for working with randomness -*- lexical-binding: t -*- + ;; Author: William Carroll <wpcarro@gmail.com> +;; Version: 0.0.1 +;; Package-Requires: ((emacs "24")) +;; Homepage: https://user.git.corp.google.com/wpcarro/briefcase ;;; Commentary: ;; Functions for working with randomness. Some of this code is not as @@ -7,6 +11,10 @@ ;;; Code: +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Dependencies +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (require 'prelude) (require 'number) (require 'math) @@ -18,56 +26,56 @@ ;; Library ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defun random/int (x) +(defun random-int (x) "Return a random integer from 0 to `X'." (random x)) ;; TODO: Make this work with sequences instead of lists. -(defun random/choice (xs) +(defun random-choice (xs) "Return a random element of `XS'." (let ((ct (list/length xs))) (list/get - (random/int ct) + (random-int ct) xs))) -(defun random/boolean? () +(defun random-boolean? () "Randonly return t or nil." - (random/choice (list t nil))) + (random-choice (list t nil))) ;; TODO: This may not work if any of these generate numbers like 0, 1, etc. -(defun random/uuid () +(defun random-uuid () "Return a generated UUID string." (let ((eight (number/dec (math/triangle-of-power :base 16 :power 8))) (four (number/dec (math/triangle-of-power :base 16 :power 4))) (twelve (number/dec (math/triangle-of-power :base 16 :power 12)))) (format "%x-%x-%x-%x-%x" - (random/int eight) - (random/int four) - (random/int four) - (random/int four) - (random/int twelve)))) + (random-int eight) + (random-int four) + (random-int four) + (random-int four) + (random-int twelve)))) -(defun random/token (length) +(defun random-token (length) "Return a randomly generated hexadecimal string of LENGTH." (->> (series/range 0 (number/dec length)) - (list/map (lambda (_) (format "%x" (random/int 15)))) + (list/map (lambda (_) (format "%x" (random-int 15)))) (list/join ""))) -;; TODO: Support random/sample -(defun random/sample (n xs) - "Return a randomly sample of list XS of size N." - (prelude/assert (and (>= n 0) (< n (list/length xs)))) - (cl-labels ((do-sample - (n xs y ys) - (if (= n (set/count ys)) - (->> ys - set/to-list - (list/map (lambda (i) - (list/get i xs)))) - (if (set/contains? y ys) - (do-sample n xs (random/int (list/length xs)) ys) - (do-sample n xs y (set/add y ys)))))) - (do-sample n xs (random/int (list/length xs)) (set/new)))) +;; TODO: Support random-sample +;; (defun random-sample (n xs) +;; "Return a randomly sample of list XS of size N." +;; (prelude/assert (and (>= n 0) (< n (list/length xs)))) +;; (cl-labels ((do-sample +;; (n xs y ys) +;; (if (= n (set/count ys)) +;; (->> ys +;; set/to-list +;; (list/map (lambda (i) +;; (list/get i xs)))) +;; (if (set/contains? y ys) +;; (do-sample n xs (random-int (list/length xs)) ys) +;; (do-sample n xs y (set/add y ys)))))) +;; (do-sample n xs (random-int (list/length xs)) (set/new)))) (provide 'random) ;;; random.el ends here |