On Mon, 2015-04-13 at 16:22, Tamas Papp <[email protected]> wrote:
> On Mon, Apr 13 2015, Mauro <[email protected]> wrote:
>
>> Why can you not make it into a function?
>>
>> function foo!(spec::Symbol, A, i)
>>     A[i,spec] = 1
>> end
>> function foo!{T<:Real}(spec::Vector{(Symbol,T), A, i)
>>       @assert(isapprox(sum(map(x -> x[2],spec)),1))
>>       for (obs,prob) = spec
>>         A[i,obs] = prob
>>       end
>>     A[i,spec] = 1
>> end
>> foo!(spec, A, i)  = error(" ...")
>>
>> that way your code will be cleaner too:
>>
>> foo!(spec, A, i)
>
> Sure, I can do this, but since foo! would be used in one place only, I
> would consider this suboptimal design.
>
>> In Julia, when you use an isa, then ask yourself whether it might not be
>> cleaner using methods instead.
>
> I am not sure I understand the reasoning behind this. I find polluting
> the namespace with wrapping up trivial code that is used in exactly one
> place icky.

Then you have to use Toivo's trick or wait for
https://github.com/JuliaLang/julia/issues/8974 which should make this
possible (as far as I understand it).

I've used above approach and I like it.  If foo! is well named then
reading of the main function becomes much easier.

Reply via email to