Vala - gregorymorrison/euler1 GitHub Wiki

Vala, introduced in 2006, is a language designed to facilitate Linux GUI development. It's a modernized C with the GObject system built in. Here's a simple iterative version of Euler1:

// Euler1 in Vala

int euler1(int size) {
    int result = 0;
    for (int i = 0; i < size; i++) {
        if (i%3==0 || i%5==0) {
            result += i;
        }
    }    
    return result;
}

int main() {
    print("%i\n", euler1(1000));
    return 0;
}

To grab your own copy for Fedora, yum install vala, vala-devel, and gtk3-devel. Vala comes with both a compiler and an interpreter. To use the compiler, call valac on your source, then execute the generated object file:

$ valac euler1.vala
$ ./euler1
233168
$

Or to use the interpreter, simply call vala with your source as an argument:

$ vala euler1.vala
233168
$

But Vala's about making desktop apps with a UI - what's that like? Here's a trivial example using the GTK toolkit that simply puts the answer in a textbox:

// Euler1 in Vala

using Gtk;

int euler1(int size) {
    int result = 0;
    for (int i = 0; i < size; i++) {
        if (i%3==0 || i%5==0) {
            result += i;
        }
    }    
    return result;
}

int main (string[] args) {
    Gtk.init (ref args);

    var window = new Window ();
    window.title = "Euler1 in Vala";
    window.border_width = 10;
    window.window_position = WindowPosition.CENTER;
    window.set_default_size (350, 70);
    var entry = new Entry ();
    entry.text = euler1(1000).to_string();
    window.add (entry);
    window.destroy.connect (Gtk.main_quit);
    window.show_all ();

    Gtk.main ();
    return 0;
}

To compile this code, include the GTK library:

$ valac --pkg gtk+-3.0 euler1.vala
$ ./euler1

And here's the result: