Re: Gee Implementing helper functions



Hi Nicolas,

On 06/18/10 09:55, Nicolas Rassat wrote:
> I am currently playing around with vala and gee and have some questions
> about them.
> 
> I want to implement some helper functions like:
> - void each (each_delegate func)
> - G find (find_delegate func)
> - ArrayList<G> find_all (find_delegate func)
> - map, fold, ...

There is a proposal for some of those. I think those are called
functionnal operators but I may be wrong. See :

https://bugzilla.gnome.org/show_bug.cgi?id=589968

> I tried to do something like this:
> 
> // code
> 
> using Gee;
> 
> public class MyList<G>: ArrayList<G>{
> 	public delegate void EachFunc (G i);
> 	public void each (EachFunc each_func) {
> 		var iter = this.iterator();
> 		iter.first();
> 		do {
> 			each_func (iter.get());
> 		} while (iter.next());
> 	}
> }
> 
> void print( int i) {stdout.printf ("%d\n", i);}
> 
> 
> static int main (string[] args) {
> 
>     var list = new MyList<int> ();
>     list.add (1);
>     list.add (2);
>     list.add (5);
>     list.add (4);
>     list.each((i) => {print( (int) i);});
>     return 0;
> }
> 
> // end code
> 
> Of course this each function could be easily replaced by a foreach
> loop, but its just a test.
> 
> It raised some questions:
> - Is there some function to do what I want in libgee that I have not
>   seen?

None currently.

> - Is this a good approch?
> - Is there any way to do without a wrapper like MyList and use directly
>   the ArrayList?

I guess you could have a Functionnal static class that contains helpers:

Functionnal.each<int>(list, (i) => { print(i); });

But I guess this is not really satisfying.

Another way would be to have new methods defined directly in the class
hierarchy. Maybe adding a reasonnable set of functionnal operators
wouldn't hurt anyone, if it is well designed.

> - Is there any way to do without the (int) casting on line
>   list.each(...)?

Hum there may be a missing generic declaration in your code. I'm not
able to try it out currently but I'll put an eye in it later.

Best regards,
Didier.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]