Comments (14)
In this case, I think the documentation is good enough but since apparently people can easily do this mistake, we can just say it does not clear the existing slice.
from go-linq.
cc: @kalaninja any ideas?
from go-linq.
Yes, it is not a bug. The idea is that the go-linq user prepares the storage and decides whether to clean it or not. I thought that it is quite obvious from the doc, since here is nothing about cleaning the result slice:
ToSlice iterates over a collection and populates result slice
But If it is not, I may document this behaviour. what do you think @ahmetalpbalkan ?
from go-linq.
This is like pre-populating a struct's fields and then saying json.Unmarshal(b, &v)
is not clearing the fields which were set previously and missing from the unmarshaled json object. I think our design is just fine.
from go-linq.
What about to add a new method AppendToSlice (with same behavior of actual ToSlice function) and change the ToSlice behavior to set the result pointer to a new slice pointer as proposed here #46?
from go-linq.
@cleitonmarx I can't think of a case of why someone would pass us a slice to be replaced with a new slice instance. What would be a good use case for that?
from go-linq.
@ahmetalpbalkan Hi I have a question, if I have a list of whatever, I want to order them "in place" like this:
l := []string{"foo", "bar", "baz"}
l = linq.From(l).Results()
then go will complains:
cannot use linq.From(l).Results() (type []interface {}) as type []string in assignment
So I added the .([]string)
like this:
l := []string{"foo", "bar", "baz"}
l = linq.From(l).Results().([]string)
but go still complains:
invalid type assertion: linq.From(l).Results().([]string) (non-interface type []interface {} on left)
So I need the ToSlice
function to do this, or is there some way to do this better which I am missing?
from go-linq.
@ahmetalpbalkan As a ".NET LINQ-like" library, I think that the behavior of ToSlice should be the same as the original LINQ ToList. In ToList implementation the return is always a new instance.
A good use case is ordering a slice using linq (example found here):
using C#:
options = options.OrderBy(o => o.Position).ToList();
using Go:
From(options).OrderBy(func(o interface{}) interface{}{
return o.(Option).Position
}).ToSlice(&options)
Currently, the code in Go will not have the same result of the C# code. My Pull Request tries to fix that.
from go-linq.
@aisk Yes, you have to use ToSlice
function.
result := []string{}
l := []string{"foo", "bar", "baz"}
linq.From(l).OrderBy(func(a interface{}) interface{}{
return true;
}).ToSlice(&result)
from go-linq.
@kalaninja Yes I am writing codes like what you pasted, and then this pull request is created ...
Now I am created a new slice to receive the result. But as you know, naming a variable is hard in software engineering... So I think is more convenient if we had a new method (or change the ToSlice
's behavior).
from go-linq.
@aisk I understand your concerns, here is what I offer.
from go-linq.
BTW the work to redefine ToSlice()
is actively ongoing at #46. @aisk we now empty the passed slice and shrink/grow it based on the result size. It'll be available in v3.0 release.
from go-linq.
@ahmetalpbalkan great!
from go-linq.
Fixed in v3-dev branch.
from go-linq.
Related Issues (20)
- Append方法优化
- "GroupBy has better performance than GroupByT" is written on the document,why?
- ToChannelT release? HOT 2
- why union.go file doesn't have func UnionBy like the func IntersectBy in intersect.go
- Why `ToSlice` need a custom grow strategy instead of the standard library? HOT 1
- runtime error: hash of unhashable type map[string]interface {} HOT 9
- Unit test ExampleOrderedQuery_ThenByDescending fails on golang-1.19.2 HOT 6
- sort result is unexpected HOT 1
- How to implement the full join function of SQL
- new generic functions type HOT 1
- 转换错误 HOT 1
- I expect the data queried using gorm to be converted using go-linq HOT 1
- bug in WhereIndexed HOT 7
- Article comparing performance of go-linq vs others HOT 4
- gopkg.in/ahmetb/go-linq.v3: gopkg.in/ahmetb/[email protected]: parsing go.mod: module declares its path as: github.com/ahmetb/go-linq/v3 but was required as: gopkg.in/ahmetb/go-linq.v3 HOT 5
- how about add parameter timeout for FromChannelT?
- Try out the new Go generics proposal with go-linq HOT 14
- Is it safe to copy in multiple concurrency HOT 2
- How to implement complex SQL functions HOT 5
- PLINQ support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-linq.