On Sat, Nov 28, 2015 at 3:37 AM, Aleksandr Mikheev
<[email protected]> wrote:
>
>
> Hi all. I have 3 questions.
>
>
> 1. I still don't understand what exactly parametric types do. I mean what is
> the reason to use
>
>> type A{Int64}
>> A1::Int64
>> A2::Int64
>> end
>
>
> instead of
>
>> type A
>> A1::Int64
>> A2::Int64
>> end

If all what you need is a Int64 version, there's no reason to use a
parametrized type, which is useful when you want to use different
types with similar fields of different field types.

Also note that you usually don't want to use a existing type name as
the name for the type parameter. i.e, for the first version you will
write

type A{T}
    A1::T
    A2::T
end

>
> ?
> Or am I misunderstanding something?
>
> 2. How should I use types like Int16 or Float16 correctly? I have some
> function
>
>> GeometryFunc(L::Int16,phi::Float16,distribution::Array{Float16,2})
>
>
> But when I try to input something like this:
>
>> GeometryFunc(100, 0.3, [0.95 1.01 1.03; 0.25 0.5 0.25])
>
>
> I have got the error.
>
>> ERROR: MethodError: 'GeometryFunc' has no method match no method matching
>> GeometryFunc(::Int64, ::Float64, ::Array{Float64,2})
>
>
> I guess there are some problems with dad I try to input. Or should I try to
> search something in the code?

The direct reason of the error is that you provide Int, Float64,
Array{Float64,2} as the argument but only defined method for Int16,
Float16, Array{Float16,2}.

The way to fix depends on what you actually want.

If your method doesn't depend on Int16 and Float16 (note that Float16
is not useful since you can't do math on it without converting it
first so it is almost certainly not what you want), simply remove the
constraint on the method definition.

If the method has to work with the type you specified, make sure to
only pass the correct type (call it with `GeometryFunc(Int16(100),
Float16(0.3), Float16[0.95 1.01 1.03; 0.25 0.5 0.25])` instead). Or
remove the type constraint and do the convertion in the function.

>
> 3. How can I delete elements with specific indexes in the array? Or how can
> I delete all elements, except ones with specific indexes (this is what I am
> trying to do actually)? For example, I have s = [2, 4, 3, 6, 8, 9, 11] and I
> want to delete 3rd, 5th and 6th elements (or save 1st, 2nd, 4th and 7th). I
> know I could do something like:
>
>> indx = [3, 5, 6]
>>
>> for i = 1:1:length(indx)
>>
>> splice!(s,indx[i])
>>
>> end
>
>
> but is there any easier way? The closest thing I found was:
>
> splice(s, k:m)
>
> which removes all elements with indexes from k to m., but this is not what I
> want exactly.

If the indices are sorted (or can be sorted), just write a loop to
move the element and shrink the storage is probably the fastest.

Reply via email to