summary refs log tree commit diff
path: root/mew.scm
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-11-26 18:16:38 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-11-26 18:16:38 +0100
commit9376d7131a9f2a8df55a556110304bd9a2e39c40 (patch)
treef4598ed4a6c49177145e9018d678863094e7180c /mew.scm
parent2a0f96114fb75ecf6975812a041e96f7b9d161c5 (diff)
downloadmew-9376d7131a9f2a8df55a556110304bd9a2e39c40.tar.gz
mew-9376d7131a9f2a8df55a556110304bd9a2e39c40.tar.xz
mew-9376d7131a9f2a8df55a556110304bd9a2e39c40.zip
add sample
Diffstat (limited to 'mew.scm')
-rw-r--r--mew.scm14
1 files changed, 13 insertions, 1 deletions
diff --git a/mew.scm b/mew.scm
index c5f1f31..caed21a 100644
--- a/mew.scm
+++ b/mew.scm
@@ -17,7 +17,7 @@
      odometer one-of op op*
      per prn proj puts
      rand range rep
-     scan scan-right sing? search seq set set-at shuffle str slurp
+     sample scan scan-right sing? search seq set set-at shuffle str slurp
      tally-accumulator tbl time
      while
      uniq-accumulator unlist until
@@ -412,6 +412,18 @@
           (loop (dec i)))))
     v)
 
+  (define (sample o)
+    (if (hash-table? o)
+      (esc ret
+        (let ((n (rand (hash-table-size o)))
+              (i 0))
+          (hash-table-for-each o
+                               (lambda (k v)
+                                 (if (= i n)
+                                   (ret (cons k v))
+                                   (set! i (inc i)))))))
+      (get o (rand (len o)))))
+
   (define range
     (case-lambda
       (()               (make-range-generator 0 +inf.0 1))