加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
eg11.18-students.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
Letotn 提交于 2023-01-10 13:02 . update eg.11.18
package main
import "fmt"
type Any interface{}
type Exam struct {
Name string
Course string
Score int
}
type Exams []*Exam
func (e Exams) Process(f func(ex *Exam)) {
for _, exam := range e {
f(exam)
}
}
func (E Exams) FindAll(fn func(ex *Exam) bool) Exams {
res := make([]*Exam, 0)
for _, exam := range E {
if fn(exam) {
res = append(res, exam)
}
}
return res
}
func (E Exams) Map(fn func(ex *Exam) Any) []Any {
res := make([]Any, len(E))
ix := 0
E.Process(func(e *Exam) {
res[ix] = fn(e)
ix++
})
return res
}
func MakeSortedAppender(courses []string) (func(e *Exam), map[string]Exams) {
sortedExams := make(map[string]Exams)
for _, course := range courses {
sortedExams[course] = make([]*Exam, 0)
}
sortedExams["Default"] = make([]*Exam, 0)
appender := func(e *Exam) {
if _, ok := sortedExams[e.Course]; ok {
sortedExams[e.Course] = append(sortedExams[e.Course], e)
} else {
sortedExams["Default"] = append(sortedExams["Default"], e)
}
}
return appender, sortedExams
}
func main() {
exam1 := &Exam{"Zhongli", "talk", 90}
exam2 := &Exam{"Zhongli", "power", 90}
exam3 := &Exam{"Gongzi", "talk", 60}
exam4 := &Exam{"Gongzi", "power", 60}
exam5 := &Exam{"Hutao", "talk", 60}
exam6 := &Exam{"Hutao", "power", 90}
allExams := Exams{exam1, exam2, exam3, exam4, exam5, exam6}
talkHightScore := allExams.FindAll(func(e *Exam) bool {
return e.Course == "talk" && e.Score >= 90
})
fmt.Println(talkHightScore)
courses := []string{"talk", "power", "weapon"}
appender, sortedExams := MakeSortedAppender(courses)
allExams.Process(appender)
fmt.Println(sortedExams)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化