Geeks With Blogs
Works on my machine

One year ago I had a little crush on Python. Although I consider myself as a hardcore C# coder I think it is a good thing to try other languages. Even if we do not plan to change our major, it is nice to grasp other techniques and practice new ways of thinking, which can be later incorporated into our favorite language.

First odd thing I read about Python was that a developer can use negative array indices. My reaction was: Why would I even want to get an IndexOutOfRangeException? That is just silly. But then I read what they actually do. They are just like the normal ones. The only difference is that they index the array backwards, for example: -1 means last, -2 means one before last, and so on. That is really handy. I cannot remember how many times I wrote count-1 or count-i.

After that I discovered that Python has an even cooler feature called array slicing. It is something like a quick for loop version. Let me show you an example:

array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print array[1:9:2]

The above code prints [1,3,5,7].

So formally speaking slicing has the following form (each argument can be negative, all arguments are optional):

[ start_index : exclusive_boundary : iteration_step ]

This is a really powerful though succinct syntax. Look at the following two examples:

array[::-1] #reverses the array
array[1:-1] #skips first and last element

Then I stared to think about those features - how can I port them to C# ?

NSlice

I decided to give it a try and created a .NET library. At the moment there are 3 extension methods:

  • Slice – performs slice on a passed collection.
  • At - works like an ElementAt extension. The only difference is that the index argument passed to an At extension can be negative.
  • AtOrDefault - works like an ElementAtOrDefault extension, but it also accepts negative indices.

But there is more into it. NSlice was written to allow slicing 3 types of .NET collections in the most efficient manner:

  • Indexed collections (IList<> implementations) - slicing is performed eagerly and instantly. It does not matter whether the collection has 10 elements or 10 million elements. This is because the result is not created by copying elements, but by creating a transparent view over source collection.
  • Enumerables (IEnumerable<> implementations) - slicing is performed lazily. Each possible slice scenario was implemented separately to achieve the best speed performance and least memory footprint. It fits nicely into the LINQ model and could be even used to slice a stream, if the latter was wrapped into IEnumerable<> implementation.
  • Strings - slicing is performed eagerly and a new string is returned as a result.


Download

Available on NuGet – type “nslice”.
Project website and source code: https://github.com/nabuk/NSlice
Wiki: https://github.com/nabuk/NSlice/wiki

Posted on Sunday, December 1, 2013 8:01 PM C# , LINQ , NSlice | Back to top


Comments on this post: Slicing for .NET

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Jakub Niemyjski | Powered by: GeeksWithBlogs.net