From 1f0b56cc5ab3e13427d7a035cfb0ffa8166280cc Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Fri, 14 Apr 2023 18:55:53 +0200 Subject: [PATCH] Add gint to apply a function with a probability --- gists.org | 10 ++++++++++ gists/apply-maybe.el | 5 +++++ 2 files changed, 15 insertions(+) create mode 100644 gists/apply-maybe.el diff --git a/gists.org b/gists.org index 1769fed..59f7bdd 100644 --- a/gists.org +++ b/gists.org @@ -527,6 +527,16 @@ It turns out it didn't work as well as I hoped, paredit steals the RET binding s :post-process-template #'my/dwim-shell-command/convert-path-cygwin)) #+end_src +* Apply maybe + +#+begin_src elisp :tangle gists/apply-maybe.el + (defun my/apply-maybe (f probability &rest args) + "Apply function F with a certain PROBABILITY [0-1)." + (if (< (random 100) (* probability 100)) + (apply f args) + 'my/not-applied)) +#+end_src + * Meta ** License diff --git a/gists/apply-maybe.el b/gists/apply-maybe.el new file mode 100644 index 0000000..2614c48 --- /dev/null +++ b/gists/apply-maybe.el @@ -0,0 +1,5 @@ +(defun my/apply-maybe (f probability &rest args) + "Apply function F with a certain PROBABILITY [0-1)." + (if (< (random 100) (* probability 100)) + (apply f args) + 'my/not-applied))