Le jeudi 07 janvier 2016 à 01:49 -0800, Tomas Lycken a écrit :
> Two common options in many 0-indexed languages (Java and C#, to name
> a couple of static ones; JavaScript and Python, to name a couple of
> dynamic) is to return either -1 or length(iterator), i.e. either the
> index before the first item, or the index after the last (-1 is by
> far the more common of the two) if no matching items are found. Since
> Julia is 1-indexed, I would say 0 or lenght(iterator) + 1 makes just
> as much sense. I could even imagine returning -1, despite having no
> obvious relation to the indices of the elements in the container,
> just because it’s familiar to many developers coming from other
> languages.
I would choose 0 for consistency with e.g. findfirst(), and because
Bool(0) == false.


> If you choose to return a Nullable{Int} (which is a fine choice), you
> should return a Nullable{Int} also when a match is found; in other
> words, if the third element matches, return Nullable(3), while if no
> element matches return Nullable{Int}(). This will preserve type
> stability and thereby not hurt performance (at least not for that
> reason :) ).
> 
> // T
> 
> On Wednesday, January 6, 2016 at 11:06:11 PM UTC+1, Erik Schnetter
> wrote:
> 
> > I would return 0 (the integer zero) or a negative number for
> > entries 
> > that are not found. This way, the return type of the function is
> > still 
> > Int. This is relevant for type stability, i.e. performance. 
> > 
> > -erik 
> > 
> > On Wed, Jan 6, 2016 at 5:01 PM, Alex <[email protected]> wrote: 
> > > Hello, 
> > > 
> > > Let's say I write a function which checks if a given array/vector
> > > contains a certain value and returns it's index. 
> > > 
> > > What should the function return if array doesn't contain the
> > value? 
> > > 
> > > I read carefully the section "Nothingness and missing values": 
> > > http://docs.julialang.org/en/latest/manual/faq/#nothingness-and-m
> > issing-values 
> > > 
> > > 
> > > but I don't get from it what would be a correct "julian"
> > solution. 
> > > 
> > > -- Alex 
> > 
> > 
> > 
> > 

Reply via email to