-
Notifications
You must be signed in to change notification settings - Fork 12
/
reduce.go
41 lines (35 loc) · 1.13 KB
/
reduce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package slice
import (
"github.com/searKing/golang/go/util/object"
"github.com/searKing/golang/go/util/optional"
)
// ReduceFunc calls a defined callback function on each element of an array, and returns an array that contains the results.
func ReduceFunc(s interface{}, f func(left, right interface{}) interface{}) interface{} {
return normalizeElem(reduceFunc(Of(s), f), s)
}
// reduceFunc is the same as ReduceFunc
func reduceFunc(s []interface{}, f func(left, right interface{}) interface{}, identity ...interface{}) interface{} {
object.RequireNonNil(s, "reduceFunc called on nil slice")
object.RequireNonNil(f, "reduceFunc called on nil callfn")
var foundAny bool
var result interface{}
if identity != nil || len(identity) != 0 {
foundAny = true
result = identity
}
for _, r := range s {
if !foundAny {
foundAny = true
result = r
} else {
result = f(result, r)
}
}
if foundAny {
return optional.Of(result).Get()
}
return optional.Empty().Get()
}