samber / lo

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)
https://pkg.go.dev/github.com/samber/lo
MIT License
17.94k stars 825 forks source link

Chaining operation support ? #131

Open JOJO0527 opened 2 years ago

JOJO0527 commented 2 years ago

It seems that lo can't do any chain opertaions like

type Poo struct {
    Id   int
    Name string
}

poos := []Poo{
        {1, "A"},
        {2, "B"},
        {3, "C"},
    }

//  it dosen't work 
lo.Map[Poo, int](poos, func(x Poo, _ int) int {
        return x.Id
    }).Filter[int](lo.Map[Poo, int](poos, func(x int, _ int) bool {
        return x%2 == 0
    })

// it works 
even := lo.Filter[int](lo.Map[Poo, int](poos, func(x Poo, _ int) int {
        return x.Id
    }), func(x int, _ int) bool {
        return x%2 == 0
    })

It will be supported in the future? May be we can introduce a middle data stream like the stream operation in Java

wirekang commented 2 years ago

Currently, Go does not supports generic for methods.

awltr commented 2 years ago

This can never work because the result of the first call is just a Go slice and will not provide the functions of lo. It could, if lo provides another package like loc (for chaining), which returns an instance of lo, which then provides all the functions underneath. But then a final consumer call is needed as well. That’s missing in your „it doesn’t work“ (because it’s probably based on JS). In Java this is called „collect“. But collect would be also misleading, because this chaining here in Go would never be lazy.

Anyway, I would love to see this feature as well. I think with a generic interface, which defines all functions of lo on a pointer receiver and a final consumer function this could be possible.

Wulfheart commented 2 years ago

This would be really cool.

tomasz-hofy commented 11 months ago

https://github.com/koss-null/FuncFrog