Archive for the ‘Programming’ Category

Getting a list of numbers between two values

This is yet another extension.

I don’t like Enumerable.Range() for the sole reason you have to give a start index and a count. So I made up my own. This is an extension for an integer type and will allow you to specify two values that you want a range for

public static IEnumerable<int> RangeTo(this int startValue, int endValue, int step)
{
	return (endValue < startValue) ?
		Enumerable.Range(endValue, startValue - endValue + 1).Reverse() :
		Enumerable.Range(startValue, endValue - startValue + 1);
}

This will also allow for reverse lists – see how to use it below:

1.RangeTo(10);  // {1,2,3,4,5,6,7,8,9,10}
5.RangeTo(10);  // {5,6,7,8,9,10}
10.RangeTo(5);  // {10,9,8,7,6,5}
3.RangeTo(3);   // {3}

int x = 15;
int y = 17;
x.RangeTo(10);  // {15,14,13,12,11,10}
y.RangeTo(x);  // {17,16,15}
x.RangeTo(y);  // {15,16,17}
13.RangeTo(x); // {13,14,15}

Random sort on IEnumerable object

Quick extension on an IEnumerable object to return the collection in a random order:

public static IEnumerable<T> Random<T>(this IEnumerable<T> source)
{
	return source.OrderBy(x => Guid.NewGuid());
}

For example, to return a random set of 5 items from the collection:

var randomSet = myEnumerable.Random().Take(5);

If anyone knows of a better way, let me know :)

Fon Bookmarklet

I’m about to go on a short holiday around Europe. I’ve my Fon router plugged in, ready to get the benefits of free internet on my iPod Touch whilst away. Typed a quick ‘fon bookmarklet’ search into google and only found a youtube video of some chinese person showing his working (but not actually giving the code)

So here’s mine. It’s simple, but it works:
javascript: document.getElementById('login_email').value='EMAIL_ADDRESS'; document.getElementById('login_password').value='PASSWORD'; document.getElementById('login_email').parentElement.submit()

And here’s it in link form for clicking and dragging to the bookmarks toolbar:
Fon Login

My own link has obfuscation on the password just so that it’s not blatantly obvious in the link (maybe I’ll do some obfuscation code eventually), but changing those details would work for the ‘Fon Login’ page that loads up when connecting to a FON_AP wifi point. It works in Safari, at least. The ‘parentElement’ is null in Firefox but since this is for an iPod Touch, I don’t really mind. If anyone knows why it’s null then just let me know.