Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement higher order function Zip #152

Open
zsoerenm opened this issue Aug 1, 2017 · 4 comments
Open

Implement higher order function Zip #152

zsoerenm opened this issue Aug 1, 2017 · 4 comments

Comments

@zsoerenm
Copy link

zsoerenm commented Aug 1, 2017

In #34 you say that zip is redundant to lift(tuple, x, y, z)
However lift is deprecated in favor of map: https://github.com/JuliaGizmos/Reactive.jl/blob/master/src/deprecation.jl#L6

But doesn't map "fire" each time there is new value in either of the two signals, that are to be combined?

On the other hand zip "fires" only when both signals are changed: http://rxmarbles.com/#zip

@JobJob
Copy link
Member

JobJob commented Aug 1, 2017

Yeah you're right. I think you could implement zip with foldp and filter. PR welcome if you get it going. If you're stuck let us know, and someone might be able to help.

@zsoerenm
Copy link
Author

zsoerenm commented Aug 2, 2017

I think you could implement zip with foldp and filter

Could elaborate more on this? Which reducing function for foldp do you have in mind?

@zsoerenm
Copy link
Author

zsoerenm commented Aug 2, 2017

To my surprise map works like zip if I have a "diamond" shape of dataflow:

julia > begin
       as = Signal(0.0)
       b1s = map(a -> a + 1, as)
       b2s = map(a -> a + 2, as)
       cs = map((b1, b2) -> b1 + b2, b1s, b2s)
       ds = foldp((cnt, _) -> cnt + 1, 0, cs)
       push!(as, 1)
       yield()
       @test value(ds) == 2
       end
Test Failed
  Expression: value(ds) == 2
   Evaluated: 1 == 2

This even works when the calculation for either b1s or b2s takes considerably longer than the other. Is this indended?

@JobJob
Copy link
Member

JobJob commented Aug 2, 2017

Yes. The single push causes the update to flow through the Signal graph just once, i.e. Reactive aims to re-run a minimal set of functions when a Signal is pushed to.

Had a little play and here is a first stab at a zip

function Iterators.zip(a::Signal, b::Signal)
    been_updated = [false, false]
    past_vals = Dict(:a=>[], :b=>[])
    foreach(a, init=false) do av
        push!(past_vals[:a], av)
        been_updated[1] = true
    end
    foreach(b, init=false) do bv
        push!(past_vals[:b], bv)
        been_updated[2] = true
    end
    zipvals = map(a, b, init=(value(a), value(b))) do av, bv
        !isempty(past_vals[:a]) && (av = past_vals[:a][1])
        !isempty(past_vals[:b]) && (bv = past_vals[:b][1])
        av,bv
    end
    the_zip = filter(value(zipvals), zipvals) do vals
        time_to_send = all(been_updated)
        if time_to_send 
            been_updated[:] .= false
            shift!(past_vals[:a])
            shift!(past_vals[:b])
        end
        time_to_send
    end
    the_zip
end

Works for the following, tested in IJulia (with using Interact to see the signal updating after each push), the #--- indicate cell boundaries.

using Interact
a = Signal(0)
b = Signal(0)
zz = zip(a,b)
#---
push!(a, 1)
#---
push!(b, 2)
#---
# zz updates to (1,2)
push!(a, 3)
#---
push!(a, 4)
#---
push!(b, 5)
# zz updates to (3,5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants