优雅的方法将一个类型的切片转换为等效类型的切片?

huangapple go评论71阅读模式
英文:

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 &lt; 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)
}

huangapple
  • 本文由 发表于 2012年12月7日 05:21:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/13752819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定