Compare/Slice/Get/Set to Container
Hello, it would be nice to add these functions to List so that vala is even easier to use! I tried contains, but I did not succeed
they are just ideas, maybe it is a very bad idea
in any case I find that slice is really a good idea!
public List<G> slice (long start, long end)
{
unowned List<G>? l = this;
var tmp = new List<G>();
if (start < l.length())
{
while(start != end && start != l.length()){
tmp.append(l.nth_data((uint)start));
start++;
}
}
return (tmp);
}
public G get(int index){
unowned List<G>? l = this;
return l.nth_data(index);
}
public void set(int index, G item){
unowned List<G>? l = this;
uint i = 0;
if (index < l.length())
{
while (i < index && l != null){
l = l.next;
i++;
}
l.data = item;
}
}
public bool contains(G needle){
foreach(var i in this){
if (needle == i)
return true;
}
return false;
}
public static List<G> from_tab(G []tab){
var l = new List<G>();
foreach (G i in tab){
l.append(i);
}
return l;
}
Example output: 21
void main()
{
var l = new List<int>();
l.append(5);
l.append(3);
l.append(2);
l.append(1);
l.append(8);
foreach(var i in l[2:4]){
print(@"$i");
}
}
Edited by Hydral Nathan Jordan