-
Notifications
You must be signed in to change notification settings - Fork 415
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
Bind to source items by indicies #550
Comments
A prototype of the implementation is shared in PR #552 because it felt too large to share in a comment here. |
For the ordered case, or when
Is this the same? If so, then this leaves only the case where |
Was the comprehensive example (binding to CSV headers) I posted in the opening comment not sufficient?
Yes, except if you look at the implementation in #552, it's rather non-trivial. Also, it doesn't require hash look-ups in the ordered case. |
Well, I could use a // same code as in opening until the header and data are split
var headerPositions = (from key in new[] { "id", "email", "last_name", "first_name", "foo" }
let position = header.IndexOf(key)
select new KeyValuePair<string, int>(key, position)
).ToDictionary();
string GetValue(string[] row, int pos) {
if (pos < 0) {
return null;
}
return row[pos];
}
var objects =
from row in data
select new {
Id = int.Parse(GetValue(row, headerPositions["id"])),
FirstName = GetValue(row, headerPositions["first_name"]),
LastName = GetValue(row, headerPositions["last_name"]),
Email = new MailAddress(GetValue(row, headerPositions["email"])),
Foo = GetValue(row, headerPositions["foo"]),
}; I don't see the advantage of the new method over this approach.
And why would requiring hash lookups be a problem? |
So you want to repeat all those headers twice? Okay, I know you're going to say that we could put them in constant strings so it's not a huge deal, but still, you've got a lot of ceremony going on like
It wouldn't. The idea here was that the indicies as well as the source are themselves query comprehensions and neither may have been materialised in advance. I guess I should have known better than to post a naïve example. 😉 That said, I exactly expected (and thus appreciate) this kind of challenging and feedback so please keep charging on! |
Implemented and released in SuperLinq v4.1.0 |
I'd like to suggest adding an operator that will return items of a sequence at given sequence of zero-based positions or indicies. It will traverse the source items and an indicies sequence in order and return the items at the corresponding indicies. Duplicate indicies are allowed. For most optimal (unbuffered) operation, indicies would be given in increasing order.
The signature would be as follows:
Simpler overloads could be added.
If
indicies
are in ascending order thenlookBackSize
is immaterial. Ifindicies
are unordered thenlookBackSize
will be the size of an internal scrolling buffer of source items that enables looking back.The
missingSelector
argument allows projection of a result item when an index is not found, such as when it is less than zero or greater or equal to the number of items insource
.Suppose the following:
The table below shows what
result
will contain for given inputs ofindicies
andlookBackSize
:indicies
lookBackSize
result
Things to note:
lookBackSize
is 0) but example 7 fixes that.lookBackSize
is 1 and so a look-back that far from index 3 is no longer possible.Example
Output
The text was updated successfully, but these errors were encountered: