ConceptualExample (It is R):
OuterFunc(){
Infunc1(){
#Dostuff to A
}
Infunc2(){
#Dostuff to Aj = 1
function example(j) j += 1end
function outer()
function CladoBuild(tp) tp = "$tp;" AddInternal = quote edge[j, 1] = currentNode node += 1 edge[j, 2] = currentNode = node index[node] = j j += 1 end AddTerminal = quote edge[j, 1] = currentNode edge[j, 2] = tip index[tip] = j tipLabel[tip] = tpc[k] k += 1 tip += 1 j += 1 end GoDown = quote l = index[currentNode] nodeLabel[currentNode - nbTip] = tpc[k] k += 1 currentNode = edge[l, 1] end tsp = split(tp, "") tp = replace(tp, ")", ")NA") tp = replace(tp, "\s", "") tpc = split(tp, r"[\\(\\),;]") tpc = tpc[!bool([i == "" for i in tpc])] skeleton = tsp[bool([i == "(" || i == ")" || i == "," || i == ";" for i in tsp])] nsk = length(skeleton) # Length of the nexus skeleton. nbNode = length(skeleton[bool([i == ")" for i in skeleton])]) # Number of nodes. nbTip = length(skeleton[bool([i == "," for i in skeleton])]) + 1 # Number of tips. nbEdge = nbNode + nbTip # Number of edges for tree is number of tips and nodes. nodeLabel = Array(ASCIIString, nbNode) tipLabel = Array(ASCIIString, nbTip) edge = Array(Int, nbEdge,2) currentNode = node = nbTip + 1 edge[nbEdge, 1] = 0 edge[nbEdge, 2] = node index = Array(Int, nbEdge+1) index[node] = nbEdge j = k = tip = 1 for i in 2:nsk if skeleton[i] == "(" eval(AddInternal) end if skeleton[i] == "," && skeleton[i-1] != ")" eval(AddTerminal) end if skeleton[i] == ")" if skeleton[i - 1] == "," eval(AddTerminal) eval(GoDown) end if skeleton[i - 1] == ")" eval(GoDown) end end end edge = edge[1:nbEdge-1, 1:2] nodeLabel = [replace(i, r"^NA", "") for i in nodeLabel] phyloobject = Clado(edge, tipLabel, nbNode, nodeLabel) return phyloobjectend
I've tried to pass values to the function but it's not quite doing what I want, I'll provide another abstract example that is exhibiting the behaviour :
j = 1
function example(j)j += 1end
What I want if for the outer j to be modified and increased by 1, currently this does not happen, the functions prints 2 to the screen but if I call j again it is still one.
The example you're using is quite hard to discuss because you've chosen an example in which variables are passed by value and not by reference, so that it's very difficult to achieve the effects you're looking for.
julia> beginj = 1example() = (j += 1)end# methods for generic function exampleexample() at none:3julia> exexample exp exp2 expanduser expm1 export exprndexit exp10 expand expm exponent exprjulia> example()2julia> example()3
So if arrays are passed to a function they are passed by reference but a variable such as a = 7 is passed by value?
Yes, all types of variables -- except for a small subset including simple primitives like numbers and strings -- are passed by reference.
I have gone about this another way which appears to mimic these R functions I'm trying to rewrite in Julia, rather than have a function and a load of inner functions to do the task, I have the outer function, and have replaced the inner functions with quote blocks:
function outer()increment = quotej += 1endj = 1eval(increment)end
This is a small example so obviously I would normally just to j+=1 in the function outer with normal code. But I think this should work, I define a function, which at the start I define the quote blocks instead of functions, then eval these blocks later one once I have my variables defined, then the quote blocks when evaluated modify the variables, that way I don't have to have functions in functions confusing scope.
for fn in _numeric_conversion_func_names@eval $fn(z::Complex) = complex($fn(real(z)),$fn(imag(z)))end
function AddInternal(edge, currentNode, node, index, j)
edge[j, 1] = currentNode node += 1 edge[j, 2] = currentNode = node index[node] = j j += 1
return currentNode, node, jend
currentNode, node, j = AddInternal(edge, currentNode, node, index, j)
function AddInternal()
edge[j, 1] = currentNode node += 1 edge[j, 2] = currentNode = node index[node] = j j += 1
end
AddInternal()