cdadar
 

I have gone through a real pain to figure out how to print an exact size in Mac OS X. Follow the steps below and maybe, just maybe, it'll work for you. I used the following steps to print the correct size in 8.5 x 11 paper


Open the image in Preview, it might help if you image size is actually 8.5 x 11.

Go to Print...

Select to Paper Size and Manage Custom Sizes...

Create a new size with width 8.5 and height 11 inches. Make sure all the margins are set to 0.

Click OK and select that template (a couple of times my settings would change automatically to something else, so you better keep checking that the same setting is selected)

In Preview Option,
Unselect Center Image
Scale -> Fill Entire Paper

In Paper Handling Option,
Select Scale to fit paper size, and also make sure that Destination Paper Size is the new template that you just created.
Unselect Scale down only.

That's it!! It worked for me, hopefully it'll work for you too...

 
Callbreak 03/01/2009
 

I have been working on a card game called Callbreak. You can find it at game.cdadar.com.


The game is still in early stages, please check it out and leave some feedback below. 


Thanks

 
TimeTrack 02/21/2009
 

timeTrack is a simple organizer to track your time. Just type in whatever you are doing right now and it will do the rest for you.

It can also work as a diary and a bookmark tool, something to record your random thoughts.

This is a work in progress and I would really appreciate some feedback. Please leave a comment below.

Installation

Installation should be a breeze....

Step 1:
Download the latest version of PLT Scheme.

Step 2
Go to Github, click on download, and then the zip file.

Step 3
Unzip it and run

/path/to/mzscheme -r serve.scm

For windows users:

if you installed PLT Scheme in the default location,

click the Start menu
click on Run and type cmd (win xp) or  type cmd (vista and over)

change directory to the folder where you unzipped the program using cd command
eg.,

cd Desktop/alokthapa<tab>

just type <tab> and press enter, repeat the same thing again

cd alokthapa<tab> and enter
 
also type

dir

you should be able to see files like app.scm and main.scm

and then type

"C:\Program Files\PLT\MzScheme.exe" -r serve.scm

NOTE: if you're running it for the first time, it will take some time to download some dependencies.

Step 4

After you see something like

Server is ready at http://localhost:8765/ (ctrl-c to stop it).

go to localhost:8765

If you encounter any problem during the installation process, send me a feedback and I'll get back to you.

 
 


This is a useful control that works like a masterpage but is more lightweight. You can use this control as a wrapper for any html or ASP.NET controls.

<%@ Control Language='C#' AutoEventWireup='true'  CodeBehind='WrapperPanel.ascx.cs' Inherits='Controls.WrapperPanel' %>
<div class='wrapper'>
  <div>
 <asp:Label ID='lbHeader' runat='server' />
 </div>
  <div>
 <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</div>
</div>

This is the code file for the control.


[PersistChildren(true)]
public partial class WrapperPanel : UserControl
{
  public string HeaderText { set { lbHeader.Text = value; } }

  private ITemplate _itemTemplate;
 [TemplateInstance(TemplateInstance.Single)]  [PersistenceMode(PersistenceMode.InnerProperty)]
 public ITemplate ItemTemplate  {
  get { return _itemTemplate; }
  set { _itemTemplate = value; }
}
 protected override void OnInit(EventArgs e)  {
  base.OnInit(e);
  itemPlaceHolder.Controls.Clear();
  if (ItemTemplate == null) return;
  ItemTemplate.InstantiateIn(itemPlaceHolder);
}
}

 You would use it as follows:

<AR:wrapperpanel runat="server" HeaderText="Some Control" id-"wrapperpanel"> <ItemTemplate> Your content here </ItemTemplate> </AR:wrapperpanel>

The result would be:

<div class='wrapper'><div> <asp:Label ID='lbHeader' runat='server' /> </div> <div> Your content here </div>  </div>

Now you can focus on what’s important and not worry about the wrapping html code.

Also using the TemplateInstance.Single attribute, you can access controls within your wrapper just as you would without it. If you didn’t use it, you willl have to access them using the FindControl function on wrapper control.

 
 

One of the cooler features of c# 3.0 are expression trees. A quick intro can be found here .Now the fact that you can compile a function at runtime means that you can build a dynamic language on top of Linq Expressions.

Here's a simple dynamic "XML Language" written using Expression Trees. Basically, the idea here is to have a one-to-one mapping between your XML expression and the Linq Expressions.

For example,

<Expression FunctionName="Constant" Type="System.String" Value="someval" />

would create an Linq Expression of type

Expression.Constant("someval",Type.GetType(string));

Here's a related test for the mapping:

        [TestMethod]
        public void GetConstantTest()
        {
            XMLExpressionProvider expr = new XMLExpressionProvider();
            XElement elem = expr.BuildConstant(typeof(int).ToString(), "1");
            Assert.AreEqual("1", expr.GetConstant(elem));
        }


and related implementations:

   public XElement BuildConstant(string type, string value)
        {
            XElement elem = new XElement("Expression");
            elem.Add(new XAttribute("FunctionName", "Constant"));
            elem.Add(new XAttribute("Type", type));
            elem.Add(new XAttribute("Value", value));

            return elem;
        }


        public object GetConstant(XElement elem)
        {
            return elem.Attribute("Value").Value;
        }


Binary operations can also be mapped similarly.

Here we're creating a new Add Expression with the constants 1 and 2. This actually creates a xml representation for the Add Operation.


        [TestMethod]
        public void BuildExpressionTest()
        {
            XMLExpressionProvider expr = new XMLExpressionProvider();
            XElement xelm = expr.BuildExpression("Add", new[] {
                                                            expr.BuildConstant(typeof(int).ToString(),"1"),
                                                            expr.BuildConstant(typeof(int).ToString(),"2")});
            Assert.AreEqual(xelm.ToString(SaveOptions.DisableFormatting),
                "<Expression FunctionName=\"Add\"><Expression FunctionName=\"Constant\" Type=\"System.Int32\" Value=\"1\" /><Expression FunctionName=\"Constant\" Type=\"System.Int32\" Value=\"2\" /></Expression>");
        }

Basically the XML Represenation would look like

    <Expression FunctionName="Add">
        <Expression FunctionName="Constant" Type="System.Int32" Value="1" />
        <Expression FunctionName="Constant" Type="System.Int32" Value="2" />
    </Expression>

In the test below, we're actually creating a XML Representation for a GreaterThan expression and executing it with different parameters.

         [TestMethod]
        public void ExpressionTest()
        {
            XMLExpressionProvider expr = new XMLExpressionProvider();
            ParameterExpression p = Expression.Parameter(typeof(int), "x");
            XElement elem = expr.BuildExpression("GreaterThan", new[] { expr.BuildConstant(typeof(int).ToString(), "5"), expr.BuildParameter(0) });
            Expression expression= expr.ExpressionFromXElement(elem, new [] {p});
            Expression<Func<int, bool>> e = Expression.Lambda<Func<int, bool>>(expression,new []{p});
            Console.WriteLine(e.ToString());
            Assert.IsTrue(e.Compile().Invoke(4));
            Assert.IsFalse(e.Compile().Invoke(7));
        }

The heart of the implementation lies in the method ExpressionFromXElement which is implemented as

        public Expression ExpressionFromXElement(XElement elem, ParameterExpression [] p)
        {
      
            if (IsExpression(elem))
            {
                
                switch (GetFunctionName(elem))
                {
                    case "Or":
                        return Expression.Or(ExpressionFromXElement(GetArguments(elem)[0], p), ExpressionFromXElement(GetArguments(elem)[1],p));
                    case "And":
                        return Expression.And(ExpressionFromXElement(GetArguments(elem)[0], p), ExpressionFromXElement(GetArguments(elem)[1], p));
                    case "LessThan":
                        return Expression.LessThan(ExpressionFromXElement(GetArguments(elem)[0], p), ExpressionFromXElement(GetArguments(elem)[1], p));
                    case "GreaterThan":
                        return Expression.GreaterThan(ExpressionFromXElement(GetArguments(elem)[0], p), ExpressionFromXElement(GetArguments(elem)[1], p));
                    case "Equal":
                        return Expression.Equal(ExpressionFromXElement(GetArguments(elem)[0], p), ExpressionFromXElement(GetArguments(elem)[1], p));
                    case "Condition":
                        return Expression.Condition(ExpressionFromXElement(GetArguments(elem)[0], p), ExpressionFromXElement(GetArguments(elem)[1], p), ExpressionFromXElement(GetArguments(elem)[2], p));
                    case "Parameter":
                        return p[Convert.ToInt32(elem.Attribute("Index").Value)];
                    case "Constant":
                        return Expression.Constant(((object)GetConstant(elem)),Type.GetType(elem.Attribute("Type").Value));
                    case "Member":
                        return Expression.PropertyOrField(ExpressionFromXElement(GetArguments(elem)[0], p), elem.Attribute("FieldName").Value);
                    case "IsEmptyOrNull":
                        return Expression.Or(
                                    Expression.Equal(ExpressionFromXElement(GetArguments(elem)[0], p), Expression.Constant(String.Empty)),
                                    Expression.Equal(ExpressionFromXElement(GetArguments(elem)[0], p), Expression.Constant(null)));
                    default: throw new Exception();
                }
            }
            return null;
        }


You can also do member accesses:

        [TestMethod]
        public void MemberAccessTest()
        {
            XMLExpressionProvider expr = new XMLExpressionProvider();
            ParameterExpression p = Expression.Parameter(typeof(string), "str");
            XElement elem = expr.BuildMemberExpr("Length", new []{expr.BuildParameter(0)});
            Expression expression = expr.ExpressionFromXElement(elem, new[] { p });
            var e = Expression.Lambda<Func<string, int>>(expression, new[] { p });
            Console.WriteLine(e.ToString());
            Assert.AreEqual(6, e.Compile().Invoke("string"));
            Assert.AreEqual(3, e.Compile().Invoke("str"));
        }

        [TestMethod]
        public void BuildMemberTest()
        {
            XMLExpressionProvider expr = new XMLExpressionProvider();
            Assert.AreEqual("<Expression FunctionName=\"Member\" FieldName=\"MaxValue\"><Expression FunctionName=\"Parameter\" Index=\"0\" /></Expression>", expr.BuildMemberExpr("MaxValue", new []{expr.BuildParameter(0)} ).ToString(SaveOptions.DisableFormatting));
        }


The examples given here should give you an idea of how it works and the possiblities that exist.

 
 

There are a few suggested ways to handle primitive obsession. Personally, I would prefer:

public class Validated
{
    private string _value;
    private Regex _regex = null;

    public Validated(string pattern)
    {
        _regex = new Regex(pattern);
    }

    public bool IsValid(string value)
    {
        if (_regex.IsMatch(value))
            return true;
        return false;
    }

    public string Value
    {
        get { return _value; }
        set
        {
            if (IsValid(value))
                _value = value;
            else throw new FormatException("Invalid format.");
        }
    }
}

And use it as:

    Validated zip = new Validated(@"/^\d{5}([\-]\d{4})?$/");   
    Validated email = new Validated(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
    zip.Value = "74445";
    email.Value = "example@yahoo.com";


I can live without have a custom error message for each of my message( there's stack trace for that) and having the explicit operators.
Now if you think that this is not flexible enough to accommodate all cases, try this,

public class Validated
{
    private string _value;
    private Func<string, bool> IsValid;


    public Validated(Func<string, bool> isValid)
    {
        this.IsValid = isValid;
    }

    public string Value
    {
        get { return _value; }
        set
        {
            if (IsValid.Invoke(value))
                _value = value;
            else throw new FormatException("Invalid format.");
        }
    }
}

And use it as:

    Validated zip = new Validated((z) => new Regex(@"/^\d{5}([\-]\d{4})?$/").IsMatch(z));
        Validated email = new Validated((e) => new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$").IsMatch(e));
        zip.Value = "74445";
        email.Value = "example@yahoo.com";

 
Progress Bar 04/16/2008
 

Here's a simple function for a progress bar

(def disp-progress (start end)
      (for i start end
       (if (is (mod i (/ (- end start) 10) ) 0)
           (prn (to-nearest (* 10 (/ (- i start) (/ (- end start) 10))) 10)))))

 
Post With GWT 08/16/2007
 

Here’s a way to do post using GWT

RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, "http://localhost:9098/json/json/jsontest");
rb.setHeader("Content-Type","application/x-www-form-urlencoded");


try {
rb.sendRequest("jsontext="+hellothere.toString(), new DateCallbackHandler());


}
catch (RequestException e) {
Window.alert(e.toString());
//GWT.log("Could not send search request", e);
}


By the way, its calling a controller of a grails project.

Here’s how it handles it


def jsontest = {

String _json = (params.jsontext)?params.jsontext:’{"JSON":"servertext"}’
// String _xyz = request.inputStream.readLine();


//render _xyz

JSONObject obj = new JSONObject(_json)
render obj.getString("JSON")
}

 
 

My implementation for factorial in Lisp. This is a response to a post found here.

Also, it is tail recursive.

(defun fact (n)
     (labels ((rec (n acc)

         (if (<= n 1)

             acc

             (rec (1- n ) (* acc n)))))

     (rec n 1)))


ps: Factorial in Haskell seems easier though

fact 0 = 1
fact n = n * fact (n-1)


or

fact = product . enumFromTo 1

 
 

Here’s my version of palindrome for lisp problems 99 no 06 found here

(defun last1 (lst)

  (car (last lst)))

(defun palin1 (lst)

       (if (equal nil lst)

       t

       (let ((start (first lst)) (finish (last1 lst)))

         (if (equal start finish)

         (palin1 (rest (butlast lst)))

         nil))))