Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice: 聚合函数 Max, Min 和 Sum #82

Merged
merged 1 commit into from
Sep 10, 2022
Merged

slice: 聚合函数 Max, Min 和 Sum #82

merged 1 commit into from
Sep 10, 2022

Conversation

flycash
Copy link
Contributor

@flycash flycash commented Sep 9, 2022

  • 在 Max 和 Min 里面要求提供至少一个元素,是因为如果任何元素都没有的话,我们不知道怎么处理。返回零值并不合适;

另外一个值得讨论的是 types 包。这个我是准备用来放一些公共接口,和泛型约束。目前这里定义了两个泛型约束,

  • RealNumber: 实数
  • Number:包含了复数的数字

我预计将来在实现树结构的时候,这里会放 Comparable 接口;在实现 HashMap 的时候,会放 Hashable 接口... 这一类的接口,我预期中并不会在专门一个地方使用。例如 Comparable 接口,可以用在树形结构里面,也可以用在 SkipList 里面.

因此需要一个公共的地方来定义类似的接口。

我选择了 types。

@codecov
Copy link

codecov bot commented Sep 9, 2022

Codecov Report

Merging #82 (814b5ae) into dev (d8f9921) will increase coverage by 0.09%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##              dev      #82      +/-   ##
==========================================
+ Coverage   95.90%   95.99%   +0.09%     
==========================================
  Files          21       22       +1     
  Lines         879      899      +20     
==========================================
+ Hits          843      863      +20     
  Misses         28       28              
  Partials        8        8              
Impacted Files Coverage Δ
slice/aggregate.go 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@flycash
Copy link
Contributor Author

flycash commented Sep 9, 2022

@longyue0521 你有什么建议吗?关于 types 的

func Max[T types.RealNumber](ts []T) T {
res := ts[0]
for i := 1; i < len(ts); i++ {
if ts[i] > res {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

关于float类型我有点担心,使用">"和"<"比较不知道是否要处理下面的情况

    var a float64 = 1.0000000000000001
    var b float64 = 1.000000000000000001
    var c float64 = 1.000000000000001
    var d float64 = 1.0000000000000000001

    fmt.Println(a == b) //true
    fmt.Println(a > b)  //false
    fmt.Println(c == d) //false
    fmt.Println(c > d)  //true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得我们处理不了这种精度问题,用户即便自己手写代码,如果他追求这种精确数字,那么实际上不能使用 float,要使用 decimal。

所以我觉得,一种方案是把 float 的丢掉,只处理 uint 和 int 族;另外一种就是我们不管,因为当用户使用 float 的时候就应该意识到这种不精确的风险。

下面的求和也是类似的。如果他们寻求精确表达,那么就不应该使用 float,如果不寻求的话,那么 Go 对 float 的支持就够了

Copy link
Collaborator

@longyue0521 longyue0521 Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flycash 明确意图,把float32和float64踢出去吧,把RealNumber改为Integer? 我觉得Number接口里可以放float32和float64.

testMinTypes[int16](t)
testMinTypes[int32](t)
testMinTypes[int64](t)
testMinTypes[float32](t)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

testMaxTypes[int16](t)
testMaxTypes[int32](t)
testMaxTypes[int64](t)
testMaxTypes[float32](t)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float32和float64可能需要考虑带小数点的情况

testSumTypes[int16](t)
testSumTypes[int32](t)
testSumTypes[int64](t)
testSumTypes[float32](t)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@longyue0521
Copy link
Collaborator

将挎包的公共接口,比如Comparable 接口, 直接放在ekit根目录下怎么样,文件名types.go和interface.go都行

@flycash flycash force-pushed the dev branch 2 times, most recently from 6c94b5b to bdce2f3 Compare September 9, 2022 12:36
@flycash
Copy link
Contributor Author

flycash commented Sep 9, 2022

将挎包的公共接口,比如Comparable 接口, 直接放在ekit根目录下怎么样,文件名types.go和interface.go都行

挪过去了

@longyue0521
Copy link
Collaborator

longyue0521 commented Sep 9, 2022

@flycash constrain是动词,文件名改为constraints.go吧

@flycash
Copy link
Contributor Author

flycash commented Sep 10, 2022

float 的问题,我决定让用户为自己的行为负责。这就好比,在 Go 语法层面上支持 float32 的加减,那么就意味着 Go SDK 的人觉得,我们使用 float 应该要自己管理精度问题

@flycash flycash merged commit f0ddf7f into ecodeclub:dev Sep 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants