Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To rotate array, do <code>a.Slice(1, null).Concat(a.Slice(null, 1))</code>. </p> <p>Here's my stab at it. <code>a.Slice(step: -1)</code> gives a reversed copy as <code>a[::-1]</code>.</p> <pre><code>/// &lt;summary&gt; /// Slice an array as Python. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt; /// &lt;param name="array"&gt;&lt;/param&gt; /// &lt;param name="start"&gt;start index.&lt;/param&gt; /// &lt;param name="end"&gt;end index.&lt;/param&gt; /// &lt;param name="step"&gt;step&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; /// &lt;remarks&gt; /// http://docs.python.org/2/tutorial/introduction.html#strings /// +---+---+---+---+---+ /// | H | e | l | p | A | /// +---+---+---+---+---+ /// 0 1 2 3 4 5 /// -6 -5 -4 -3 -2 -1 /// &lt;/remarks&gt; public static IEnumerable&lt;T&gt; Slice&lt;T&gt;(this T[] array, int? start = null, int? end = null, int step = 1) { array.NullArgumentCheck("array"); // step if (step == 0) { // handle gracefully yield break; } // step &gt; 0 int _start = 0; int _end = array.Length; // step &lt; 0 if (step &lt; 0) { _start = -1; _end = -array.Length - 1; } // inputs _start = start ?? _start; _end = end ?? _end; // get positive index for given index Func&lt;int, int, int&gt; toPositiveIndex = (int index, int length) =&gt; { return index &gt;= 0 ? index : index + length; }; // start if (_start &lt; -array.Length || _start &gt;= array.Length) { yield break; } _start = toPositiveIndex(_start, array.Length); // end if (_end &lt; -array.Length - 1) { yield break; } if (_end &gt; array.Length) { _end = array.Length; } _end = toPositiveIndex(_end, array.Length); // slice if (step &gt; 0) { // start, end if (_start &gt; _end) { yield break; } for (int i = _start; i &lt; _end; i += step) { yield return array[i]; } } else { // start, end if (_end &gt; _start) { yield break; } for (int i = _start; i &gt; _end; i += step) { yield return array[i]; } } } </code></pre> <p>nunit tests: </p> <pre><code>[Test] // normal cases [TestCase(3, 5, 1, 3, 4)] [TestCase(0, 5, 1, 0, 4)] [TestCase(3, null, 1, 3, 9)] [TestCase(0, null, 1, 0, 9)] [TestCase(null, null, 1, 0, 9)] [TestCase(0, 10, 1, 0, 9)] [TestCase(0, int.MaxValue, 1, 0, 9)] [TestCase(-1, null, 1, 9, 9)] [TestCase(-2, null, 1, 8, 9)] [TestCase(0, -2, 1, 0, 7)] // corner cases [TestCase(0, 0, 1, null, null)] [TestCase(3, 5, 2, 3, 3)] [TestCase(3, 6, 2, 3, 5)] [TestCase(100, int.MaxValue, 1, null, null)] [TestCase(int.MaxValue, 1, 1, null, null)] [TestCase(-11, int.MaxValue, 1, null, null)] [TestCase(-6, -5, 1, 4, 4)] [TestCase(-5, -6, 1, null, null)] [TestCase(-5, -5, 1, null, null)] [TestCase(0, -10, 1, null, null)] [TestCase(0, -11, 1, null, null)] [TestCase(null, null, 100, 0, 0)] // -ve step [TestCase(null, null, -1, 9, 0)] [TestCase(-7, -5, -1, null, null)] [TestCase(-5, -7, -1, 5, 4)] [TestCase(-5, -7, -2, 5, 5)] [TestCase(-7, null, -1, 3, 0)] public void Slice01(int? s, int? e, int i, int? first, int? last) { var a = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var slice = a.Slice(start: s, end: e, step: i).ToArray(); Print(slice); if (first.HasValue) { Assert.AreEqual(first, slice.First()); } if (last.HasValue) { Assert.AreEqual(last, slice.Last()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload