Adding a method for computing Cartesian Product to Groovy’s Collection(s)

groovy-cartesian-productIn these days I’m using the Groovy programming language very often, I found this language very intuitive and expressive. I try to use, when it is appropriate and convenient , Functional programming style and methods.

One of the key elements of functional programming paradigm (opposite to the imperative paradigm) is “thinking in  space rather than thinking in time”, this translates in a extensive usage of collections and constructs for creating a collection based on existing collections. The most common collection used is the list, the syntactic construct for creating a list based on existing lists is named List comprehension.

I think that the list, or more generic collection, comprehension in Groovy is very powerful (Groovy Collection API), and in my everyday usage I found that it has everything that I need to express the algorithm that I implement in terms of Collection comprehension. By the way, more that once I needed to obtain the Cartesian product of two collections, so I thought it is nice to have a method in Collection for computing the Cartesian product.

Cartesian product

The Cartesian product is a mathematical operation which returns a set (or product set) from multiple sets. That is, for sets A and B, the Cartesian product A × B is the set of all ordered pairs (a,b) where a ∈ A and b ∈ B:

f(A, B) = \bigcup_{a\in A}\bigcup_{b\in B} (a, b).

Read more of this post

Learning Functional Programming: a K-Means implementation in Groovy

kmeansSince few days I started studying the Functional Programming paradigm, so I decide practicing  by implementing a commonly used algorithm. Although Groovy it isn’t strictly a functional programming language, it has all the characters of a functional programming language.

K-Means

K-Means is a method of cluster analysis which aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean.

Read more of this post

CRUX PPC 2.5 Release Candidate 4

It’s time for a new (and we hope the last one) release candidate for CRUX PPC 2.5. This release candidate implements all the changes introduced in CRUX PPC Release Candidate 3.

From this release candidate we provide a full support for IBM pSeries machines (since we bought a second hand IBM Intellistation 2-way POWER4+ workstation).

Please test it:

The 32bit version:
ftp://ftp.cruxppc.org/cruxppc/test/crux-ppc-2.5rc4.iso
ftp://ftp.cruxppc.org

The 64bit version:
ftp://ftp.cruxppc.org/cruxppc/test/crux-ppc64-2.5rc4.iso
ftp://ftp.cruxppc.org/cruxppc/test/crux-ppc64-2.5rc4.iso.md5

Please give us feedback on the CRUX PPC forum: http://www.cruxppc.org/forum/viewtopic.php?f=2&t=220

Loading font face from file using Mono and Cairo

I was trying to build up a little application for previewing and installing FreeType fonts using Gtk#, but I didn’t found any existing API for loading the font face from a file (.ttf) in Mono.Cairo.

In Mono.Cairo there is the class FontFace that represents a FontFace, this class has only a constructor that accept an IntPtr (handle) as unique arguments, this handle should refer to an allocated and initialized cairo’s cairo_font_face_t.

The only chances that I had for obtaining the cairo_font_face_t handler for the file was to relay to the C API of Cairo and FreeType. The result is a new class, FreeTypeFontFace derived from Cairo.FontFace:

using System;
using System.Runtime.InteropServices;
using Cairo;


public class FreeTypeInitException : Exception
{
    public FreeTypeInitException() : base("Can't initialize freetype environment.")
    {
    }
}

public class CreateFaceException : Exception
{
    public CreateFaceException(string filename) : base("Can't create the face for file: " + filename + ".")
    {
    }
}

public class LoadFaceException : Exception
{
    public LoadFaceException(string filename) : base("Can't load the face for file: " + filename + ".")
    {
    }
}

public class FreeTypeFontFace : FontFace
{
    private static bool initialized = false;
    private static IntPtr ft_lib;
    private IntPtr ft_face;

    private FreeTypeFontFace(IntPtr handler, IntPtr ft_face):base(handler)
    {
        this.ft_face = ft_face;
    }

    public void Dispose()
    {
        cairo_font_face_destroy (Handle);
        FT_Done_Face (ft_face);
        ((IDisposable) this).Dispose ();
    }

    public static FreeTypeFontFace Create(string filename, int faceindex, int loadoptions)
    {
        if(!initialized)
            initialize();

        IntPtr ft_face;
        if(FT_New_Face (ft_lib, filename, faceindex, out ft_face) != 0)
            throw new LoadFaceException(filename);

        IntPtr handler = cairo_ft_font_face_create_for_ft_face (ft_face, loadoptions);
        if(cairo_font_face_status(handler) != 0)
            throw new CreateFaceException(filename);

        return new FreeTypeFontFace(handler, ft_face);
    }

    private static void initialize() {
        if(FT_Init_FreeType (out ft_lib) != 0)
            throw new FreeTypeInitException();
        initialized = true;
    }

    [DllImport ("libfreetype.so.6")]
    private static extern int FT_Init_FreeType (out IntPtr ft_lib);

    [DllImport ("libfreetype.so.6")]
    private static extern int FT_New_Face (IntPtr ft_lib, string filename, int faceindex, out IntPtr ft_face);

    [DllImport ("libfreetype.so.6")]
    private static extern int FT_Done_Face (IntPtr ft_face);

    [DllImport ("libcairo.so.2")]
    private static extern IntPtr cairo_ft_font_face_create_for_ft_face (IntPtr ft_face, int loadoptions);

    [DllImport ("libcairo.so.2")]
    private static extern int cairo_font_face_status (IntPtr cr_face);

    [DllImport ("libcairo.so.2")]
    private static extern int cairo_font_face_destroy (IntPtr cr_face);
}

The constructor is private: the FreeTypeFontFace instances should be create using the static method Create because we have to provide the correct cairo_font_face_t handler to the base constructor.

The steps for initialize the cairo_font_face_t structure are:

  1. Initialize the freetype environment if it isn’t already been initialized.
  2. Load the FreeType face from the file using FT_New_Face() (from the FreeType library).
  3. Build the cairo_font_face_t structure from the FreeType face using cairo_ft_font_face_create_for_ft_face().

As you can see, the Create method can throw some exceptions.

When the object isn’t needed anymore the programmer should invoke the Dispose method (as happens with other Cairo related objects).

To obtain a .net library, named FreeTypeFontFace.dll, for using the class save the above file as “FreeTypeFontFace.cs” and build it using:

mcs -pkg:mono-cairo -target:library FreeTypeFontFace.cs

Follow a sample code (written in boo) using the FreeTypeFontFace for displaying a FreeType Font preview:

FontPreview

import System
import FreeTypeFontFace from "FreeTypeFontFace.dll"


import Gtk from "gtk-sharp"
import Gdk from "gdk-sharp"
import Cairo from "Mono.Cairo"

class FontPreview(DrawingArea):
	private fontfile as string
	private static sizes = [6, 8, 10, 11, 12, 14, 16, 18, 20, 22, 24]
	private static lorem = "Loading font face from file using Mono and Cairo"

	def constructor(fontfile as string):
		self.ModifyBg(StateType.Normal, Gdk.Color(0xff,0xff,0xff))
		self.fontfile = fontfile

	def OnExposeEvent(args as Gdk.EventExpose):
		g = Gdk.CairoHelper.Create (args.Window)
		font = FreeTypeFontFace.Create(self.fontfile, 0, 0)
		g.ContextFontFace = font
		p as int = 10
		for i as int in sizes:
			g.SetFontSize(i)
			p = p + i + 2
			g.MoveTo(10, p)
			g.ShowText(lorem)
		te = g.TextExtents(lorem);
		WidthRequest = te.Width + 20;
		HeightRequest = p + te.Height + 10;
		font.Dispose()
		ig as IDisposable = g
		ig.Dispose()

Application.Init()
w = Gtk.Window("FontPreview")
w.Add(FontPreview("/usr/share/fonts/dejavu/DejaVuSansMono-Oblique.ttf"))
w.DeleteEvent += {Application.Quit()}
w.ShowAll()
Application.Run()