To be able to edit code and run cells, you need to run the notebook yourself. Where would you like to run the notebook?

This notebook takes about 20 seconds to run.

In the cloud (experimental)

Binder is a free, open source service that runs scientific notebooks in the cloud! It will take a while, usually 2-7 minutes to get a session.

On your computer

(Recommended if you want to store your changes.)

  1. Copy the notebook URL:
  2. Run Pluto

    (Also see: How to install Julia and Pluto)

  3. Paste URL in the Open box

Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

Author 1
throttled

Create a throttled function, which calls the given function f at most once per given interval max_delay.

It is leading (f is invoked immediately) and not trailing (calls during a cooldown period are ignored).

👀 Reading hidden code
"Create a throttled function, which calls the given function `f` at most once per given interval `max_delay`.

It is _leading_ (`f` is invoked immediately) and _not trailing_ (calls during a cooldown period are ignored)."
function throttled(f::Function, max_delay::Real, initial_offset::Real=0)
local last_run_at = time() - max_delay + initial_offset
# return f
() -> begin
now = time()
f()
last_run_at = now
end
nothing
end
end
2.0 ms
log (generic function with 1 method)
👀 Reading hidden code
function log()
@info "Hello" rand()
end
7.1 ms
👀 Reading hidden code
log()
Hello
rand()
0.3362369120617531
335 ms
#1 (generic function with 1 method)
t = throttled(log, 2, 5)
👀 Reading hidden code
18.4 μs
t()
👀 Reading hidden code
3.0 ms
0.03041815757751465
let
a = time()
b = sqrt.([200])
time() - a
end
👀 Reading hidden code
30.4 ms

With cooldown delay

👀 Reading hidden code
195 μs
throttled2

Create a throttled function, which calls the given function f at most once per given interval max_delay.

It is leading (f is invoked immediately) and not trailing (calls during a cooldown period are ignored).

An optional third argument sets an initial cooldown period, default is 0. With a non-zero value, the throttle is no longer leading.

"Create a throttled function, which calls the given function `f` at most once per given interval `max_delay`.

It is _leading_ (`f` is invoked immediately) and _not trailing_ (calls during a cooldown period are ignored).

An optional third argument sets an initial cooldown period, default is `0`. With a non-zero value, the throttle is no longer _leading_."
function throttled2(f::Function, max_delay::Real, initial_offset::Real=0)
local last_run_at = time() - max_delay + initial_offset
# return f
() -> begin
now = time()
if now - last_run_at >= max_delay
f()
last_run_at = now
end
nothing
end
end
👀 Reading hidden code
1.8 ms

👀 Reading hidden code
67.5 μs

👀 Reading hidden code
66.9 μs