lo icon indicating copy to clipboard operation
lo copied to clipboard

Chaining operation support ?

Open JOJO0527 opened this issue 3 years ago • 4 comments

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

JOJO0527 avatar May 09 '22 10:05 JOJO0527

Currently, Go does not supports generic for methods.

wirekang avatar May 09 '22 10:05 wirekang

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.

awltr avatar Jun 26 '22 00:06 awltr

This would be really cool.

Wulfheart avatar Oct 25 '22 22:10 Wulfheart

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

tomasz-hofy avatar Dec 14 '23 14:12 tomasz-hofy