Languages

vala

Sqlite example

4
Your rating: None Average: 4 (1 vote)
/**
 * Using SQLite in Vala Sample Code
 * Port of an example found on the SQLite site.
 * http://www.sqlite.org/quickstart.html
 */
 
using Sqlite;
 
public class SqliteSample : Object {
 
    public static int callback (int n_columns, string[] values,
                                string[] column_names)
    {
        for (int i = 0; i < n_columns; i++) {
            stdout.printf ("%s = %s\n", column_names[i], values[i]);
        }
        stdout.printf ("\n");
 
        return 0;
    }
 
    public static int main (string[] args) {
        Database db;
        int r

SDL sample

5
Your rating: None Average: 5 (1 vote)
using SDL;
using SDLGraphics;
 
public class SDLSample : Object {
 
    private const int SCREEN_WIDTH = 640;
    private const int SCREEN_HEIGHT = 480;
    private const int SCREEN_BPP = 32;
    private const int DELAY = 10;
 
    private weak SDL.Screen screen;
    private Rand rand;
    private bool done;
 
    public SDLSample () {
        this.rand = new Rand ();
    }
 
    public void run () {
        this.init_video ();
 
        while (!done) {
            this.draw ();
            this.process_events ();
            SDL.Timer.delay (DELAY);
        }
    }

Lua table sample

0
Your rating: None
using Lua;
 
static int main () {
 
    var vm = new LuaVM ();
    vm.open_libs ();
 
    // Create a Lua table with name 'foo'
    vm.new_table ();
    for (int i = 1; i <= 5; i++) {
        vm.push_number (i);         // Push the table index
        vm.push_number (i * 2);     // Push the cell value
        vm.raw_set (-3);            // Stores the pair in the table
    }
    vm.set_global ("foo");
 
    // Ask Lua to run our little script
    vm.do_string ("""
 
        -- Receives a table, returns the sum of its components.
        io.write("The table the script received has

Lua sample

0
Your rating: None
using Lua;
 
static int my_func (LuaVM vm) {
    stdout.printf ("Vala Code From Lua Code! (%f)\n", vm.to_number (1));
    return 1;
}
 
static int main (string[] args) {
 
    string code = """
            print "Lua Code From Vala Code!"
            my_func(33)
        """;
 
    var vm = new LuaVM ();
    vm.open_libs ();
    vm.register ("my_func", my_func);
    vm.do_string (code);
 
    return 0;
}

Character input

0
Your rating: None
public static void main (string[] args) {
    int c;
    stdout.printf ("Type something and press enter. Type '0' to quit\n");
    do {
        c = stdin.getc ();
        if (c > 10) {
            stdout.printf ("%c (%d)\n", c, c);
        }
    } while (c != '0');
}

Readline sample

0
Your rating: None
[CCode (lower_case_cprefix = "", cheader_filename = "readline/readline.h")]
namespace ReadLine {
    [CCode (cname = "readline")]
    public extern string? read_line (string prompt = "");
    public extern void add_history (string line);
}
 
void main () {
    while (true) {
        var name = ReadLine.read_line ("Please enter your name: ");
        if (name != null && name != "") {
            stdout.printf ("Hello, %s\n", name);
            ReadLine.add_history (name);
        }
    }
}

Reading from standard input

0
Your rating: None
/*
 * We use the fact that char[] allocates space
 * and that FileStream.gets takes a char[] but returns a string!
 *
 * Note that stdin.gets uses the "safe" fgets(), not the unsafe gets()
 */
string read_stdin () {
    var input = new StringBuilder ();
    var buffer = new char[1024];
    while (!stdin.eof ()) {
        string read_chunk = stdin.gets (buffer);
        if (read_chunk != null) {
            input.append (read_chunk);
        }
    }
    return input.str;
}
 
int main () {
    string name = read_stdin ();
    stdout.printf ("\n-----\n%s\n", name);
    return 

Nonlinear least-squares fitting sample

0
Your rating: None
using Gsl;
 
public class FitSample
{
        struct Data
        {
                public size_t n;
                public double* y;
                public double* sigma;
        }
 
        static int expb_f (Vector x, void* data, Vector f)
        {
                size_t n = ((Data*)data)->n;
                double* y = ((Data*)data)->y;
                double* sigma = ((Data*)data)->sigma;
                double A = x.get (0);
                double lambda = x.get (1);
                double b = x.get (2);
                size_t i;
                for (i = 0; i 

Multidimensional root-finding sample

0
Your rating: None
using Gsl;
 
public class MultiRootSample : Object
{
        struct RParams
        {
                public double a;
                public double b;
        }
 
        static int rosenbrock_f (Vector x, void* params, Vector f)
        {
                double a = ((RParams*)params)->a;
                double b = ((RParams*)params)->b;
 
                double x0 = x.get (0);
                double x1 = x.get (1);
 
                double y0 = a*(1-x0);
                double y1 = b*(x1-x0*x0);
 
                f.set (

Monte Carlo integration sample

0
Your rating: None
using Gsl;
 
public class MonteSample : Object
{
        static double exact = 1.3932039296856768591842462603255;
 
        static double g (double* k, size_t dim, void* params)
        {
                double A = 1.0 / (Math.PI * Math.PI * Math.PI);
                return A / (1.0 - Math.cos(k[0]) * Math.cos(k[1]) * Math.cos(k[2]));
        }
 
        static void display_results (string title, double result, double error)
        {
                stdout.printf ("%s ==================\n", title);
                stdout.printf ("result = % .6f\n", result);
 
Syndicate content