Thunks.jl provides a simple implementation of a Thunk for lazy computation.
A thunk represents a computation that is not run until we reify it,
meaning make "real". Once reified, the thunk caches the value of the
computation. The core implementation is only 25 LOC, so consider taking
a peak.
julia> ] add https://github.com/tbenst/Thunks.jl
Note that the below example will execute nearly instantly due to laziness, whereas the eager equivalent would take a minute.
w = thunk(sleep)(60)
x = thunk(identity)(2)
y = @thunk identity(3)
@thunk yy = identity(3)
z = thunk(+)(x, y)
@assert z.evaluated == false
@assert reify(z) == 5
@assert z.evaluated == true
@assert w.evaluated == falseA macro is also provided for convenience:
abc = @thunk begin
w = sleep(60)
a = 2
b = 3
c = 1
sum([a,b,c])
end
@assert reify(abc) == 6Currently, naked indexing is not supported:
i = 10
x = @thunk collect(1:i)[7:end]
@assert isnothing(x)
x = thunk(collect)(1:i)[7:end]
ERROR: MethodError: no method matching lastindex(::Thunk)This can be worked around by wrapping in a function
x = @thunk ((x)->collect(1:x)[7:end])(i)Thunks.jl is inspired by the Thunk implementation of the fantastic Dagger.jl and is intended as a lightweight, more performant alternative without the scheduling capabilities.