This is probably a bit of a newbie question (and possibly going off topic)
but I typically use closures to
accomplish what Keno's let block does. For example,
a = 10
let a = a
global f1
f1(x) = x*a
end
function genf(a)
f(x) = x*a
return f
end
f2 = genf(a)
My question is: is there any real difference (performance or otherwise)
between f1 and f2 above?
On Friday, October 9, 2015 at 10:57:50 AM UTC-7, Keno Fischer wrote:
And since this is a scoping problem, `let` is your friend
>
> ```
> julia> a = 20
>
> julia> let a = a
> global f
> f(x) = x*a
> end
> f (generic function with 1 method)
>
> julia> f(20)
> 400
>
> julia> a = 40
> 40
>
> julia> f(20)
> 400
> ```
>
> On Fri, Oct 9, 2015 at 10:36 AM, Tomas Lycken <[email protected]
> <javascript:>> wrote:
>
>> Then don't use the same variable ;)
>>
>> ```
>> julia> a = 10;
>>
>> julia> _a = a;
>>
>> julia> f(x) = _a * x;
>>
>> julia> f(2)
>> 20
>>
>> julia> a = 20;
>>
>> julia> f(2)
>> 20
>> ```
>>
>> This has nothing to do with pass-by-sharing, but rather with scoping -
>> the manual talks quite a lot about this (but don't hesitate to suggest
>> improvements if something is unclear to you!)
>> http://docs.julialang.org/en/release-0.4/manual/variables-and-scoping/
>>
>> // T
>>
>>
>> On Friday, October 9, 2015 at 4:26:38 PM UTC+2, Thuener Silva wrote:
>>>
>>> I understand the pass-by-sharing concept, but if I want to use only the
>>> value of a variable?
>>> Simple example to illustrate what I'm trying to say:
>>>
>>> julia> a = 10
>>> 10
>>>
>>> julia> f(x) = a*x
>>> f (generic function with 1 method)
>>>
>>> julia> f(2)
>>> 20
>>>
>>> julia> a = 20
>>> 20
>>>
>>> julia> f(2)
>>> 40
>>>
>>> I don't wan to to change the function when I change the value of a.
>>>
>>> Grats,
>>> Thuener Silva
>>>
>>
>