1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

array: optimized generic filter()

This commit is contained in:
Alexander Medvednikov
2019-10-17 10:44:20 +03:00
parent eda0c73bef
commit 80e79a3966
6 changed files with 67 additions and 7 deletions

View File

@@ -332,7 +332,7 @@ pub fn (a []char) index(v char) int {
////////////// FILTER //////////////
// Creates a new array with all elements that pass the test implemented by the provided function.
pub fn (a []string) filter(predicate fn(p_val string, p_i int, p_arr []string) bool) []string
pub fn (a []string) filter2(predicate fn(p_val string, p_i int, p_arr []string) bool) []string
{
mut res := []string
for i := 0; i < a.len; i++ {
@@ -343,7 +343,7 @@ pub fn (a []string) filter(predicate fn(p_val string, p_i int, p_arr []string)
return res
}
pub fn (a []int) filter(predicate fn(p_val, p_i int, p_arr []int) bool) []int
pub fn (a []int) filter2(predicate fn(p_val, p_i int, p_arr []int) bool) []int
{
mut res := []int
for i := 0; i < a.len; i++ {
@@ -356,7 +356,7 @@ pub fn (a []int) filter(predicate fn(p_val, p_i int, p_arr []int) bool) []int
////////////// REDUCE //////////////
// Executes a reducer function (that you provide) on each element of the array,
// Executes a reducer function (that you provide) on each element of the array,
// resulting in a single output value.
pub fn (a []int) reduce(iter fn (accum, curr int) int, accum_start int) int {
mut _accum := 0