Skip to content

tbenst/Thunks.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thunks.jl

Documentation Build Status Coverage

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.

Installation

julia> ] add https://github.com/tbenst/Thunks.jl

Usage

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 == false

A 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) == 6

Limitations

Currently, 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)

Acknowledgements

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.

About

Lazy thunk implementation for Julia.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages