Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Simple Particle Engine for a shooter game
Snake / Nibbles clone in C and Ncurses
Please improve
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Popular
Parsing of XML data has high CPU usage
Snake / Nibbles clone in C and Ncurses
Please improve
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Active Record getting unique records
Simple Particle Engine for a shooter game
Breadth first cartesian product iterator
php refactoring
first BST
Pastable version of
Algorithm for zig-zagging over potentially infinite sets
<pre class='prettyprint' language='cs'>using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using NUnit.Framework; namespace MagicMvc.Tests { [TestFixture] public class ZigZaggerTests { IEnumerable<int> NaturalNumbers() { int i = 0; while (true) { yield return i++; } } public class Tuple<T0, T1> { public Tuple(T0 first, T1 second) { this.First = first; this.Second = second; } public T0 First { get; private set; } public T1 Second { get; private set; } } readonly Action<Tuple<int, int>, int, int> assert = (tuple, a, b) => Assert.That(tuple.First == a && tuple.Second == b); static IEnumerable<Tuple<TX, TY>> ZigZag<TX, TY>(IEnumerable<TX> xs, IEnumerable<TY> ys) { var xValues = new List<TX>(); IEnumerator<TX> xEnumerator = xs.GetEnumerator(); IEnumerator<TY> yEnumerator = ys.GetEnumerator(); var yValues = new List<TY>(); while (xEnumerator.MoveNext() && yEnumerator.MoveNext()) { xValues.Add(xEnumerator.Current); yValues.Add(yEnumerator.Current); for (int i = xValues.Count - 1; i >= 0; i--) { yield return new Tuple<TX, TY>(xValues[i], yValues[xValues.Count - i - 1]); } } } [Test] public void CombineAFuncWithOneParameter() { Expression<Func<int, bool>> filterOne = i => i != 1; Expression<Func<int, bool>> filterTwo = i => i != 2; Expression<Func<int, bool>> combined = filterOne.And(filterTwo); IQueryable<int> filtered = this.NaturalNumbers().Take(3).AsQueryable().Where(combined); Assert.AreEqual(0, filtered.Single()); } [Test] public void CombineFuncsWithTwoParameters() { Expression<Func<int, int, bool>> filterOne = (x, y) => x != y; Expression<Func<int, int, bool>> filterTwo = (x, y) => x*2 == y; } [Test] public void TestInfiniteSets() { List<Tuple<int, int>> tuples = ZigZag(this.NaturalNumbers(), this.NaturalNumbers()).Take(10).ToList(); this.assert(tuples[0], 0, 0); this.assert(tuples[1], 1, 0); this.assert(tuples[2], 0, 1); this.assert(tuples[3], 2, 0); this.assert(tuples[4], 1, 1); this.assert(tuples[5], 0, 2); this.assert(tuples[6], 3, 0); this.assert(tuples[7], 2, 1); this.assert(tuples[8], 1, 2); this.assert(tuples[9], 0, 3); } } }</pre> <a href="http://www.refactormycode.com/codes/806-algorithm-for-zig-zagging-over-potentially-infinite-sets" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>