1 Star 0 Fork 1

常清静矣/golang-100

forked from 蓝桥云课/golang-100 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
008-filterInt.go 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
konroyliu 提交于 2021-01-11 10:51 . golang-100 answer
package main
import "fmt"
func FilterInt(arr []int, f func(int) bool) []int {
arf := make([]int, 0)
for _, v := range arr {
if f(v) {
arf = append(arf, v)
}
}
return arf
}
func FilterFloat64(arr []float64, f func(float64) bool) []float64 {
arf := make([]float64, 0)
for _, v := range arr {
if f(v) {
arf = append(arf, v)
}
}
return arf
}
func FilterBool(arr []bool, f func(bool) bool) []bool {
arf := make([]bool, 0)
for _, v := range arr {
if f(v) {
arf = append(arf, v)
}
}
return arf
}
func FilterString(arr []string, f func(string) bool) []string {
arf := make([]string, 0)
for _, v := range arr {
if f(v) {
arf = append(arf, v)
}
}
return arf
}
func main() {
intCheck := func(x int) bool { return x > 1 }
fmt.Println(FilterInt([]int{0, 2}, intCheck)) // [2]
float64Check := func(x float64) bool { return x > 0.5 }
fmt.Println(FilterFloat64([]float64{0.0, 1.0}, float64Check)) // [1.0]
boolCheck := func(x bool) bool { return x }
fmt.Println(FilterBool([]bool{false, true}, boolCheck)) // [true]
stringCheck := func(x string) bool { return len(x) > 1 }
fmt.Println(FilterString([]string{"", "hi"}, stringCheck)) // ["hi"]
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/syongaaa/golang-100.git
git@gitee.com:syongaaa/golang-100.git
syongaaa
golang-100
golang-100
master

搜索帮助