According to the docs, if blocks are leaky. I can use it to my advantage with the following function f
, but not with g
, why?
@enum Case A B
f = function (case, x)
if case == A
coeff = 3
elseif case == B
coeff = 8
end
coeff * x
end
g = function (case, x)
if case == A
lambda(x) = 2x
elseif case == B
lambda(x) = x^2
end
lambda(x)
end
f(A, 5) # 15
f(B, 5) # 40
g(A, 5) # 25 .. wait, what?
g(B, 5) # ERROR: lambda not defined.