昨天,我为新的官方Golang 1.18版本构建了一些通用函数。你认为它是什么,什么可以增加和改进?
package main
import (
"fmt"
"log"
"reflect"
)
func ToString(this any) string {
return fmt.Sprintf("%v", this)
}
type List[V any] []V
func (this List[V]) Size() int {
return len(this)
}
func (this *List[V]) Add(next V) *List[V] {
*this = append(*this, next)
return this
}
func (this List[V]) Get(v int) V {
return this[v]
}
func (this *List[V]) Map(f func(V) V) *List[V] {
var res List[V]
for _, v := range *this {
res.Add(f(v))
}
return &res
}
func (this *List[V]) Filter(f func(V) bool) *List[V] {
var res List[V]
for _, v := range *this {
if f(v) {
res.Add(v)
}
}
return &res
}
type Map[K comparable, V any] map[K]V
func (this Map[K, V]) Size() int {
return len(this)
}
func (this *Map[K, V]) Add(key K, value V) *Map[K, V] {
(*this)[key] = value
return this
}
func (this *Map[K, V]) Get(key K) V {
return (*this)[key]
}
func (this Map[K, V]) Keys() *List[V] {
res := make(List[V], 0, len(this))
for _, v := range this {
res = append(res, v)
}
return &res
}
func (this Map[K, V]) Values() *List[V] {
res := make(List[V], 0, len(this))
for _, v := range this {
res = append(res, v)
}
return &res
}
func (this *Map[K, V]) ContainsKey(key K) bool {
if _, ok := (*this)[key]; ok {
return true
}
return false
}
func (this *Map[K, V]) ContainsValue(value V) bool {
for _, v := range *this {
if reflect.TypeOf(v) == reflect.TypeOf(value) && ToString(v) == ToString(value) {
return true
}
}
return false
}
func (this *Map[K, V]) Map(f func(K, V) (K, V)) *Map[K, V] {
res := make(Map[K, V], 0)
for k, v := range *this {
res.Add(f(k, v))
}
return &res
}
func (this *Map[K, V]) Filter(f func(V) bool) *Map[K, V] {
res := make(Map[K, V], 0)
for k, v := range *this {
if f(v) {
res.Add(k, v)
}
}
return &res
}
func main() {
listTemp := make(List[int], 0)
listTemp.Add(1)
log.Println(listTemp)
log.Println("string: " + ToString(listTemp.Get(0)))
mapTemp := make(Map[string, string], 0)
mapTemp.Add("al", "aluminum").Map(func(k string, v string) (string, string) {
log.Println("first call: " + v)
if v == "aluminum" {
return k, "aluminium"
}
return k, v
}).Values().Add("silver").Map(func(v string) string {
log.Println("second call: " + v)
return v
})
mapTemp.Add("ag", "silver")
mapTemp.Add("o", "oxygen")
log.Println(mapTemp)
log.Println("string: " + ToString(mapTemp))
log.Println(mapTemp.ContainsKey("al"))
log.Println(mapTemp.ContainsKey("1"))
log.Println(mapTemp.Values())
log.Println(mapTemp.Size())
log.Println(mapTemp.Values().Size())
log.Println(mapTemp.Get("al"))
log.Println(mapTemp.Values().Get(2))
mapTemp2 := make(Map[string, int], 0)
log.Println(mapTemp2.Add("al", 1).ContainsValue(1))
}你也可以在高尔夫操场上试试:https://go.dev/play/p/7OFh0KCmHSc
我想要构建函数,您可以像在Java或Dart中那样在后面添加这些函数,因此我经常将给定的值返回给函数,或者,例如,在Map()上,我返回更改后的值。这是直觉吗?
我还没有找到任何有效的解决方案,所以请随意使用我的解决方案并加以改进。
编辑:一段时间后,我还会添加以下函数:
func (l *List[V]) RemoveFirst() *List[V] {
if l.Size() > 0 {
res := (*l)[1:]
return &res
}
return l
}
func (l *List[V]) RemoveLast() *List[V] {
if l.Size() > 0 {
res := (*l)[:len(*l)-1]
return &res
}
return l
}
(...)
func (m *Map[K, V]) RemoveKey(k K) *Map[K, V] {
res := *m
delete(res, k)
return &res
}而不是这样,我现在改为一个单一的字符作为接收者(l代表列表,m代表地图)。
你认为也改变最初的地图和列表是有意义的吗?还是仅仅返回更改后的值更直观?那么,如果您调用list.RemoveKey(key),更改初始列表并返回修改后的值,还是只返回更改后的值?
发布于 2022-04-28 18:23:19
https://codereview.stackexchange.com/questions/275391
复制相似问题