array: clean up filter()

pull/2558/head
Alexander Medvednikov 2019-10-26 14:53:55 +03:00
parent 70c9565607
commit 6944161b15
2 changed files with 6 additions and 48 deletions

View File

@ -269,7 +269,6 @@ pub fn (a []bool) str() string {
sb.write('true')
} else {
sb.write('false')
}
if i < a.len - 1 {
sb.write(', ')
@ -350,33 +349,6 @@ pub fn (a []char) index(v char) int {
return -1
}
////////////// FILTER //////////////
// Creates a new array with all elements that pass the test implemented by the provided function.
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++ {
if predicate(a[i], i, a) {
res << a[i]
}
}
return res
}
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++ {
if predicate(a[i], i, a) {
res << a[i]
}
}
return res
}
////////////// REDUCE //////////////
// 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 {

View File

@ -309,26 +309,6 @@ fn test_in() {
assert !(0 in a)
}
fn callback_1(val int, index int, arr []int) bool {
return val >= 2
}
fn callback_2(val string, index int, arr []string) bool {
return val.len >= 2
}
fn test_filter2() {
a := [1, 2, 3, 4, 5, 6]
b := a.filter2(callback_1)
assert b[0] == 2
assert b[1] == 3
c := ['v', 'is', 'awesome']
d := c.filter2(callback_2)
assert d[0] == 'is'
assert d[1] == 'awesome'
}
fn sum(prev int, curr int) int {
return prev + curr
}
@ -378,6 +358,12 @@ fn test_map() {
assert d[0] == 'V'
assert d[1] == 'IS'
assert d[2] == 'AWESOME'
bools := c.map(it == 'v')
assert bools.len == 3
assert bools[0] == true
assert bools[1] == false
assert bools[2] == false
}
fn test_array_str() {