Wednesday, March 14, 2007

Problem

You need to find the numeric code for a character (either ASCII or Unicode).

Solution

namespace Cookbook1_2;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
begin
  var ch      : char    := 'A';
  var chAsInt : Integer := Integer(ch);
  Console.WriteLine( chAsInt );
end;

end.

The ASCII or Unicode value of character is obtained by casting the character to an Integer. Also note that I have forgo Chromes type inference and explicitly declared the type for each variable.

 

posted on Wednesday, March 14, 2007 8:58:53 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback

Problem

Quite a common task in programming is the need to find the ASCII value of a character. The code below demonstrates how to do this in D.

Solution

int main(char[][] args) {

    auto ch        = 'A';
    int  charAsInt = ch;
    printf( "%d", charAsInt );

    return 0;
}

In D, getting the ASCII value of a character a trivial task. Simply assign the character to an integer and D will “promote” the character to reveal its ASCII value.

Some other points to note about the above code are:

  • char values in D are UTF-8 by default; if you want UTF-16 or UTF-32 characters then use the wchar or dchar types respectively.
  • Also note the use of the auto keyword when I declared the character variable, ch. The auto keyword gives you access to D implicit type inference features.
Tags: ,
posted on Wednesday, March 14, 2007 8:02:01 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Monday, March 05, 2007

Problem

You want to break up a string into its individual tokens or words

Solution

I’ll present the solution in two forms; the first will use “traditional” Object Pascal idiom, the second will make the most of Chrome’s features where appropriate. Both solutions will use the FCL’s string.Split() method.

Object Pascal Version

namespace Cookbook1_1a;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
const
  Space : char = ' ';
var
  userRec    : String;
  attributes : array of String;
begin
  userRec    := '01 Barry Carr 17/04/1964
barry@notarealdomain.com';
  attributes := userRec.Split( Space );
 
  for each token : String in attributes do
      Console.WriteLine( token );
end;

end.

Chrome Version

namespace Cookbook1_1b;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
const
  Space : char = ' ';
begin
  var userRec    := '01 Barry Carr 17/04/1964
barry@notarealdomain.com';
  var attributes := userRec.Split( Space );
 
  for each token in attributes do
      Console.WriteLine( token );
end;

end.

Notice how the Chrome version doesn’t have any type declarations, Chrome’s type inference is doing all the work of figuring out what the types are at compile time. One other point to note is that Chrome allows the developer to declare their variables in-line, where they are needed and does not require all local variable to be declared at the top of the method, procedure or function as classic object pascal compilers do.

Finally, the loop at the bottom is there so that I can see that the string has been split as I’d expect. A unit test would be better, however.

posted on Monday, March 05, 2007 8:50:25 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback

The sample was compiled using the latest version of D, 1.007 at the time of writing. You can get the latest version of the D compiler here.

Problem

You want to break up a string into its individual tokens or words 

Solution

Use the split() function from std.string, like so:

import std.stdio;
import std.string;

void main(char[][] args)  {
     char[]   record     = "01 Barry Carr 17/04/1964 barry@notarealdomain.com";
     char[][] attributes = split(record);

     foreach( char[] attribute; attributes )
        writefln( attribute );
 }

There is on overloaded version of split() that allows you specify what characters are to be used as delimiters.

The foreach statement at the end of the code above is there so that the tokens can be displayed on the console.

Tags: ,
posted on Monday, March 05, 2007 7:02:39 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Sunday, March 04, 2007

Taking a leaf out of Gary Short’s (cook)book and blatantly stealing his idea, I’m going to follow his example and start some programming cookbooks of my own. Like Gary, my cookbooks will be based upon the items featured in the Python Cookbook. Gary is currently doing a cookbook for Smalltalk, one of my favourite programming languages, so I’ll do cookbooks for D, Chrome and possibly F# and OCaml. I’m interested in learning all of these languages so undertaking these cookbooks should help me to gain a wider understanding of them and also enable me to share my experiences with a wider audience.

The Languages

D is the creation of Walter Bright, the man that developed the worlds first C++ compiler (before Walter, all C++ “compilers” were C preprocessors). D’s aim is to overcome some of the short-coming of C++. D offers virtually all the features you’d expect from a modern language, including: Templates (generics), Delegates, Interfaces, Mixins, (optional) Garbage Collection, Type Inference, Design by Contract, Tail Recursion and much, much more. The D compiler is free to down load and use and is available for Win32 and Linux.

Chrome is an Object Pascal implementation targeted at the .NET and Mono platforms. It’s being developed by RemObjects. Chrome offers all the features of C# 2.0 as well as Design by Contract, Type Inference and Virtual Constructors. It also improves on “classic” Object Pascal implementations by improving the with statement and by adding the ability to declare variables in-line, like the C family of languages. Chrome is a commercial product but RemObjects do provide a free, command line compiler.

OCaml is an Object-oriented functional/imperative language hybrid developed and maintained by INRIA, France's national research institute for computer science. OCaml can be interpreted or compiled very efficiently into native code. Like all the languages mentioned so far, OCaml supports Type Inference, Garbage Collection and Generics. Also, as you would expect from a functional language, OCaml supports: Tail Recursion, Pattern-matching and Currying.

F# is an experimental functional language developed by Microsoft and, as you’d expect, F# is targeted at the .NET platform. F# is based on OCaml.

Tags: , , ,
posted on Sunday, March 04, 2007 8:06:44 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback