MoreLINQ icon indicating copy to clipboard operation
MoreLINQ copied to clipboard

Lead, Lag & Window behavior are incoherent

Open Orace opened this issue 6 years ago • 3 comments

Lead and Lag return a default value when looking before or after the source sequence. Window stays in the source sequence.

The difference of behavior is misleading. We may think to homogenize this and have this grammar:

{Lead|Lag|Window}{_|Left|Right|LeftAndRight} 

This is a breaking change for current Lead and Lag behavior.

Orace avatar Nov 01 '19 18:11 Orace

An other option is to made Window relaxed (ie: behave like WindowLeftAndRight) and introduce Strict method.

{Lead|Lag|Window}{_|Left|Right|Strict}

This is a breaking change for current Window behavior.

Orace avatar Nov 01 '19 19:11 Orace

The difference of behavior is misleading.

Please expand. You didn't say why apart from that the 3 differ in behaviour and that two require defaults. Each has arguments that pertain to its function.

atifaziz avatar Nov 02 '19 10:11 atifaziz

For readability, I use the not yet merged ValueTuple providing overloads of Lead and Lag.

Current behavior is:

var nums = new[] {1,2,3 };
nums.Lag(1);         // [1,0],[2,1],[3,2]
nums.Lead(1);        // [1,2],[2,3],[3,0]
nums.Window(2);      // [1,2],[2,3]
nums.WindowLeft(2);  // [1,2],[2,3],[3]
nums.WindowRight(2); // [1],[1,2],[2,3]

All of this methods are providing pairs of neighbour elements from the original source. What is misleading for me is the behavior around the first and the last element.

  • Lead and Lag provide values external of the sequence.
  • WindowLeft and WindowsRight just doesn't include it.

What is missing is:

  • A window left and right
  • A padding value for Window

And my expected behavior is:

var nums = new[] {1,2,3 };
nums.Lag(1);         // [2,1],[3,2]
nums.Lead(1);        // [1,2],[2,3]
nums.Window(2);      // [1,2],[2,3]

Then add overloads for other needs: (I put the parameters names for readability).

var nums = new[] {1,2,3 };
nums.Lag(1, paddingValue: 0);    // [1,0],[2,1],[3,2]
nums.Lead(1, paddingValue: 0);   // [1,2],[2,3],[3,0]
nums.Window(2, paddingValue: 0); // [0,1],[1,2],[2,3],[3,0]

WindowLeft and WindowRight should be renamed to enlighten their behaviors.

Orace avatar Nov 02 '19 17:11 Orace