diff options
author | William Carroll <wpcarro@gmail.com> | 2019-10-09T11·13+0100 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2019-12-24T15·21+0000 |
commit | 6b456c1b7a4f6899f063a6e65355af51901d9c7a (patch) | |
tree | cfc70d74818ae9fabdbbfb0cf16cce092e4c1a09 /configs/shared/.emacs.d/wpc/random.el | |
parent | a7c72adb2ebec1e497fc040eaf3551d564d61a5b (diff) |
Massive configuration overhaul
Currently paying the price of months of non-diligent git usage. Here's what has changed. - Theming support in Gvcci and wpgtk - Dropping support for i3 - Supporting EXWM - Many Elisp modules - Collapsed redundant directories in ./configs
Diffstat (limited to 'configs/shared/.emacs.d/wpc/random.el')
-rw-r--r-- | configs/shared/.emacs.d/wpc/random.el | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/configs/shared/.emacs.d/wpc/random.el b/configs/shared/.emacs.d/wpc/random.el new file mode 100644 index 000000000000..148506c04d4e --- /dev/null +++ b/configs/shared/.emacs.d/wpc/random.el @@ -0,0 +1,73 @@ +;;; random.el --- Functions for working with randomness -*- lexical-binding: t -*- +;; Author: William Carroll <wpcarro@gmail.com> + +;;; Commentary: +;; Functions for working with randomness. Some of this code is not as +;; functional as I'd like from. + +;;; Code: + +(require 'prelude) +(require 'number) +(require 'math) +(require 'series) +(require 'list) +(require 'set) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Library +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(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) + "Return a random element of `XS'." + (let ((ct (list/length xs))) + (list/get + (random/int ct) + xs))) + +(defun random/boolean? () + "Randonly return t or nil." + (random/choice (list t nil))) + +;; TODO: This may not work if any of these generate numbers like 0, 1, etc. +(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)))) + +(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/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)))) + +(provide 'random) +;;; random.el ends here |