Thanks Winmerge!

I have recently been doing some fairly intensive merging and difference checking with Tortoise SVN and subversion and once again find myself thanking my luck stars that Winmerge is installed on my (windows) system just waiting to lend a helping hand!

 

For those who haven’t come across it Winmerge is an open source tool for visually checking file differences, it allows you to move individual changes or differences from one file to the other.  I have found it to be one of the best such tools and it’s free!  It also does a good job of comparing a whole directory tree of files.

 

Tortoise SVN can be configured to use Winmerge when showing differences etc. rather than its default difference visualiser, I find that this makes tortoise much more effective.  Instructions for configuring Winmerge as the default diff viewer can be found in the Tortoise help pages, but to summarise:

 

Right-Click on a folder in explorer, choose the ‘Tortoise SVN’ sub menu and then choose the ‘Settings’ menu option.  Click on the ‘Dif Viewer’ pane, select ‘External’  and  enter the following into the text-box below:

 

C:\Program Files\WinMerge\WinMergeU.exe -e -x -ub -dl %bname -dr %yname %base %mine

 

[the path entered will depend on where you installed Winmerge, for the real low-down on configuring this you had better check the documentation!]

 

Anyway, to sum up, if you find yourself struggling with comparing files or merging changes with subversion then I have found that Winmerge can be of great help, I can’t imagine doing without it anymore!

Captcha Gotcha

Here is an interesting article about the economics of human CAPTCHA ‘solving’ in India. $2 for a 1000 reads, that’s a lot of squinting at wavy, almost impossible to read letters to the dollar, can’t be good for the health! Apparently it’s part of their ‘data processing’ industry!

Technical Support Service for irish Web Designers Launched

We have finally got around to launching our new Technical Support Service for Irish Web Designers – a service designed to take care of the occasional programming and enginering aspects of a Web Designer’s Web development work, leaving them free to concentrate on the actual Web Design and Delivery.

 

This is a local service provided by experts in Ireland – so fast response times with the minimum of  administrative overhead are guaranteed.  This service is designed to be as useful for very small tasks (perhaps just an hour’s duration) as it is to larger tasks.

More details can be found here…

Displaying latest wordpress post on ‘random’ Web Page

Our client’s brief was to display their latest blog post on their main web page in a truncated form, their main web page is not part or their blog.  After a little searching I came across a wordpress plugin called Get-a-Post which seemed to promise just what we required.

 

It will retrieve a post, and sets the post’s data up correctly so that wordpress functions like the_title() and the_content() which must normally be called from within the wordpress loop can be called by php code that is outside of the wordpress loop, this is important to us as we want to display the data on a web page which is not part of the actual wordpress blog – from a ‘random’ page.

 

To install the plugin, just download it, and copy get-a-post.php to the blog’s wp-content/plugins/ directory.  Then goto the wordpress admin/plugins screen and activate it.

 

Once the plugin has been activated, its function get_a_post() can be called from any php script, if called with no parameters the latest post will be made active – perfect!  Before calling it make sure to include/require ‘wp-config.php’.  Here is an example script that gets the latest post’s title and content and packages it up into a div and some spans:

 

<?php require_once(“./wp-config.php”); ?>

<?php get_a_post(); ?>
<div class=”last_post_div”>
<span class=”last_post_title”><?php the_title()?></span> – <span class=”last_post_content”>
<?php echo get_the_content(); ?>
</span>
</div>

Get full Worpress Content from a Post with ‘More’ block

The wordpress php function get_the_content(), when called from within the wordpress ‘loop’ will usually return the current post’s content – ‘very handy’ I hear you cry!  Indeed, but all is not as simple as it may seem.  If there is a <!–More–> block in the post it will not return the full text but instead will return some of the text with a link like ‘read more…’ inserted (what is displayed can be controled by an argument to the function). This is fine _unless_ you actually want to get the post’s full contents!!

 

Finding the answer to this seemingly simple question nearly drove me mad, but I eventually found a solution, there is a global flag called $more, setting this to 1 before calling get_the_content() or the_content() etc. will result in the <!–more–> blocks not being processed and all of the post’s text will be returned!

 

global $more;
$more = 1;
$content = get_the_content();        

Silverlight Pros & Cons

The register have posted an interesting list of Pros and Cons for Microsoft SIlverlight – agree or disagree, love it or hate it – it’s going to be in interesting time!

 

Personally I think that anything that gives Adobe a run for its money can’t be all bad!!

OpenGL 3.0 Specification falls short of expectations

The Register Reports that the recently released OpenGL 3.0 specification has left many GL gaming developers threatening to defect to MS Direct3D, it seems that many of the promised additions and new features did not get a look in to the long awaited (3 years!) 3.0 Spec.  It has been promised that 3.1 will remedy this situation – but this has not yet done much to appease the seething developers!

Implicit Cast of Lambda Expression to Custom Class in C# 3.0?

We hit a little speed bump in our work with parameter-less lambdas – we wanted to be able to assign them to objects of a custom class via an implicit cast, this is very important to our cunning plan…

 

To illustrate this consider the following class that just encapsulates a 0-arity delegate Func<T>:

    public class RuleProp<T>
    {
        private Func<T> _lambda = null;
        public RuleProp(Func<T> l)
        {
            _lambda = l;
        }
        public static implicit operator RuleProp<T>(Func<T> l)
        {
            return new RuleProp<T>(l);
        }
        public RuleProp()
        {
        }
    }

Now, because we have specified an implicit cast for: Func<T> -> RuleProp<T>, it would seem  that we should be able to do the following:

var rp = new RuleProp<string>();
rp = () => "hello"; // Not OK, can't convert lambda to delegate 

But this gives us a compiler error, can’t convert lambda to Fun<T> as it’s not a delegate!!  Looking at the C# specification it seems that if more than one implicit conversion is required, the C# compiler will only do one.  In this case 2 conversions are required:

 

Lambda Expression -> Func<T> -> RuleProp<T>

 

And so the compiler can’t handle it.  If we provide an explicit cast to Func<T> it compiles OK:

 

rp = (Func<string>)(() => "hello");  // Cast works..

 

But this is really ugly as the type must be provided each time.  A workaround was suggested in this article:

 

http://www.interact-sw.co.uk/iangblog/2008/03/17/lambda-inference

 

We provide an ‘adaptor’ function which encourages a cast from Lambda -> Func<T>, and the implicit cast from Func<T> to RuleProp<T> will handle the rest, we can use it like this:

 

rp = Adaptor(() => "hello");  // Now OK!

This is much nicer than the explicit cast as no type needs to be specified!  Here is the function, it just take,s and directly returns a delegate – resulting in just the conversion that we need!

 

public static Func<T> Adaptor<T>(Func<T> f)
{
 return f;
}

 

So although it is not as nice as directly assigning a lambda to RuleProp<T> it’s a grand workaround and will do nicely for the time being – thanks to all at the C# MSDN forum for their help!

Programatically Converting a Literal into an equivalent Lambda Expression in c#3.0

As part of a project that we are working on at the moment  we need a way of converting a literal or constant into a lambda expression which when evaluated will yield the original literal value (in c#).  This must work in the same way for multiple types.

 

For example given the literal string “hello” we need to programmatically create the lambda

 

() => “hello”

 

and for the boolean true we want to create:

 

() => true

 

etc.

 

This can be achieved by creating an appropriate expression tree and then compiling it into a lambda expression, and as we want this to work for different types we wrap all of this into a generic function like the following:

 

        static public Func<T> ToLambda<T>(T val)
        {
            ConstantExpression body = Expression.Constant(val);
            LambdaExpression exp = Expression.Lambda<Func<T>>(body, null);

 

            return (Func<T>)exp.Compile();
        }

 

The function takes the value in question as type T and returns a parameter-less delegate of return type T.  It uses the passed value to create a ConstantExpression, and then uses it to create a LambdaExpression, as the required lambda has no paramters it passes ‘null’ into the constructor’s second parameter.  Now all that is left is to compile the LambdaExpression, this creates and returns the required delegate which is then returned.

 

The following code shows the lambda generator in action:

 

        static public void TestLiteralToLambda()
        {
            // Create the Lambda from literal
            var boolLambda = ToLambda(true);
            // Evaluate the lambda..
            bool boolVal = boolLambda();
            // and, log out some info
            Console.WriteLine(“Lambda evaluates to – {0}”, boolVal);

 

            var stringLambda = ToLambda(“Rainy day in Dublin”);
            string stringVal = stringLambda();
            Console.WriteLine(“Lambda evaluates to – {0}”, stringVal);

 

            var doubleLambda = ToLambda(3.14159);
            double doubleVal = doubleLambda();
            Console.WriteLine(“Lambda evaluates to – {0}”, doubleVal);          ?
        }

 

So far, so good.  Although this might seem a little strange and vaguely useless, what it allows us to do is to treat literals and lambdas of the same type in exactly the same way.

 

Specifically what we want to be able to do is to define ‘super properties’ on objects whose values when queried are either regular values (as normal) or are the result of evaluating a rule, this rule being provided as a lambda expression.  When the property’s value is set the programmer either provides a value (literal or constant) or a rule (lambda expression).

 

Further details on the exact makeup of these ‘super properties’ elude us at the moment, more later…

New ‘Technical Backup’ Service for Web Designers

We are currently designing an new service offering, the details of which will be posted on our web page once they haven been finalised.

 

In summary we will be offering a novel development/engineering service to web design firms.  Often web design firms will not have the need for a full time technical person and then sometimes feel the heat when they need to get some programming work done in a hurry as part of a web project.  In these cases it makes sense to outsource the technical work, however the act of outsourcing can quickly become a headache especially if the individual programming tasks are relatively small but numerous (as they often are).  It would be nice to be able to pick up the phone and ask someone to do some work or solve a problem without any extra administrative overhead.

 

That’s where we come in, our service will allow our clients to block book a set of hours that then can be used or draw down as needed over a period of time, we will invoice at the end of the time period.  Any hours not drawn down will not be charged for!  This approach will hopefully afford our clients a fast and responsive technical service with a the minimum of management overhead.

 

The services offered will include the usual suspects:

 

Server Side – PHP, ASP, Perl, Adobe Flash/ActionScript, SOAP

Client Side – Java Script, VBScript

Database – DB Design & Programming, mySQL, MS SQL Server

XML – XML Format Design, XML Programming

Custom Content Management Systems, Server Configuration, SMS/Text Message Support, etc.