# HG changeset patch # User Steve Losh # Date 1484590701 0 # Node ID 835e1bf26a35e7a661da12d53d0d3e55371c4a6a # Parent 07621aaf57e27c52178db2c20726716a93aebbb6 Rename generator functions, add a weightlist load form diff -r 07621aaf57e2 -r 835e1bf26a35 package.lisp --- a/package.lisp Mon Jan 16 13:03:29 2017 +0000 +++ b/package.lisp Mon Jan 16 18:18:21 2017 +0000 @@ -2,11 +2,14 @@ (:use :cl :chancery.quickutils) (:export :define-rule + :define-string + :gen + :gen-string :quote - :bind - :bind* :eval + :list + :vector :cap :cap-all diff -r 07621aaf57e2 -r 835e1bf26a35 src/chancery.lisp --- a/src/chancery.lisp Mon Jan 16 13:03:29 2017 +0000 +++ b/src/chancery.lisp Mon Jan 16 18:18:21 2017 +0000 @@ -71,12 +71,6 @@ :sums (prefix-sums weights) :total (apply #'+ 0.0 weights))) -(defun weightlist-random (weightlist) - "Return a random item from the weightlist, taking the weights into account." - (loop :with n = (random (weightlist-total weightlist)) - :for item :in (weightlist-items weightlist) - :for weight :in (weightlist-sums weightlist) - :when (< n weight) :do (return item))) (defmethod print-object ((wl weightlist) s) (print-unreadable-object (wl s :type t) @@ -85,6 +79,19 @@ (weightlist-items wl)) s))) +(defmethod make-load-form ((wl weightlist) &optional environment) + (make-load-form-saving-slots wl + :slot-names '(weights sums items total) + :environment environment)) + + +(defun weightlist-random (weightlist) + "Return a random item from the weightlist, taking the weights into account." + (loop :with n = (random (weightlist-total weightlist)) + :for item :in (weightlist-items weightlist) + :for weight :in (weightlist-sums weightlist) + :when (< n weight) :do (return item))) + (defun build-weightlist-uniform (values) (make-weightlist values (loop :for nil :in values :collect 1))) @@ -162,7 +169,7 @@ (defmacro define-rule (name-and-options &rest expressions) (build-define-rule 'evaluate-expression name-and-options expressions)) -(defmacro generate (expression) +(defmacro gen (expression) "Generate a single Chancery expression." `(evaluate-expression ',expression)) @@ -230,7 +237,7 @@ " (build-define-rule 'evaluate-string-expression name-and-options expressions)) -(defmacro generate-string (expression) +(defmacro gen-string (expression) "Generate a single Chancery string expression." `(evaluate-string-expression ',expression))