Closed
Labels
Milestone
Description
Sometimes the Join would have a key being calculated. So it would be useful to have an overload of Join passing the matching key along with the value selector
public IEnumerable<V> Join<T0,T1,K,V>(this IEnumerable<T0> source,IEnumerable<T0> target,Func<T0,K> key0Selector,Func<T1,K> key1Selector,Func<V,K,T0,T1> valueSelector);
////
var results = items0.Join(items1,(outer) => ComplexLogicForKey0(outer),(inner) => ComplexLogicForKey1(inner),(key,outer,inner) => (key,outer,inner));Maybe the tuple version of Join should also use this as return value instead
/// overload of Join
public IEnumerable<(K,T0,T1)> Join<T0,T1,K>(this IEnumerable<T0> source,IEnumerable<T0> target,Func<T0,K> key0Selector,Func<T1,K> key1Selector);
Activity
Clockwork-Muse commentedon Feb 20, 2019
Curious question:
If the derivation logic for a key is so expensive you want to pass it along with the result, would your program perform better/more consistently if you calculated/stored it in the original object? Especially since it has to iterate through all the inner elements before it can return even the first result.
Thaina commentedon Feb 20, 2019
@Clockwork-Muse Even today I could actually just select a key-value pair like this
This proposal is just for convenient and compactness, because the
Joinfunction itself always know the key to comparing equality so it should just be able to pass alongClockwork-Muse commentedon Feb 20, 2019
Well yeah.
What I meant was, would your program do better if
itemalready hadkeycalculated/stored inside of it?Thaina commentedon Feb 20, 2019
@Clockwork-Muse If it would be used only once when joining then why should I calculate and store it beforehand?
Why do you think we have
Lazy?Why it should take up memory when it not being used until calling
Joinfunction?With my proposal, if it not being matched with anything then it would be popped out immediately
It almost always have more advantage to just do something when it really needed. Why you are trying to think about anything unnecessary here? Why you need to try to thinking about doing anything when it really not necessary like caching something before really using it
Why???
Really you should ask yourself first that why you should cache value instead of argue with me why we should pass a value that would be very efficient to just pass what it already just being gotten
Clockwork-Muse commentedon Feb 20, 2019
.... what I was curious about was whether or not this value, whatever it was, was truly used once only. Without more knowledge of your usecase, my default assumption tends to be that it's probably used more widely. You could of course implement it with a
Lazy<T>- the exact first use wasn't at issue.Side note:
If you mean, "if there's no match between the left and the right operands, then the result is dropped immediately", that can only happen when the iterator is disposed. Like I said before, it has to calculate the key for all elements of the right operand before it can return the first row, and they have to hang around until the last element of the left operand is processed. Granted, yes, left elements are dropped after it moves to the next, so it might be beneficial there.
Thaina commentedon Feb 21, 2019
@Clockwork-Muse That's the point to not cache anything. The
Joinfunction should do thing as efficient as possible for the assumption of only onceI was thinking from the start that if the logic will need to cache anything, it will be caching internally anyway. If we cache it beforehand then it means we have unnecessary copy of what will be cached again
And complex logic might not be inefficient, just hard to write. It could be just a math and switch operation which just take a long line but could be run very fast. There are many reason that taking what already there is just better
eiriktsarpalis commentedon Nov 23, 2020
As already mentioned, it is fairly straightforward to encode the desired behaviour on top of the existing join implementation:
It does not seem like this request is sufficiently popular to consider adding it to System.Linq. As such, I am going to close this issue. Thank you for your contribution!