Le mercredi 04 février 2015 à 23:11 -0800, Wai Yip Tung a écrit : > I have successfully defined a custom array Ngram. > > > > type Ngram <: AbstractArray{ASCIIString,1} > seq::ASCIIString > n::Int > end > > > function getindex(s::Ngram, i::Int) > s.seq[i:i+s.n-1] > end > > > function Base.size(s::Ngram) > > length(s.seq) - s.n + 1 > end > > > > > It works as I expected. For example > > > In [219]: > > > > ng=SeqUtil.Ngram("hello",2) > > > Out[219]: > 4-element Ngram: > "he" > "el" > "ll" > "lo" > > > But it seems in other usage, a corresponding `similar` method is > necessary > > > In [222]: > > > > map(print,ng) > > > > he > > `similar` has no method matching similar(::Ngram, ::Type{Nothing}, ::(Int32,)) > while loading In[222], in expression starting on line 1 > > in similar at abstractarray.jl:116 > in map at abstractarray.jl:1329 > > > I have read the documentation and I still don't get what I need to do > in the similar method. You help will be appreciated. That method should just create an array of the same type, with the same element type and dimensions by default, but accepting arguments to override these two values. For example, you can see what that function does for standard Arrays with: @edit similar([1:3]). In the case of map(f, ng), the goal is to create an array to hold the result: its element type must be that of the result of calling f on an element of ng, and its dimensions must be the the same as ng.
The problem with Ngram is that it cannot store the result of calling map() on it. So I'm not sure what's the best solution here. Looks like Ngram is not really an AbstractArray, only an iterable and indexable, in which case a different map() method would be used. Then you wouldn't implement similar()., but rather the start()/next()/done() iteration protocol. Regards