bram85's Blog

bram85's Avatar Image
Tooting mostly about #emacs.
← All posts

#elisp (apply-maybe)

Execute a function with a certain probability.

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

My use-case is to export a (large) #orgmode file to PDF only once in ~10 saves with an after-save-hook. So I automatically get a more-or-less up to date PDF without having to wait.

I’m aware of async exports but for some reason it’s quite a hassle to get right. apply-maybe provides a good trade-off for the time being.

Curious if you can come up with other use-cases.

https://apps.bram85.nl/git/bram/gists/src/commit/1f0b56cc5ab3e13427d7a035cfb0ffa8166280cc/gists/apply-maybe.el

#emacs

To like or reply, open original post on Emacs.ch