diff --git a/docs/src/index.md b/docs/src/index.md index f6262c426..ed5da13b8 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -42,6 +42,19 @@ s = @par combine(p, q, r) @assert collect(s) == 16 ``` +or similarly: + +```julia +s = @par begin + p = add1(4) + q = add2(p) + r = add1(3) + combine(p, q, r) +end + +@assert collect(s) == 16 +``` + The connections between nodes `p`, `q`, `r` and `s` is represented by this dependency graph: ![graph](https://user-images.githubusercontent.com/25916/26920104-7b9b5fa4-4c55-11e7-97fb-fe5b9e73cae6.png) diff --git a/src/thunk.jl b/src/thunk.jl index cafb6ef5e..f5125c51e 100644 --- a/src/thunk.jl +++ b/src/thunk.jl @@ -79,14 +79,38 @@ delayedmap(f, xs...) = map(delayed(f), xs...) @par f(args...) -> Thunk Convenience macro to call `Dagger.delayed` on `f` with arguments `args`. +May also be called with a series of assignments like so: + +```julia +x = @par begin + a = f(1,2) + b = g(a,3) + h(a,b) +end +``` + +`x` will hold the Thunk representing `h(a,b)`; additionally, `a` and `b` +will be defined in the same local scope and will be equally accessible +for later calls. """ macro par(ex) - @assert ex.head == :call "@par requires a function call as the argument" - f = ex.args[1] - args = ex.args[2:end] - # TODO: Support kwargs - :(Dagger.delayed(f)($(args...))) + _ex = _par(ex) + dump(_ex) + @show _ex + _ex +end +function _par(ex::Expr) + if ex.head == :call + f = ex.args[1] + args = ex.args[2:end] + # TODO: Support kwargs + return :(Dagger.delayed($(esc(f)))($(_par.(args)...))) + else + return Expr(ex.head, _par.(ex.args)...) + end end +_par(ex::Symbol) = esc(ex) +_par(ex) = ex persist!(t::Thunk) = (t.persist=true; t) cache_result!(t::Thunk) = (t.cache=true; t) diff --git a/test/thunk.jl b/test/thunk.jl index 635b0c160..ec65d0e93 100644 --- a/test/thunk.jl +++ b/test/thunk.jl @@ -5,4 +5,15 @@ b = @par sum([x,1,2]) c = @par a * b @test collect(c) == 20 + + c = @par begin + x = 2 + a = x + x + b = sum([x,1,2]) + c = a * b + end + @test x isa Int + @test a isa Dagger.Thunk + @test c isa Dagger.Thunk + @test collect(c) == 20 end