Lint random.el
Usual lints... fixes usage in tree.el.
This commit is contained in:
parent
5b50e34e12
commit
1ea996b676
2 changed files with 37 additions and 29 deletions
|
@ -1,5 +1,9 @@
|
||||||
;;; random.el --- Functions for working with randomness -*- lexical-binding: t -*-
|
;;; random.el --- Functions for working with randomness -*- 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:
|
||||||
;; Functions for working with randomness. Some of this code is not as
|
;; Functions for working with randomness. Some of this code is not as
|
||||||
|
@ -7,6 +11,10 @@
|
||||||
|
|
||||||
;;; Code:
|
;;; Code:
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;; Dependencies
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(require 'prelude)
|
(require 'prelude)
|
||||||
(require 'number)
|
(require 'number)
|
||||||
(require 'math)
|
(require 'math)
|
||||||
|
@ -18,56 +26,56 @@
|
||||||
;; Library
|
;; Library
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(defun random/int (x)
|
(defun random-int (x)
|
||||||
"Return a random integer from 0 to `X'."
|
"Return a random integer from 0 to `X'."
|
||||||
(random x))
|
(random x))
|
||||||
|
|
||||||
;; TODO: Make this work with sequences instead of lists.
|
;; TODO: Make this work with sequences instead of lists.
|
||||||
(defun random/choice (xs)
|
(defun random-choice (xs)
|
||||||
"Return a random element of `XS'."
|
"Return a random element of `XS'."
|
||||||
(let ((ct (list/length xs)))
|
(let ((ct (list/length xs)))
|
||||||
(list/get
|
(list/get
|
||||||
(random/int ct)
|
(random-int ct)
|
||||||
xs)))
|
xs)))
|
||||||
|
|
||||||
(defun random/boolean? ()
|
(defun random-boolean? ()
|
||||||
"Randonly return t or nil."
|
"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.
|
;; 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."
|
"Return a generated UUID string."
|
||||||
(let ((eight (number/dec (math/triangle-of-power :base 16 :power 8)))
|
(let ((eight (number/dec (math/triangle-of-power :base 16 :power 8)))
|
||||||
(four (number/dec (math/triangle-of-power :base 16 :power 4)))
|
(four (number/dec (math/triangle-of-power :base 16 :power 4)))
|
||||||
(twelve (number/dec (math/triangle-of-power :base 16 :power 12))))
|
(twelve (number/dec (math/triangle-of-power :base 16 :power 12))))
|
||||||
(format "%x-%x-%x-%x-%x"
|
(format "%x-%x-%x-%x-%x"
|
||||||
(random/int eight)
|
(random-int eight)
|
||||||
(random/int four)
|
(random-int four)
|
||||||
(random/int four)
|
(random-int four)
|
||||||
(random/int four)
|
(random-int four)
|
||||||
(random/int twelve))))
|
(random-int twelve))))
|
||||||
|
|
||||||
(defun random/token (length)
|
(defun random-token (length)
|
||||||
"Return a randomly generated hexadecimal string of LENGTH."
|
"Return a randomly generated hexadecimal string of LENGTH."
|
||||||
(->> (series/range 0 (number/dec 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 "")))
|
(list/join "")))
|
||||||
|
|
||||||
;; TODO: Support random/sample
|
;; TODO: Support random-sample
|
||||||
(defun random/sample (n xs)
|
;; (defun random-sample (n xs)
|
||||||
"Return a randomly sample of list XS of size N."
|
;; "Return a randomly sample of list XS of size N."
|
||||||
(prelude/assert (and (>= n 0) (< n (list/length xs))))
|
;; (prelude/assert (and (>= n 0) (< n (list/length xs))))
|
||||||
(cl-labels ((do-sample
|
;; (cl-labels ((do-sample
|
||||||
(n xs y ys)
|
;; (n xs y ys)
|
||||||
(if (= n (set/count ys))
|
;; (if (= n (set/count ys))
|
||||||
(->> ys
|
;; (->> ys
|
||||||
set/to-list
|
;; set/to-list
|
||||||
(list/map (lambda (i)
|
;; (list/map (lambda (i)
|
||||||
(list/get i xs))))
|
;; (list/get i xs))))
|
||||||
(if (set/contains? y ys)
|
;; (if (set/contains? y ys)
|
||||||
(do-sample n xs (random/int (list/length xs)) ys)
|
;; (do-sample n xs (random-int (list/length xs)) ys)
|
||||||
(do-sample n xs y (set/add y ys))))))
|
;; (do-sample n xs y (set/add y ys))))))
|
||||||
(do-sample n xs (random/int (list/length xs)) (set/new))))
|
;; (do-sample n xs (random-int (list/length xs)) (set/new))))
|
||||||
|
|
||||||
(provide 'random)
|
(provide 'random)
|
||||||
;;; random.el ends here
|
;;; random.el ends here
|
||||||
|
|
|
@ -125,7 +125,7 @@ generating test data. Warning this function can overflow the stack."
|
||||||
:children (->> (series/range 0 (number/dec bf))
|
:children (->> (series/range 0 (number/dec bf))
|
||||||
(list/map
|
(list/map
|
||||||
(lambda (_)
|
(lambda (_)
|
||||||
(when (random/boolean?)
|
(when (random-boolean?)
|
||||||
(do-random d vf bf))))))))
|
(do-random d vf bf))))))))
|
||||||
(do-random 0 value-fn branching-factor)))
|
(do-random 0 value-fn branching-factor)))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue