I understand that the sublist method works similar to slicing, but what are some other ways i can get similar outputs?
For example
List = [1,3,5,7,9]
Python code
ListSliced = List[1:]
Using sublist in dart
ListSliced = List.sublist(1);
Is there a equivalent of List[1:]
in DART??
JavaScript
x
13
13
1
Code written in dart.
2
3
List<dynamic>' is not a subtype of type 'bool'
4
5
branches(tree) {
6
return tree.sublist(1);
7
}
8
9
isLeaf(tree) {
10
return (!branches(tree));
11
}
12
13
JavaScript
1
9
1
Equivalent code in python.
2
3
def branches(tree):
4
return tree[1:]
5
6
def is_leaf(tree):
7
return not branches(tree)
8
9
Any ideas on whats causing this error
Full code for the tree program.
JavaScript
1
37
37
1
bool isTree(tree) {
2
if ((tree is! List) | (tree.length < 1)) {
3
return false;
4
}
5
for (final branch in branches(tree)) {
6
if (!isTree(branch)) {
7
return false;
8
}
9
}
10
return true;
11
}
12
13
branches(tree) {
14
return tree.sublist(1);
15
}
16
17
Object label(tree) {
18
return tree[0];
19
}
20
21
List tree(rootLabel, [List branches = const []]) {
22
for (final branch in branches) {
23
assert(isTree(branch));
24
}
25
return ([rootLabel] + branches);
26
}
27
28
isLeaf(tree) {
29
return (!branches(tree));
30
}
31
32
var t = tree('hey', [
33
tree('hello'),
34
tree('hum', [tree('there'), tree('hey')])
35
]);
36
37
Advertisement
Answer
Another way is to use skip
For example:
JavaScript
1
3
1
List l = [1,3,5,7,9];
2
print(l.skip(2)); //prints (5,7,9)
3