Skip to content

Commit

Permalink
deprecated functions that could easily be written using generics
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze committed Feb 20, 2025
1 parent 2123ef3 commit e1bda7f
Show file tree
Hide file tree
Showing 40 changed files with 322 additions and 41 deletions.
48 changes: 24 additions & 24 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,6 @@ Recursive Functions:
- [GoString](http://godoc.org/github.com/awalterschulze/goderive/plugin/gostring) `deriveGoString(T) string`
- [Hash](http://godoc.org/github.com/awalterschulze/goderive/plugin/hash) `deriveHash(T) uint64`

Set Functions:

- [Keys](http://godoc.org/github.com/awalterschulze/goderive/plugin/keys) `deriveKeys(map[K]V) []K`
- [Sort](http://godoc.org/github.com/awalterschulze/goderive/plugin/sort) `deriveSort([]T) []T`
- [Unique](http://godoc.org/github.com/awalterschulze/goderive/plugin/unique) `deriveUnique([]T) []T`
- [Set](http://godoc.org/github.com/awalterschulze/goderive/plugin/set) `deriveSet([]T) map[T]struct{}`
- [Min](http://godoc.org/github.com/awalterschulze/goderive/plugin/min)
- `deriveMin(list []T, default T) (min T)`
- `deriveMin(T, T) T`
- [Max](http://godoc.org/github.com/awalterschulze/goderive/plugin/max)
- `deriveMax(list []T, default T) (max T)`
- `deriveMax(T, T) T`
- [Contains](http://godoc.org/github.com/awalterschulze/goderive/plugin/contains) `deriveContains([]T, T) bool`
- [Intersect](http://godoc.org/github.com/awalterschulze/goderive/plugin/intersect)
- `deriveIntersect(a, b []T) []T`
- `deriveIntersect(a, b map[T]struct{}) map[T]struct{}`
- [Union](http://godoc.org/github.com/awalterschulze/goderive/plugin/union)
- `deriveUnion(a, b []T) []T`
- `deriveUnion(a, b map[T]struct{}) map[T]struct{}`

Functional Functions:

- [Fmap](http://godoc.org/github.com/awalterschulze/goderive/plugin/fmap)
Expand All @@ -125,10 +105,6 @@ Functional Functions:
- `deriveJoin([]string) string`
- `deriveJoin(func() (T, error), error) func() (T, error)`
- `deriveJoin(func() (T, ..., error), error) func() (T, ..., error)`
- [Filter](http://godoc.org/github.com/awalterschulze/goderive/plugin/filter) `deriveFilter(pred func(T) bool, []T) []T`
- [All](http://godoc.org/github.com/awalterschulze/goderive/plugin/all) `deriveAll(pred func(T) bool, []T) bool`
- [Any](http://godoc.org/github.com/awalterschulze/goderive/plugin/any) `deriveAny(pred func(T) bool, []T) bool`
- [TakeWhile](http://godoc.org/github.com/awalterschulze/goderive/plugin/takewhile) `deriveTakeWhile(pred func(T) bool, []T) []T`
- [Flip](http://godoc.org/github.com/awalterschulze/goderive/plugin/flip) `deriveFlip(f func(A, B, ...) T) func(B, A, ...) T`
- [Curry](http://godoc.org/github.com/awalterschulze/goderive/plugin/curry) `deriveCurry(f func(A, B, ...) T) func(A) func(B, ...) T`
- [Uncurry](http://godoc.org/github.com/awalterschulze/goderive/plugin/uncurry) `deriveUncurry(f func(A) func(B, ...) T) func(A, B, ...) T`
Expand Down Expand Up @@ -163,6 +139,30 @@ Concurrency Functions:
- [Dup](http://godoc.org/github.com/awalterschulze/goderive/plugin/dup)
- `deriveDup(c <-chan T) (c1, c2 <-chan T)`

Deprecated in favour of generics:

- [Keys](http://godoc.org/github.com/awalterschulze/goderive/plugin/keys) `deriveKeys(map[K]V) []K`
- [Sort](http://godoc.org/github.com/awalterschulze/goderive/plugin/sort) `deriveSort([]T) []T`
- [Unique](http://godoc.org/github.com/awalterschulze/goderive/plugin/unique) `deriveUnique([]T) []T`
- [Set](http://godoc.org/github.com/awalterschulze/goderive/plugin/set) `deriveSet([]T) map[T]struct{}`
- [Min](http://godoc.org/github.com/awalterschulze/goderive/plugin/min)
- `deriveMin(list []T, default T) (min T)`
- `deriveMin(T, T) T`
- [Max](http://godoc.org/github.com/awalterschulze/goderive/plugin/max)
- `deriveMax(list []T, default T) (max T)`
- `deriveMax(T, T) T`
- [Contains](http://godoc.org/github.com/awalterschulze/goderive/plugin/contains) `deriveContains([]T, T) bool`
- [Intersect](http://godoc.org/github.com/awalterschulze/goderive/plugin/intersect)
- `deriveIntersect(a, b []T) []T`
- `deriveIntersect(a, b map[T]struct{}) map[T]struct{}`
- [Union](http://godoc.org/github.com/awalterschulze/goderive/plugin/union)
- `deriveUnion(a, b []T) []T`
- `deriveUnion(a, b map[T]struct{}) map[T]struct{}`
- [Filter](http://godoc.org/github.com/awalterschulze/goderive/plugin/filter) `deriveFilter(pred func(T) bool, []T) []T`
- [All](http://godoc.org/github.com/awalterschulze/goderive/plugin/all) `deriveAll(pred func(T) bool, []T) bool`
- [Any](http://godoc.org/github.com/awalterschulze/goderive/plugin/any) `deriveAny(pred func(T) bool, []T) bool`
- [TakeWhile](http://godoc.org/github.com/awalterschulze/goderive/plugin/takewhile) `deriveTakeWhile(pred func(T) bool, []T) []T`

When goderive walks over your code it is looking for a function that:
- was not implemented (or was previously derived) and
- has a predefined prefix.
Expand Down
2 changes: 2 additions & 0 deletions example/plugin/contains/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ goderive will generate the following code:
package contains

// deriveContains returns whether the item is contained in the list.
//
// Deprecated: In favour of generics.
func deriveContains(list []boat, item boat) bool {
for _, v := range list {
if v == item {
Expand Down
2 changes: 2 additions & 0 deletions example/plugin/contains/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions example/plugin/intersect/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package intersect

// deriveIntersect returns the intersection of the two lists' values
// It assumes that the first list only contains unique items.
//
// Deprecated: In favour of generics.
func deriveIntersect(this, that []int) []int {
intersect := make([]int, 0, deriveMin(len(this), len(that)))
for i, v := range this {
Expand All @@ -34,6 +36,8 @@ func deriveIntersect(this, that []int) []int {
}

// deriveContains returns whether the item is contained in the list.
//
// Deprecated: In favour of generics.
func deriveContains(list []int, item int) bool {
for _, v := range list {
if v == item {
Expand All @@ -44,6 +48,8 @@ func deriveContains(list []int, item int) bool {
}

// deriveMin returns the minimum of the two input values.
//
// Deprecated: In favour of generics.
func deriveMin(a, b int) int {
if a < b {
return a
Expand Down
6 changes: 6 additions & 0 deletions example/plugin/intersect/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions example/plugin/keys/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ import (
)

// deriveSort sorts the slice inplace and also returns it.
//
// Deprecated: In favour of generics.
func deriveSort(list []string) []string {
sort.Strings(list)
return list
}

// deriveKeys returns the keys of the input map as a slice.
//
// Deprecated: In favour of generics.
func deriveKeys(m map[string]int) []string {
keys := make([]string, 0, len(m))
for key := range m {
Expand Down
4 changes: 4 additions & 0 deletions example/plugin/keys/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions example/plugin/max/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func deriveCompare(this, that boat) int {
}

// deriveMax returns the maximum of the two input values.
//
// Deprecated: In favour of generics.
func deriveMax(a, b int) int {
if a > b {
return a
Expand All @@ -49,6 +51,8 @@ func deriveMax(a, b int) int {
}

// deriveMaxs returns the maximum value from the input list and the default value, if the list is empty.
//
// Deprecated: In favour of generics.
func deriveMaxs(list []boat, def boat) boat {
if len(list) == 0 {
return def
Expand Down
4 changes: 4 additions & 0 deletions example/plugin/max/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions example/plugin/min/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ goderive will generate the following code:
package min

// deriveFilter returns a list of all items in the list that matches the predicate.
//
// Deprecated: In favour of generics.
func deriveFilter(predicate func(boat) bool, list []boat) []boat {
j := 0
for i, elem := range list {
Expand All @@ -46,6 +48,8 @@ func deriveFilter(predicate func(boat) bool, list []boat) []boat {
}

// deriveMin returns the minimum of the two input values.
//
// Deprecated: In favour of generics.
func deriveMin(a, b int) int {
if a < b {
return a
Expand All @@ -54,6 +58,8 @@ func deriveMin(a, b int) int {
}

// deriveMins returns the minimum value from the list, or the default value if the list is empty.
//
// Deprecated: In favour of generics.
func deriveMins(list []boat, def boat) boat {
if len(list) == 0 {
return def
Expand Down
6 changes: 6 additions & 0 deletions example/plugin/min/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/plugin/set/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ goderive will generate the following code:
package set

// deriveSet returns the input list as a map with the items of the list as the keys of the map.
//
// Deprecated: In favour of generics.
func deriveSet(list []int) map[int]struct{} {
set := make(map[int]struct{}, len(list))
for _, v := range list {
Expand Down
2 changes: 2 additions & 0 deletions example/plugin/set/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/plugin/sort/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func deriveCompare(this, that []*MyStruct) int {
}

// deriveSort sorts the slice inplace and also returns it.
//
// Deprecated: In favour of generics.
func deriveSort(list []*MyStruct) []*MyStruct {
sort.Slice(list, func(i, j int) bool { return deriveCompare_(list[i], list[j]) < 0 })
return list
Expand Down
2 changes: 2 additions & 0 deletions example/plugin/sort/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions example/plugin/union/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ package union

// deriveUnique returns a list containing only the unique items from the input list.
// It does this by reusing the input list.
//
// Deprecated: In favour of generics.
func deriveUnique(list []*Person) []*Person {
if len(list) == 0 {
return nil
Expand Down Expand Up @@ -60,6 +62,8 @@ func deriveUnique(list []*Person) []*Person {
}

// deriveFilter returns a list of all items in the list that matches the predicate.
//
// Deprecated: In favour of generics.
func deriveFilter(predicate func(*Person) bool, list []*Person) []*Person {
j := 0
for i, elem := range list {
Expand All @@ -75,6 +79,8 @@ func deriveFilter(predicate func(*Person) bool, list []*Person) []*Person {

// deriveUnion returns the union of the items of the two input lists.
// It does this by append items to the first list.
//
// Deprecated: In favour of generics.
func deriveUnion(this, that []*Person) []*Person {
for i, v := range that {
if !deriveContains(this, v) {
Expand Down Expand Up @@ -104,6 +110,8 @@ func deriveHash(object *Person) uint64 {
}

// deriveContains returns whether the item is contained in the list.
//
// Deprecated: In favour of generics.
func deriveContains(list []*Person, item *Person) bool {
for _, v := range list {
if deriveEqual(v, item) {
Expand Down
8 changes: 8 additions & 0 deletions example/plugin/union/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/plugin/unique/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package unique

// deriveUnique returns a list containing only the unique items from the input list.
// It does this by reusing the input list.
//
// Deprecated: In favour of generics.
func deriveUnique(list []*Visitor) []*Visitor {
if len(list) == 0 {
return nil
Expand Down
2 changes: 2 additions & 0 deletions example/plugin/unique/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/talk/01_min/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e1bda7f

Please sign in to comment.