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

new generic functions type #116

Closed
mortezakhademan opened this issue Jun 24, 2023 · 1 comment
Closed

new generic functions type #116

mortezakhademan opened this issue Jun 24, 2023 · 1 comment

Comments

@mortezakhademan
Copy link

mortezakhademan commented Jun 24, 2023

Hello
Why you don't implement generic functions like this:

package main

import "fmt"

func main() {
	i := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	i2 := from(i).where(func(i int) bool {
		return i%2 == 0
	})
	fmt.Println(i2)
}

func from[T any](a []T) *q[T] {
	q := &q[T]{}
	q.list = a
	return q
}

type q[T any] struct {
	list []T
}

func (a *q[T]) where(f1 func(T) bool) []T {
	var result []T
        // this is just for sample, you can return type of q struct 
	for _, l := range a.list {
		if f1(l) {
			result = append(result, l)
		}
	}
	return result
}
@ahmetb
Copy link
Owner

ahmetb commented Jul 17, 2023

See #96.
Sadly the current Go generics does not allow method chaining like From().Select().Where().... The only feasible approach is Where(From()).Select() type of method composition which isn't the point of this library. There are alternative libraries that let you go that path.

@ahmetb ahmetb closed this as completed Jul 17, 2023
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

No branches or pull requests

2 participants