Skip to content

Commit

Permalink
Implement substitution to expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ahojukka5 committed Jun 22, 2020
1 parent 5812cf9 commit e714d07
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/subs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ function subs(s::Symbol, arg::Pair{Symbol, T}) where T <: Union{Int, Float64, Ex
k, v = arg
return s == k ? v : s
end

function subs(ex::Expr, arg::Pair{Symbol, T}) where T <: Union{Int, Float64, Expr}
ne = copy(ex)
for i=2:length(ne.args)
ne.args[i] = subs(ne.args[i], arg)
end
return ne
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ using SymDiff: differentiate

@testset "test_subs_num.jl" begin include("test_subs_num.jl") end
@testset "test_subs_symbol.jl" begin include("test_subs_symbol.jl") end
@testset "test_subs_expr.jl" begin include("test_subs_expr.jl") end
end
4 changes: 4 additions & 0 deletions test/test_subs_expr.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using Test
using SymDiff

@test subs(:(1 + a + b), :a => 1) == :(1 + 1 + b)

0 comments on commit e714d07

Please sign in to comment.