<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Sergio Pereira : Juicy</title><link>http://devlicio.us/blogs/sergio_pereira/archive/tags/Juicy/default.aspx</link><description>Tags: Juicy</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Language Envy - C# needs Ranges</title><link>http://devlicio.us/blogs/sergio_pereira/archive/2010/01/02/language-envy-c-needs-ranges.aspx</link><pubDate>Sat, 02 Jan 2010 10:55:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:54862</guid><dc:creator>sergiopereira</dc:creator><slash:comments>14</slash:comments><description>	&lt;p&gt;
		As soon as I started learning Ruby, a few years ago, I got immediately hooked 
		on its &lt;a href="http://ruby-doc.org/core/classes/Range.html"&gt;&lt;code&gt;Range&lt;/code&gt;&lt;/a&gt; class. 
		I could not believe I had been programming in .NET without them for so long.
	&lt;/p&gt;

	&lt;p&gt;
		I like to think of range objects as the specification for a &lt;code&gt;for&lt;/code&gt;
		loop, packaged in an object that can be passed around. That&amp;#39;s not the whole story, though.
		Ranges also represent sequences or intervals, which can be queried for intersection or
		containment. See the following Ruby sample code.
	&lt;/p&gt;

		&lt;pre name="code" class="ruby:nogutter"&gt;#declare a Range object
summer_months = 6..9
#enumerate it
summer_months.each {|m| puts &amp;quot;#{Date.new(2000, m, 1).strftime(&amp;#39;%B&amp;#39;)} is a Summer month.&amp;quot; }
#other handy features
summer_months.include? 7 # ==&amp;gt; true
summer_months.to_a # ==&amp;gt; [6, 7, 8, 9]  (converted to array)&lt;/pre&gt;


	&lt;h3&gt;I needed that in C#&lt;/h3&gt;
	&lt;p&gt;
		That was back when the CLR 2.0 was just about to be released and I ended up
		writing my own Range&lt;/code&gt; class. I have used this class in a handful of
		projects since then and I&amp;#39;m still surprised that we don&amp;#39;t have it in the
		standard .NET class library. The closest thing I&amp;#39;m aware of is the method
		&lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx"&gt;&lt;code&gt;System.Linq.Enumerable.Range(int, int)&lt;/code&gt;&lt;/a&gt;,
		which is rather limited (it only enumerates integers one by one).
	&lt;/p&gt;

	&lt;p&gt;
		Here&amp;#39;s the code that I came up with, which has served me well for many years. First 
		an abstract base class. I&amp;#39;ll omit parts of the code for brevity but I&amp;#39;ll give you 
		a link for the complete source at the end.
	&lt;/p&gt;

	&lt;pre name="code" class="csharp:nogutter"&gt;public abstract class RangeBase&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt; where T : IComparable
{
  public T Start { get; set; }
  public T End { get; set; }

  protected RangeBase(T start, T end)
  {
    // ... snip ...
  }

  protected abstract T GetNextItem(T currentItem);

  public IEnumerator&amp;lt;T&amp;gt; GetEnumerator()
  {
    T item = Start;

    while (true)
    {
      if (item.CompareTo(End) &amp;gt; 0)
        break;

      yield return item;

      item = GetNextItem(item);
    }
  }


  IEnumerator IEnumerable.GetEnumerator()
  {
    return GetEnumerator();
  }

  public bool Intersects(RangeBase&amp;lt;T&amp;gt; otherRange)
  {
    // ... snip ...
  }

  public bool Contains(RangeBase&amp;lt;T&amp;gt; otherRange)
  {
    // ... snip ...
  }

  public bool Contains(T element)
  {
    return (Start.CompareTo(element) &amp;lt;= 0) &amp;amp;&amp;amp; (End.CompareTo(element) &amp;gt;= 0);
  }
}&lt;/pre&gt;

	&lt;p&gt;
		There&amp;#39;s also a generic concrete type and a bunch of the more common
		implementations for convenience.
	&lt;/p&gt;

	&lt;pre name="code" class="csharp:nogutter"&gt;public class Range&amp;lt;T&amp;gt; : RangeBase&amp;lt;T&amp;gt; where T : IComparable
{
  public Func&amp;lt;T, T&amp;gt; GetNext { get; private set; }

  public Range(T start, T end, Func&amp;lt;T, T&amp;gt; getNext)
    : base(start, end)
  {
    GetNext = getNext;
  }

  protected override T GetNextItem(T currentItem)
  {
    return GetNext(currentItem);
  }
}

public class Int32Range : RangeBase&amp;lt;int&amp;gt;
{
  public Int32Range(int start, int end) : base(start, end){}
  protected override int GetNextItem(int currentItem)
  {
    return currentItem + 1;
  }
}

public class Int64Range : RangeBase&amp;lt;long&amp;gt; { /* ... snip ... */ }
public class DayRange : RangeBase&amp;lt;DateTime&amp;gt; { /* ... snip ... */ }
public class HourRange : RangeBase&amp;lt;DateTime&amp;gt; { /* ... snip ... */ }
//...etc...&lt;/pre&gt;


	&lt;h3&gt;Coding with Ranges&lt;/h3&gt;
	&lt;p&gt;
		With the above classes I can write code that is similar in functionality to the
		Ruby version.
	&lt;/p&gt;
	&lt;pre name="code" class="csharp:nogutter"&gt;//declare a range of numbers
var summerMonths = new Int32Range(6, 9);
//enumerate it
foreach(var m in summerMonths) 
  Console.WriteLine(new DateTime(2000, m, 1).ToString(&amp;quot;MMMM&amp;quot;) + &amp;quot; is a Summer month.&amp;quot;);
//other handy features
summerMonths.Contains(7); // ==&amp;gt; true
summerMonths.Contains(new Int32Range(7, 8)); // ==&amp;gt; true
summerMonths.ToArray(); // ==&amp;gt; [6, 7, 8, 9]  (using LINQ extensions)&lt;/pre&gt;

	&lt;p&gt;
		We can also use the generic &lt;code&gt;Range&lt;/code&gt; type to create less orthodox
		iterations, like a sequence of dates that repeat &lt;i&gt;every other week&lt;/i&gt;:
	&lt;/p&gt;

&lt;pre name="code" class="csharp:nogutter"&gt;var startDate = new DateTime(2010, 1, 1);
var endDate = startDate.AddMonths(3);
var appointmentDates = new Range&amp;lt;DateTime&amp;gt;(startDate, endDate, d =&amp;gt; d.AddDays(14));
appointmentDates.ToArray(); 
// ==&amp;gt; [ 1/1/2010, 1/15/2010, 1/29/2010, 
//          2/12/2010, 2/26/2010, 3/12/2010, 3/26/2010 ]&lt;/pre&gt;

	&lt;p&gt;
		The complete source for these classes and even some unit tests can be found
		&lt;a href="http://github.com/sergiopereira/juicy/blob/master/src/Juicy.Core/Ranges.cs"&gt;here&lt;/a&gt;.
	&lt;/p&gt;

	&lt;h3&gt;Other implementations&lt;/h3&gt;
	&lt;p&gt;
		Of course I was not the first to implement a Range class in C#. A quick search
		yielded at least a 
		&lt;a href="http://www.pluralsight.com/community/blogs/dbox/archive/2005/04/24/7690.aspx"&gt;couple&lt;/a&gt;
		of &lt;a href="http://andyclymer.blogspot.com/2009/03/ruby-ranges-in-net.html"&gt;articles&lt;/a&gt; with
		different approaches to the same goal.
	&lt;/p&gt;
 &lt;p&gt;Another indication that ranges are a popular data structure is the fact that F# has them too.
With luck we will see ranges in a future version of the .NET framework. Oh, 
and since we are wishing for things. let&amp;#39;s hope they introduce range literals in C# at that same time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=54862" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Ruby/default.aspx">Ruby</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Series/default.aspx">Series</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Language-Envy/default.aspx">Language-Envy</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Juicy/default.aspx">Juicy</category></item><item><title>Language Envy - Juicy, a simple webserver</title><link>http://devlicio.us/blogs/sergio_pereira/archive/2009/04/13/language-envy-juicy-a-simple-webserver.aspx</link><pubDate>Tue, 14 Apr 2009 04:02:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:46007</guid><dc:creator>sergiopereira</dc:creator><slash:comments>17</slash:comments><description>	
	&lt;p&gt;
	Many times when I&amp;#39;m writing JavaScript code with  Ajax calls I don&amp;#39;t have an 
	URL to call and get the data yet. Or sometimes I have an URL that gives me
	valid data and I need to test the code against invalid data or some other
	variation that I don&amp;#39;t have handy.
	&lt;/p&gt;

	&lt;h3&gt;WEBrick&lt;/h3&gt;

	&lt;p&gt;
	In these situations, instead of creating a temporary ASP.NET application,
	I learned to love using Ruby and the adorable simplicity of 
	&lt;a href="http://www.webrick.org/"&gt;WEBrick&lt;/a&gt;. Imagine that I needed
	an URL that simulated latency on the server.
	&lt;/p&gt;

&lt;pre name="code" class="ruby"&gt;require &amp;#39;webrick&amp;#39;
include WEBrick

server = HTTPServer.new( :Port =&amp;gt; 2000 )

server.mount_proc(&amp;quot;/getData&amp;quot;){|req, res|
  res.body = &amp;quot;{result: &amp;#39;ok&amp;#39;, accountId: 123}&amp;quot;
  res[&amp;#39;Content-Type&amp;#39;] = &amp;quot;application/json&amp;quot;
  delay = req.query[&amp;#39;delay&amp;#39;] || &amp;quot;0&amp;quot;
  sleep(delay.to_i)
}

trap(&amp;quot;INT&amp;quot;){ server.shutdown }
server.start&lt;/pre&gt;
  
	&lt;p&gt;
	After running the script I can open use the URL &lt;i&gt;http://localhost:2000/getData?delay=5&lt;/i&gt; 
	in my Ajax call and, after a delay of five seconds, the JSON string &lt;i&gt;{result: &amp;#39;ok&amp;#39;, accountId: 123}&lt;/i&gt;
	will be returned. The server will stay there, in a console window, running until I kill it with CTRL+C.
	&lt;/p&gt;

	&lt;p&gt;
	I could add more mount points on that server for as many test URLs as I wish. I find this incredibly practical.
	&lt;/p&gt;

	&lt;h3&gt;I wish there was something as simple in .NET&lt;/h3&gt;

	&lt;p&gt;
	Well, I couldn&amp;#39;t find anything as easy in .NET. The few offerings I found were not as straight forward. Some
	of the alternatives are distributed in .msi files, which makes them less convenient, especially
	for integration testing or JavaScript unit testing (which is my end goal.)
	&lt;/p&gt;

	&lt;h3&gt;Hello Juicy&lt;/h3&gt;
	&lt;p&gt;
	Envy sometimes makes you move forward. I played around with sockets and the HTTP protocol
	and ended up with a library that looks promising. See that same code written using
	the &lt;code&gt;Juicy.DirtCheapDaemons.Http.HttpServer&lt;/code&gt; class, a.k.a. the &lt;b&gt;Juicy Web Server&lt;/b&gt;.
	&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;using System;
using System.IO;
using System.Threading;
using Juicy.DirtCheapDaemons.Http;

  class Program
  {
    static void Main(string[] args)
    {
      HttpServer server = new HttpServer{ PortNumber = 2000 };
      server.Start();

      server.Mount(&amp;quot;/getData&amp;quot;, 
        (req, resp) =&amp;gt;
          {
            resp.Output.Write(&amp;quot;{result: &amp;#39;ok&amp;#39;, accountId: 123}&amp;quot;);
            resp[&amp;quot;Content-Type&amp;quot;] = &amp;quot;application/json&amp;quot;;
            var delay = int.Parse(req.QueryString[&amp;quot;delay&amp;quot;] ?? &amp;quot;0&amp;quot;);
            Thread.Sleep(1000 * delay);
          }
      );

      Console.WriteLine(&amp;quot;Press enter to stop server&amp;quot;);
      Console.ReadLine();
      server.Shutdown();
    }
  }&lt;/pre&gt;

&lt;p&gt;
	There are a few things more that can be done with &lt;code&gt;HttpServer&lt;/code&gt;.
&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;//mounting virtual path with a lambda (shown in previous example)
server.Mount(&amp;quot;/virtual/path&amp;quot;, (req, resp) =&amp;gt; DoSomethingWith(req, resp));

//mounting virtual path to serve static files
server.Mount(&amp;quot;/virtual/path&amp;quot;, @&amp;quot;c:\some\directory&amp;quot;);

//hiding a virtual path
server.Mount(&amp;quot;/virtual/path/subdir&amp;quot;, new ResourceNotFoundHandler());&lt;/pre&gt;

	&lt;p&gt;
	As it is, &lt;code&gt;HttpServer&lt;/code&gt; supports only &lt;i&gt;GET&lt;/i&gt; requests.
	The plan is to add more support (for features that make sense in a
	unit testing scenario) with time.
	&lt;/p&gt;
	&lt;p&gt;

	I obviously don&amp;#39;t envision Juicy becoming a full-fledged web server
	that I can recommend using in a live web site. But I can see it
	being helpful in the scenarios I&amp;#39;ve just described and for testing
	in general.
	&lt;/p&gt;

	&lt;h3&gt;I&amp;#39;m sold. Where can I get it?&lt;/h3&gt;
	&lt;p&gt;
	This project is hosted at GitHub: &lt;a href="http://github.com/sergiopereira/juicy/tree/master"&gt;http://github.com/sergiopereira/juicy/tree/master&lt;/a&gt;,
        where you can download the source directly or even clone the repository :
	&lt;/p&gt;

&lt;pre&gt;c:\projects&amp;gt;git clone git://github.com/sergiopereira/juicy.git&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=46007" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Ruby/default.aspx">Ruby</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Language-Envy/default.aspx">Language-Envy</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Juicy/default.aspx">Juicy</category></item></channel></rss>