Coder Social home page Coder Social logo

Comments (14)

ahmetb avatar ahmetb commented on May 27, 2024 1

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.

ahmetb avatar ahmetb commented on May 27, 2024

cc: @kalaninja any ideas?

from go-linq.

kalaninja avatar kalaninja commented on May 27, 2024

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.

ahmetb avatar ahmetb commented on May 27, 2024

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.

cleitonmarx avatar cleitonmarx commented on May 27, 2024

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.

ahmetb avatar ahmetb commented on May 27, 2024

@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.

aisk avatar aisk commented on May 27, 2024

@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.

cleitonmarx avatar cleitonmarx commented on May 27, 2024

@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.

kalaninja avatar kalaninja commented on May 27, 2024

@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.

aisk avatar aisk commented on May 27, 2024

@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.

kalaninja avatar kalaninja commented on May 27, 2024

@aisk I understand your concerns, here is what I offer.

from go-linq.

ahmetb avatar ahmetb commented on May 27, 2024

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.

aisk avatar aisk commented on May 27, 2024

@ahmetalpbalkan great!

from go-linq.

ahmetb avatar ahmetb commented on May 27, 2024

Fixed in v3-dev branch.

from go-linq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.