Skip to content

Commit

Permalink
Merge pull request #6 from hyphennn/hyphen/dev
Browse files Browse the repository at this point in the history
feat: support conv
  • Loading branch information
hyphennn authored Dec 20, 2023
2 parents 87bd2e7 + 0943dc9 commit 77da219
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ Inspiration source: ByteDance code.byted.org/lang/gg

This package was not yet open source at the time of the author's resignation, and also cannot find a similar repository, so some ideas from lang/gg were used to create this package. If lang/gg plans to open source it in the future, please feel free to contact me so that ownership of this repository can be transferred

致力于提供一个不引入任何额外依赖以及只带来极小额外性能成本的 go lamda 表达式库
致力于提供一个不引入任何额外依赖以及只带来极小额外性能成本的 go lamda 表达式库,涵盖面向 go 的一些 lamda 常用表达式

灵感来源:字节跳动 code.byted.org/lang/gg

此包在作者离职时仍未开源,也未找到一个类似的仓库,因此基于 lang/gg 的一些思想做了此包,若后续 lang/gg 计划开源,欢迎联系我,可以转移此仓库的所有权

注:所有代码均为手写,个人理解不存在侵权问题,如果 lang/gg 愿意开源自然更好
32 changes: 32 additions & 0 deletions gconv/conv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Package gconv
// Author: hyphen
// Copyright 2023 hyphen. All rights reserved.
// Create-time: 2023/12/20
package gconv

import (
"github.com/hyphennn/glamda/internal"
"github.com/hyphennn/glamda/internal/constraints"
)

func ToPtr[T constraints.UnPtrAble](t T) *T {
return &t
}

func FromPtr[T constraints.UnPtrAble](t *T) T {
if t == nil {
return internal.Zero[T]()
}
return *t
}

func StringPtr(s string) *string {
return &s
}

func Ptr2String(s *string) string {
if s == nil {
return ""
}
return *s
}
22 changes: 22 additions & 0 deletions gconv/conv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Package gconv
// Author: hyphen
// Copyright 2023 hyphen. All rights reserved.
// Create-time: 2023/12/20
package gconv_test

import (
"testing"

"github.com/hyphennn/glamda/gconv"
"github.com/hyphennn/glamda/internal/assert"
)

func TestToPtr(t *testing.T) {
s := gconv.ToPtr("123")
assert.Equal(t, "123", *s)
}

func TestFromPtr(t *testing.T) {
s := "123"
assert.Equal(t, "123", gconv.FromPtr(&s))
}
13 changes: 13 additions & 0 deletions gstream/gstream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package gstream
// Author: hyphen
// Copyright 2023 hyphen. All rights reserved.
// Create-time: 2023/12/11
package gstream

import (
"testing"
)

func Test0(t *testing.T) {

}
33 changes: 33 additions & 0 deletions gstream/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Package gstream
// Author: hyphen
// Copyright 2023 hyphen. All rights reserved.
// Create-time: 2023/12/11
package gstream

type MapStream[K comparable, V any] struct {
m map[K]V
s *SliceStream[K]
}

func AsMapStream[K comparable, V any, M ~map[K]V](m M) *MapStream[K, V] {
return &MapStream[K, V]{m, nil}
}

func ToSliceStream[K comparable, T, V any](m *MapStream[K, V], fc func(K, V) T) *SliceStream[T] {
s := make([]T, 0, len(m.m))
m.ForEach(func(k K, v V) {
s = append(s, fc(k, v))
})
return AsSliceStream(s)
}

func (m *MapStream[K, V]) ForEach(fc func(K, V)) *MapStream[K, V] {
for k, v := range m.m {
fc(k, v)
}
return m
}

func (m *MapStream[K, V]) Collect() map[K]V {
return m.m
}
36 changes: 36 additions & 0 deletions gstream/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Package gstream
// Author: hyphen
// Copyright 2023 hyphen. All rights reserved.
// Create-time: 2023/12/11
package gstream

type SliceStream[T any] struct {
s []T
}

func AsSliceStream[T any](s []T) *SliceStream[T] {
return &SliceStream[T]{s}
}

func ToMapStream[K comparable, T, V any](s *SliceStream[T], fc func(T) (K, V)) *MapStream[K, V] {
m := make(map[K]V, len(s.s))
s.ForEach(func(t T) {
k, v := fc(t)
m[k] = v
})
return AsMapStream(m)
}

func (s *SliceStream[T]) ForEach(fc func(T)) {
for _, v := range s.s {
fc(v)
}
}

func (s *SliceStream[T]) Count() int {
return len(s.s)
}

func (s *SliceStream[T]) Collect() []T {
return s.s
}
4 changes: 4 additions & 0 deletions internal/constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ type Number interface {
type Addable interface {
Number | Complex | ~string
}

type UnPtrAble interface {
Number | Complex | ~string
}
15 changes: 15 additions & 0 deletions lmap/lmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ func ForEach[K comparable, V any](m map[K]V, fc func(K, V)) {
fc(k, v)
}
}

func Reverse[K, V comparable](m map[K]V) map[V]K {
ret := make(map[V]K, len(m))
for k, v := range m {
ret[v] = k
}
return ret
}

func SafeStore[K comparable, V any, M ~map[K]V](m M, k K, v V) {
if m == nil {
m = make(map[K]V)
}
m[k] = v
}

0 comments on commit 77da219

Please sign in to comment.