将一种类型的切片转换为同等类型切片的优雅方法? [英] Elegant way to convert a slice of one type to a slice of an equivalent type?

查看:98
本文介绍了将一种类型的切片转换为同等类型切片的优雅方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个激励性的例子:

实现各种调度策略,对作业列表进行排序。

类型Job结构{
权重int
长度int
}

//给定一部分作业,重新排序他们。
类型策略func([] Job)[]作业

func作业计划(作业[]作业,策略策略] []作业{
作业(作业)
}

一个非常简单的策略是首先执行最短的工作(不考虑其重量/优先级)。

  func MinCompletionTimes(job [] Job)[] Job {
//嗯...
}

好的,这个策略只不过是一个job.length的排序,所以让我们使用排序包。定义一个自定义类型,然后实现sort.Interface ...

pre $ 输入JobSlice []作业//可能应该调用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,现在回到我们的简单策略。 。
$ b

  func MinCompletionTimes(jobs [] Job)[] Job {
sort.Sort([] JobSlice(jobs ))//不能转换作业(键入[] Job)键入[] JobSlice
返回作业
}



呃...

解决方案

首先,我没有看到 Jobs ,即使您像使用它一样定义 jobs [] Jobs

我认为你的意思是 Job ,因为错误状态不能转换作业(类型[] Job),所以我假定当你做 [] Jobs 时,你的意思是 [] Job






如果是这样,那么用这个,y 作业切片转换为 JobSlice 的片段,其中的基础类型为 []工作

  [] JobSlice(jobs)//将Job的一部分转换为Job的一部分片段? 

换句话说,您试图将 [] Job 有效地 [] []作业。相反,我想你只是想将 [] Job 转换为 JobSlice

  JobSlice(jobs)






因此,拿出一堆代码,你可以看到这种转换将起作用。

 类型Job结构{
权重int
长度int
}

类型JobSlice []作业

func main(){
x:= [] Job {{},{}}

y:= JobSlice(x)
z:= [] Job(y)

fmt.Println (x,y,z)
}


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...

解决方案

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 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)
}

这篇关于将一种类型的切片转换为同等类型切片的优雅方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆