英文:
Elegant way to convert a slice of one type to a slice of an equivalent type?
问题
type Job struct {
weight int
length int
}
type Strategy func([]Job) []Job
func Schedule(jobs []Job, strat Strategy) []Job {
return strat(jobs)
}
func MinCompletionTimes(job []Job) []Job {
// Hmm...
}
type JobSlice []Job // Should probably be called MinCompletionTimesJobSlice
func (js JobSlice) Len() {
return len(js)
}
func (js JobSlice) Less(i, j int) bool {
return js[i].length < js[j].length
}
func (js JobSlice) Swap(i, j int) {
js[i], js[j] = js[j], js[i]
}
func MinCompletionTimes(jobs []Job) []Job {
sort.Sort([]JobSlice(jobs)) // cannot convert jobs (type []Job) to type []JobSlice
return jobs
}
Er...
英文:
A motivating example:
Implementing various scheduling "strategies", which sort a list of Jobs.
type Job struct {
weight int
length int
}
// Given a slice of Jobs, re-order them.
type Strategy func([]Job) []Job
func Schedule(jobs []Job, strat Strategy) []Job {
return strat(jobs)
}
One very simple strategy is to execute the shortest jobs first (disregarding their weight/priority).
func MinCompletionTimes(job []Job) []Job {
// Hmm...
}
Well, this strategy is nothing more than a sort on job.length, so let's use the sort package. Define a custom type, and implement sort.Interface...
type JobSlice []Job // Should probably be called MinCompletionTimesJobSlice
func (js JobSlice) Len() {
return len(js)
}
func (js JobSlice) Less(i, j int) bool {
return js[i].length < js[j].length
}
func (js JobSlice) Swap(i, j int) {
js[i], js[j] = js[j], js[i]
}
Hooray, now to back to our simple strategy...
func MinCompletionTimes(jobs []Job) []Job {
sort.Sort([]JobSlice(jobs)) // cannot convert jobs (type []Job) to type []JobSlice
return jobs
}
Er...
答案1
得分: 4
首先,我没有看到任何地方定义了Jobs
,尽管你在使用jobs []Jobs
。我认为你的意思是Job
,因为错误提示中说cannot convert jobs (type []Job)
,所以我假设当你写[]Jobs
时,你实际上是想写[]Job
。
如果是这样的话,那么你正在尝试将一个Job
的切片转换为一个JobSlice
的切片,而JobSlice
的底层类型是[]Job
。
[]JobSlice(jobs) // 将一个`Job`的切片转换为一个`Job`的切片的切片?
换句话说,你想要将[]Job
转换为JobSlice
。
JobSlice(jobs)
所以,删除一些代码后,你可以看到这个转换是有效的。
type Job struct {
weight int
length int
}
type JobSlice []Job
func main() {
x := []Job{{},{}}
y := JobSlice(x)
z := []Job(y)
fmt.Println(x, y, z)
}
英文:
<s>First of all, I don't see Jobs
defined anywhere even though you use it like jobs []Jobs
.
I think you mean Job
since the error states cannot convert jobs (type []Job)
, so I'll assume that when you're doing []Jobs
, you really mean []Job
.
If so, then with this, y</s> You're trying to convert a slice of Job
to a slice of JobSlice
, the which has an underlying type of []Job
.
[]JobSlice(jobs) // converting a slice of Job to a slice of slices of Job?
In other words, you're trying to convert []Job
to effectively [][]Job
. Instead I think you just wanted to convert your []Job
to JobSlice
JobSlice(jobs)
So taking out a bunch of the code, you can see that this conversion will work.
type Job struct {
weight int
length int
}
type JobSlice []Job
func main() {
x := []Job{{},{}}
y := JobSlice(x)
z := []Job(y)
fmt.Println(x, y, z)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论