有没有推荐的方法来定义收集切片的常见行为? [英] Is there a recommended way to define common behaviour for collection of slices?

查看:114
本文介绍了有没有推荐的方法来定义收集切片的常见行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从社区获得建议,以便制定最佳做法。
请耐心等待,如下例所示:



假设您使用半开间隔,即你知道什么时候开始。



例如


  • 可以将 HalfOpenInterval 限制为一天。例如:你说从下午1点起(直到一天结束)。我们称之为 ClockInterval

  • 可以将 HalfOpenInterval 限制为Universe的存在。例如:你说从1810年7月9日我们宣布独立性(直到宇宙末期......假设)。我们称之为期间



对于两种实体类型:您都使用它们的集合,您的代码中包含时钟句点切片

<因此,现在出现这个问题:您必须为给定时间 func FindEnclosingHalfOpenInterval )找到的封闭时间间隔时钟和周期,所以你开始编写代码...



嗯,我进入这个问题...我应该如何组织代码才能写只有一次共同的功能。 ( func FindEnclosingHalfOpenInterval )。

所以我进入这段代码: https://play.golang.org/p/Cy7fFaFzYJR



但是我不知道是否有更好的方法来定义收集切片的常见行为。



请读者意识到我需要做一个逐个元素转换为每个类型的切片(我有一个切片类型的每种类型的具体HalfOpenInterval我定义)。所以我想知道是否有任何方法可以让我引入新的 HalfOpenInterval 类型,而不必进行一些调整,并且自动获取使用 func FindEnclosingHalfOpenInterval ?.也许我的基于丰富oo-java的思想并不是在简单直接前进的世界中面对问题的正确方法。我所有的听到,任何建议。

解决方案

这里的关键问题是你需要将切片从一种类型转换为另一种类型。

转换片



正确的方法是创建一个新的片并循环它转换每一个项目。如果您事先创建数组,则可以快速执行此操作:

  func ToIntervalsFromClockIntervals(clockIntervals [] ClockInterval)HalfOpenIntervals {
间隔:= make(HalfOpenIntervals,0,len(clockIntervals))
for _,clockInterval:= range clockIntervals {
intervals = append(intervals,clockInterval)
}
返回间隔
}



成分



除此之外,您还可以通过另一种方式解决您只想编写 GetEnclosingInterval 函数一次的问题。我并不是说它更好:它还有其他的优点和缺点。什么更适合取决于你如何使用除了你发布在这里的切片。



这里我的重构建议(和固定): https://play.golang.org/p/Ko43hJUMpyT (TehSṕhinX您忘记制作可变方法 baseIntervals.add with 指针接收者而不是值接收者 (或者非指针接收者的名字))


$ b $ < HalfOpenIntervals
不再存在。相反,对于 CLockIntervals PeriodIntervals 有两种不同的类型,它们都有排序和 GetEnclosingInterval 通过公共基础结构实现的函数。



为了方便起见,我添加了一个 Add 函数,每一个都有一个 New ... 函数。这就是缺点:因为 CLockIntervals (和 PeriodIntervals )不再是切片,而是一个结构体将需要便利的功能与外部的内层工作。



- 编辑 -



概括



来自oo背景的我自己,我知道避免重复代码的代价是不惜一切代价的。

现在开始写代码已经超过2年了。我了解到,这并不总是最好的方法。在不同类型的代码中复制代码是我最近常见的事情。但不知道是否所有人都会同意这一说法。


I am trying to get suggestion from the community in order to make the best practices. Please bear with me, with the following example:

Suppose that you work with half open intervals, that is, something that you know when it starts.

For example

  • There can be HalfOpenInterval restricted to a day. Example: you say "from 1:00 pm afterwards" (until the end of the day). Let's call it ClockInterval
  • There can be HalfOpenInterval restricted to the existence of universe. Example: you say "from 9 of july of 1810 we declare the indepedency" (until the end of the cosmos.. hypothetically). Let's call it Period

For both entities types: you work with a collection of them, so you usually have slices of clocks and periods in your code.

So now comes the problem: you must find the enclosing interval for a given time (func FindEnclosingHalfOpenInterval) for both clocks and periods, so you start writing the code...

And well, i get into this matter... how i should organize the code in order to write only once the common func. (func FindEnclosingHalfOpenInterval).

So i get into this code: https://play.golang.org/p/Cy7fFaFzYJR

But i keep wondering if there is a better way to define common behaviour for collection of slices.

Please reader you shall realize that i need to do an "element by element" conversion for each type of slice (and i have a type of slice for each type of concrete HalfOpenInterval i define). So i wonder if there is there any way that allows me to introduce new types of HalfOpenInterval without having to do some adjustement and "automatically" gets the abilitiy to use the func FindEnclosingHalfOpenInterval?. Perhaps my rich-oo-java-based mind is not the correct way to face the problems in the simplistic-straight-ahead-go-world. I'm all hears, to any suggestion.

解决方案

The key Problem here is you need to convert a slice from one type to another type.

Converting Slices

The correct way to do this is to create a new slice and loop over it converting every single item. You can do this fast(er) if you create the array beforehand:

func ToIntervalsFromClockIntervals(clockIntervals []ClockInterval) HalfOpenIntervals {
    intervals := make(HalfOpenIntervals, 0, len(clockIntervals))
    for _, clockInterval := range clockIntervals {
        intervals = append(intervals, clockInterval)
    }
    return intervals
}

Composition

Apart from that composition is another way you could solve the problem that you want to write the GetEnclosingInterval function only once. I'm not saying it is better: it has other advantages and disadvantages. What fits better depends on how else you use the slices apart from what you posted here.

Here my refactored suggestion (and fixed): https://play.golang.org/p/Ko43hJUMpyT (TehSṕhinX you forgot to make the mutable method baseIntervals.add with pointer recievers instead ofvalue recievers(or whatever is the name for non-pointer recievers))

The HalfOpenIntervals does not exist any more. Instead you have two different types for CLockIntervals and PeriodIntervals and both have the sorting and GetEnclosingInterval function implemented via a common base struct.

For convenience I added a Add function and a New... function for each of them. This is where the disadvantages come in: Since CLockIntervals (and PeriodIntervals) is not a slice any more but a struct you will need convenience functions to work with the inner slice from the outside.

-- edit --

Over generalising

Coming from a oo-oriented background myself I know the drive to avoid duplicated code at all costs.

Writing go code for more than 2 years now on a full time basis I learned that this is not always the best approach in go. Duplicating code in go for different types is a common thing I do these days. Not sure if all would agree with this statement, though.

这篇关于有没有推荐的方法来定义收集切片的常见行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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