835e1bf26a35

Rename generator functions, add a weightlist load form
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 16 Jan 2017 18:18:21 +0000
parents 07621aaf57e2
children e736ba397f2a
branches/tags (none)
files package.lisp src/chancery.lisp

Changes

--- 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
--- 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))