Showing posts with label languages. Show all posts
Showing posts with label languages. Show all posts

Saturday, July 09, 2011

Python/Ruby like generators in Vala

Hello,
the post below is copied straight from here.

Here I'll show a cool snippet code making use Vala async functions and iterators for emulating Python/Ruby generators. Creating a generator is as simple as extending the Generator class and implementing the generate() method.

abstract class Generator {
    private bool consumed;
    private G value;
    private SourceFunc callback;

    public Generator () {
        helper ();
    }

    private async void helper () {
        yield generate ();
        consumed = true;
    }

    protected abstract async void generate ();

    protected async void feed (G value) {
        this.value = value;
        this.callback = feed.callback;
        yield;
    }

    public bool next () {
        return !consumed;
    }

    public G get () {
        var result = value;
        callback ();
        return result;
    }

    public Generator<G> iterator () {
        return this;
    }
}

class IntGenerator : Generator<int> {
    protected override async void generate () {
        for (int i=0; i < 10; i++) {
            yield feed (i);
        }
    }
}

void main () {
    var gen = new IntGenerator ();
    foreach (var i in gen) {
        message ("%d", i);
    }
}

You can find the above code snippet here as well.

Sunday, December 19, 2010

Valag 1.1 released, graph generator for the Vala AST

Hello,
a new version of Valag, a graphviz generator for the Vala language AST, has been released.

Changes since 1.0 version:
  • Add --format and --prefix options.
  • Update to latest libvala-0.12.
  • Bug fixes.
This new version also distributes the xdot.py program as a viewer for the generated graphs.

More information and download at the Valag homepage.

Friday, December 17, 2010

Maja - The Vala to Javascript compiler

Hello,
I've just released the first version of Maja, the Vala to Javascript compiler. The mapping is not quite complete but you can do pretty much everything you could do with javascript directly. There are (still incomplete) bindings for the qooxdoo framework and the demo browser is being ported to vala successfully.
Maja can be used in any environments, not only web browsers.
Programming in Vala saves you from lots of type safety troubles (Javascript), lot of typing (Java) and the syntax is really enjoyable as it is quite close to the Javascript model.

Usage and download at the homepage.

Thursday, February 04, 2010

Vala 0.7.10 released

Hello,
I'm lately following the Vala development and finally we got a new release with plenty of bug fixes and enhancements which makes Vala even more interesting and usable as general purpose language.

Here's the announcement.
I'd like to point you to the new Vala journal, a good resource to stay tuned with Vala changes periodically.

News:
* Support coalescing operator ??.
* Support to_string and bitwise complement with enums.
* Return handler id when connecting signal handlers.
* Support struct comparison.
* Support constructor chaining in structs.
* Enforce protected member restrictions.
* Improve performance of flow analysis.
* Support automatic line continuations in Genie.
* Improvements to the .gir reader and writer.
* Add --enable-mem-profiler commandline option.
* Many bug fixes and binding updates.

Monday, December 14, 2009

Lua for Pythoners - Dictionary

Hello,
as promised this is the second post, this time for dictionaries. The most important thing to notice is that tbl[key]=nil means deleting the entry from the table, while in python dict[key]=None is still a valid entry with None value.

Here's the snippet you can launch in a lua interpreter and this is the catalog of python dictionary examples.

Sunday, December 13, 2009

Lua for Pythoners - Lists

Hello,
lately I'm discovering Lua as general purpose language. Many people don't agree with lua being used as a general purpose language, many others (including a newbie like me) think Lua has such a simple syntax and powerful tables to become a full fledged language.
I use a lot of Python, so I start from here: what Python does that Lua can't do almost in the same way? Read the question as it is, don't read "what Python does that Lua can't do", it's different.

This is the first of several post series I'm writing. I'm trying to "translate" python pills in Lua + Penlight

Please consider that many things can be done really better in Lua. As I said, this is a kind of "translation". You will understand that Lua does better than Python in other things that will not be shown because the examples are made in Python.

Here's the snippet, you can run it with a stand-alone Lua interpreter.

If you know better ways of doing things like in Python please comment. Comments like 'Hey, it's inefficient! This is not the way you do things in Lua' are accepted of course but not related to the post.

Errata:
I've had some important feedback thanks to #lua people in IRC:
- Variables in Lua are globally assigned, so at least in functions you must use local
- Using table.insert in loops is slow, better use tbl[#tbl+1] assignment, and even better remember the next free position tbl[next] = element; next = next + 1