Skip to content

Commit

Permalink
First iteration of a @lift macro. addresses #10
Browse files Browse the repository at this point in the history
  • Loading branch information
shashi committed Jun 17, 2014
1 parent c81e9ab commit 6fa9dd8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/React.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module React
using Base.Order
using Base.Collections

export Signal, Input, Node, signal, lift, map, foldl,
export Signal, Input, Node, signal, lift, @lift, map, foldl,
foldr, merge, filter, droprepeats, sampleon

import Base: push!, foldl, foldr, merge, map,
Expand All @@ -12,6 +12,8 @@ import Base: push!, foldl, foldr, merge, map,
# A signal is a value that can change over time.
abstract Signal{T}

include("macros.jl")

# A topological order
begin
local counter = uint(0)
Expand Down
24 changes: 24 additions & 0 deletions src/macros.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# return a list of symbols in expr that f applies to.
function find_applicable(ex::Expr, f::Function)
unique(find_applicable!(Symbol[], current_module(), ex, f))
end

find_applicable!(L::Vector{Symbol}, M::Module, ex, f::Function) = L
function find_applicable!(L::Vector{Symbol}, M::Module, ex::Symbol, f::Function)
isdefined(M, ex) && applicable(f, eval(M, ex)) ? push!(L, ex) : L
end
function find_applicable!(L::Vector{Symbol}, M::Module, ex::Expr, f::Function)
if ex.head == :call
for arg in ex.args
find_applicable!(L, M, arg, f)
end
end
L
end

macro lift(ex)
S = find_applicable(ex, signal)
Expr(:call, :lift,
Expr(:->, Expr(:tuple, S...), ex),
esc(S...))
end

0 comments on commit 6fa9dd8

Please sign in to comment.