V: array.find() / array.index_of()

Created on 1 Oct 2019  路  3Comments  路  Source: vlang/v

Can we have a function that searches for a given element by value and

  • returns its first index, if found
  • or a -1 or error, if not found
    ?

Basically what "a in b" does for arrays, only returning an int instead of a bool.
re: naming:
.find(value) or
.index_of(value) or
.first_index_of(value) or

Docs should make clear that it returns the first index of the element that has the same value as the provided one.

Thanks.

Feature Request

Most helpful comment

I think it could be like C#, simple and clean. For example:

arr := [1,2,3,4]
elem := arr.find(3)? // returns ?int
elem_index := arr.first(predicate)? // returns ?T
elements := arr.find_all(predicate)? // returns ?[]T or []T to check

it would be possibile to write a lot of useful and well documented functions, avoiding the boring and verbose Go style

All 3 comments

I think find(value) will be better?

I think it could be like C#, simple and clean. For example:

arr := [1,2,3,4]
elem := arr.find(3)? // returns ?int
elem_index := arr.first(predicate)? // returns ?T
elements := arr.find_all(predicate)? // returns ?[]T or []T to check

it would be possibile to write a lot of useful and well documented functions, avoiding the boring and verbose Go style

Was this page helpful?
0 / 5 - 0 ratings

Related issues

taojy123 picture taojy123  路  3Comments

choleraehyq picture choleraehyq  路  3Comments

markgraydev picture markgraydev  路  3Comments

fidergo-stephane-gourichon picture fidergo-stephane-gourichon  路  3Comments

jtkirkpatrick picture jtkirkpatrick  路  3Comments