Infragistics WPF controls

Programming Challenges with functional programming in F#: Article #1 the Collatz conjecture

Hello and welcome to the first installment of posts on solving common programming challenges using functional programming in F#. I've been in software development professionally for over ten years now.  I've gained a lot of experience with many different programming languages.  Object oriented design was my main tool of choice.  Object oriented design while effective is not the only software design methodology available to you as a programmer.  One of the most important lessons to be learned in software development in my opinion is to use the best tool for the job. I've decided to learn functional programming in order to improve my problem solving skills.  F# and functional programming requires a different approach to solving problems than other methodologies.  Trying to solve problems using a new way of thinking is a great way to stimulate neuroplasticity and learn a new skill.   The 3n+1 programming challenge The programming challenge that I chose to begin learning F# is called the 3n+1 problem.  I discovered the challenge in the book "Programming Challenges: The Programming Contest Training Manual" by Steven S. Skiena and Miguel A. Revilla which you can find on amazon.com here Here's the gist: You start by creating an algorithm that accepts integer n as input.  The algorithm will generate a sequence of numbers based on it's input.  Starting with n, the algorithm will check to see if n is odd or even.  if n is odd, then you will generate the next value in the sequence by calculating n * 3 + 1.  If n is even, the next value in the sequence is n / 2.  You will then repeat the process for each new value added to the sequence until n = 1. Here is an example of the sequence generated when n = 22; 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 The cycle length is the length of the sequence generated for a given number.  In this case, the cycle length is 16 because the sequence has 16 integers (including the input as well as the number 1). Given two numbers i and j, you are to determine the maximum cycle length over all numbers between i and j including both start and end points. Here are some examples: i j max cycle length 1 10 20 100 200 125 201 210 89 900 1000 174 Let's start by discussing some of the fundamental differences between between the two solutions. The C# solution I've been working in C# for over a decade now.  Naturally my instincts tell me to try to tackle this problem using object oriented design principles.  So here is a solution that I design using C# and OOP. CalculationResult.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ThreePlusOneCSharp { public class CalculationResult { public int InputValue { get; set; } public List<int> ResultList { get; set; } public int CycleLength { get { return ResultList.Count; } } public CalculationResult() { this.ResultList = new List<int>(); } } } Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ThreePlusOneCSharp { class Program { static void Main(string[] args) { int startRange = int.Parse(args[0]); int endRange = int.Parse(args[1]); int maxCycleLength = _CalculateMaxCycleLengthForRange(startRange, endRange); Console.WriteLine("The max Cycle length for {0}..{1} is = '{2}'", args[0], args[1], maxCycleLength.ToString()); } private static int _CalculateMaxCycleLengthForRange(int startRange, int endRange) { var cycleLengths = new List<int>(); for (int rangeIndex = startRange; rangeIndex <= endRange; rangeIndex++) { var calcResult = new CalculationResult(); calcResult.InputValue = rangeIndex; calcResult.ResultList.Add(rangeIndex); var result = _CalculateNextInput(calcResult); cycleLengths.Add(result.CycleLength); } return cycleLengths.Max(); } private static CalculationResult _CalculateNextInput(CalculationResult result) { result.ResultList.Add(result.InputValue); if (result.InputValue % 2 == 0) result.InputValue = (result.InputValue / 2); else result.InputValue = (result.InputValue * 3) + 1; if (result.InputValue == 1) return result; return _CalculateNextInput(result); } } } As you can see, I created a class to hold the input value as well as a list of all new inputs generated for the specified input. I created a property called CycleLength which returns the count of the list of inputs generated. We use recursion to call _CalculateNextInput which allows us to keep up with the cycle length for the given input. The _CalculateMaxCycleLengthForRange routine will iterate through all numbers between i and j and keep a list of each cycle length. Once all integers in the range have been processed, we return the max cycle length from all cycle lengths in the list. The F# solution let rec generateSequence num = seq { yield num if num <> 1 then match num with | x when x % 2 = 0 -> yield! generateSequence (x / 2) | x -> yield! generateSequence ((x * 3) + 1) } let getMaxCycleLength range = let valueSequences = seq { for number in range do yield seq { yield! generateSequence number } } let cycleLengthSequence = seq { for sequence in valueSequences do yield Seq.toList(sequence).Length } Seq.max(cycleLengthSequence) [<EntryPoint>] let main args = let low, high = int args.[0], int args.[1] let input = [low .. high] let cycleLength = getMaxCycleLength input System.Console.Write(" {0} ", cycleLength) 0 Conclusion Join me for my next article where I will discuss in detail the two different implementations.      


ASP.NET MVC Basics: How to create a HtmlHelper

 ASP.NET MVC Basics: How to create a HtmlHelper Hello! And welcome to another tutorial on refactorthis.net.  This is the first tutorial in a series on ASP.NET MVC development.  This first installment will cover how to create an HtmlHelper.   What is ASP.NET MVC? I apologize if I'm stating the obvious, however, this is a tutorial on the basics so I'm approaching the tutorial with the assumption that the reader has no knowledge of ASP.NET, MVC, or ASP.NET MVC.  I'm sure you noticed that I made three references to the technology.  That's because ASP.NET MVC is more than one technology.  First we have ASP.NET, which is a server side web development technology created by Microsoft that utilizes the .NET framework.  Simply put, ASP.NET allows you to write dynamic web pages in .NET language of your choice (C#, VB.NET, etc..).  ASP.NET is an open source technology and it can be developed using the .NET framework or the Mono runtime.  You can find all sorts of wonderful information at http://www.asp.net/ . ASP.NET comes in two "flavors".  ASP.NET WebForms which is the original flavor, and ASP.NET MVC.  ASP.NET MVC is made up of two concepts.  First and foremost there is the MVC design pattern.  The MVC or Model-View-Controller design pattern was introduced by Trygve Reenskaug in the 1970's.  The design pattern has also be explained by Martin Fowler in his wonderful book on design patterns called Patterns of Enterprise Application Architecture . The idea behind the MVC pattern is that you have data that you wish to display (Model), you have a presentation layer in which you wish to display your Model (View), and you have a class that handles the interaction between the View and the Model (controller).  The MVC design pattern is an excellent example of a design with proper separation of concerns.  Applications that implement the MVC design pattern are generally loosely coupled, and easy to test (unit testing).  In ASP.NET MVC, the View is a web page, the model can be any class that holds data that you want to display in the View, and a controller class. Now that I've explained ASP.NET and MVC, I'll now explain ASP.NET MVC.  You see, not only is ASP.NET MVC an implementation of the Model-View-Controller design pattern, it's also a framework built by Microsoft to support the implementation of the MVC design pattern.  You will see as you begin working with MVC that the framework is a large part of what makes ASP.NET MVC what it is.  You could implement ASP.NET MVC without the framwork, however, I don't recommend it.  The framework will create controller's for you, create Views that are bound to a strongly typed model of your choice, and much more.  Using the ASP.NET MVC framework, you can create a basic CRUD web application in a very short amount of time. I hope that this gives you a nice overview of the ASP.NET MVC framework and design pattern. HtmlHelper: What they are and how to create them I'll explain HtmlHelpers by taking you through implementing one..  We'll start by firing up Visual Studio and create a new ASP.NET MVC 4 project (note: I'm using VS 2012, however, if you have an earlier version installed, simply choose whichever ASP.NET MVC version that you have available to you.  If you don't have any ASP.NET MVC templates, you can use the Web platform installer to install ASP.NET MVC on your system.       The next dialog allows you to choose which type of ASP.NET MVC application that you'd like to create.  We will pick an Internet Application, with the Razor View engine and a unit testing project as shown below.     Now click OK and your project will be created for you.  The ASP.NET MVC Framework will create a lot of boiler plate code behind the scenes.  This includes "Forms based Authentication" in the web.config file as well as an AccountController which is a controller to handle authentication to the site.  There is also a default controller called HomeController. We are going to create an html helper that creates an HTML table.  This is merely an example and is not particularly useful, however, it will show you how to create and use an HTML Helper.  An HTML helper is nothing more than an extension method which returns a string of HTML.  Here is the official definition of an extension method from MSDN: Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. Creating the HtmlHelper  Create a folder called "Helpers" in your solution.  In the folder, create a static class called TableExtensions.cs .  The class will look like this: TableExtensions.cs using System; using System.Text; using System.Web.Mvc; namespace HtmlHelpers.Helpers { public static class TableExtensions { public static MvcHtmlString Table(this HtmlHelper helper, string id, string name, int rows, int columns) { if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id"); if(string.IsNullOrEmpty("name")) throw new ArgumentNullException("name"); if (rows <= 0) throw new IndexOutOfRangeException(); if (columns <= 0) throw new IndexOutOfRangeException(); StringBuilder tableBuilder = new StringBuilder(); tableBuilder.Append(string.Format("<table id=\"{0}\" name=\"{1}\">", id, name)); for (int rowCounter = 0; rowCounter < rows; rowCounter++) { tableBuilder.Append("<tr>"); for (int columnCounter = 0; columnCounter < columns; columnCounter++) { tableBuilder.Append("<td>"); tableBuilder.Append(rowCounter.ToString()); tableBuilder.Append("</td>"); } tableBuilder.Append("</tr>"); } tableBuilder.Append("</table>"); return new MvcHtmlString(tableBuilder.ToString()); } } } As you can see, we've created a static class and a static method. The the first method parameter is  this HtmlHelper helperThis is where the magic happens. This allows us to use the method from the View like all other HTML helpers.The other methods specify the number of rows and columns that should be built in the HTML table.Here is an example of how to use the helper: @using HtmlHelpers.Helpers; @{ ViewBag.Title = "Index"; } <h2>Index</h2> @Html.Table("myTable", "myTableName", 4, 4)The output looks like this: And there you have it folks.  I hope you enjoyed this tutorial.  If you have any questions please don't hesitate to leave a comment. Thanks for reading.


WPF Succinctly source code for download

I've scrambled to recover the example source for WPF Succinctly from Syncfusion from a dying hard drive, but I've located most of the source code for your reference.  Simply click on the banner below to download.  Please let me know if there are any problems found.  Thanks for reading.        


WPF Succinctly: my first ebook published by Syncfusion

      It's with great pleasure that I let you know about my ebook, WPF Succinctly, published by the great team over at Syncfusion.  This book is a part of their succinctly series which offers a wide variety of free ebooks on the hottest software development topics.  I'm proud of the book and especially of the final product.  I tried to start with the basics (controls, xaml basics) and then work my way up to more advanced topics like MVVM and Commands.  The book is full of examples for you to follow along. I'm currently working on a following up to WPF succinctly called "Orubase Succinctly" which is a book on Syncfusion's mobile hybrid platform.  You can learn more about the product on the official Orubase page. Don't forget to check out the other FREE books in the succinctly series here.  Also, I'd like to recommend that you check out the trial versions of the Syncfusion's essential studio suite of controls.  The suite includes top notch controls for ASP.NET, ASP.NET MVC, WPF, Silverlight, Windows phone, WinRT, WinForms and more.   I would love any feedback regarding the book.  And please be on the lookout for Orubase succinctly which I should be wrapping up soon. Until next time, Buddy James  


About the author

My name is Buddy James.  I'm a Microsoft Certified Solutions Developer from the Nashville, TN area.  I'm a Software Engineer, an author, a blogger (http://www.refactorthis.net), a mentor, a thought leader, a technologist, a data scientist, and a husband.  I enjoy working with design patterns, data mining, c#, WPF, Silverlight, WinRT, XAML, ASP.NET, python, CouchDB, RavenDB, Hadoop, Android(MonoDroid), iOS (MonoTouch), and Machine Learning. I love technology and I love to develop software, collect data, analyze the data, and learn from the data.  When I'm not coding,  I'm determined to make a difference in the world by using data and machine learning techniques. (follow me at @budbjames).  

Related links

Month List