Sunday, July 1, 2012

The Boolean adjective comes from George Boole.

George Boole in the 19th century came up with Boolean Logic of which Wikipedia says: "The operations are usually taken to be conjunction, disjunction, and negation, with constants 0 and 1." This means that Boolean as an adjective may have a true/false connotation or an and/or/not connotation. In C# clearly there is a true/false nature to a Boolean variable. When I started doing C#, after having done 3D Studio Max in a former life a dozen years ago, this definition of Boolean confused me because 3D Studio Max uses an and/or/not definition for "Boolean."

In 3D Studio Max one could overlay two independent meshes and then make a new mesh out of the two operands. The common area between the two operands could be kept while the uncommon areas were dropped creating a shape the shape of the space where the two items overlaid, or the two operands could be welded into one whole dropping nothing, or one operand would disappear while subtracting the area it had in common with its partner from its partner not unlike taking a bite out of an apple.

3D Studio Max Boolean operations (circa 2000) for making one ... on Twitpic

In reading about LINQ in C# 4.0 in a Nutshell I have learned how to do the 3D Studio Max style operations to two "overlaying" collections!

using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace LinqStuff.Tests
{
   [TestClass]
   public class LinqTests
   {
      public LinqTests()
      {
      }
      
      [TestMethod]
      public void AndTest()
      {
         int[] firstFiveDigits = new int[] { 1, 2, 3, 4, 5 };
         int[] fourDigitsAfterThree = new int[] { 4, 5, 6, 7 };
         int[] intersect = firstFiveDigits.Intersect(fourDigitsAfterThree).ToArray();
         Assert.AreEqual(intersect.Length, 2);
         Assert.AreEqual(intersect[0], 4);
         Assert.AreEqual(intersect[1], 5);
      }
      
      [TestMethod]
      public void OrTest()
      {
         int[] firstFiveDigits = new int[] { 1, 2, 3, 4, 5 };
         int[] fourDigitsAfterThree = new int[] { 4, 5, 6, 7 };
         int[] union = firstFiveDigits.Union(fourDigitsAfterThree).ToArray();
         Assert.AreEqual(union.Length, 7);
         Assert.AreEqual(union[0], 1);
         Assert.AreEqual(union[1], 2);
         Assert.AreEqual(union[2], 3);
         Assert.AreEqual(union[3], 4);
         Assert.AreEqual(union[4], 5);
         Assert.AreEqual(union[5], 6);
         Assert.AreEqual(union[6], 7);
      }
      
      [TestMethod]
      public void NotTest()
      {
         int[] firstFiveDigits = new int[] { 1, 2, 3, 4, 5 };
         int[] fourDigitsAfterThree = new int[] { 4, 5, 6, 7 };
         int[] except = firstFiveDigits.Except(fourDigitsAfterThree).ToArray();
         Assert.AreEqual(except.Length, 3);
         Assert.AreEqual(except[0], 1);
         Assert.AreEqual(except[1], 2);
         Assert.AreEqual(except[2], 3);
      }
   }
}

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete