Suppose that we have the following DOM structures:
[lang=jade]
fieldset.input-hub
div.input-element(data-name="a" data-value="1")
div.input-element(data-name="a" data-value="2")
div.input-element(data-name="b" data-value="3")
fieldset.input-hub(data-name="c")
div.input-element(data-name="d" data-value="4")
div.input-element(data-name="undefined" data-value="5")
/* Invalid input should not be parsed */
div.input-element(data-value="6")
...
How would you parse the data-value as the following result:
{a: [1, 2], b: 3, c: {d: 4, undefined: 5}}
Note that the fieldset will appear recursively
Related
Why can't I assign a shuffled array to a new array?
For example, this is my list:
List nums = [1, 2, 3];
List newlist=nums.shuffle();
However, this does not work for me.
Because shuffle() on List does not return anything (which is indicated by void which means the method does not return anything usable) but are instead shuffling the list you are calling the method on.
void shuffle([Random? random])
Shuffles the elements of this list randomly.
https://api.dart.dev/stable/2.15.1/dart-core/List/shuffle.html
If you want to output your nums list after it have been shuffled, you just need to print nums like this after the nums.shuffle() call:
void main() {
final nums = [1, 2, 3];
nums.shuffle();
print(nums); // [2, 3, 1]
}
If you want to have a new list there is shuffled and based on another list, you can do something like this (the .. is called a cascade call, you can read more about it here: https://dart.dev/guides/language/language-tour#cascade-notation):
void main() {
final nums = [1, 2, 3];
final shuffeledNums = nums.toList()..shuffle();
print(nums); // [1, 2, 3]
print(shuffeledNums); // [2, 1, 3]
}
Which is the same as this without the use of cascade:
void main() {
final nums = [1, 2, 3];
final shuffeledNums = nums.toList();
shuffeledNums.shuffle();
print(nums); // [1, 2, 3]
print(shuffeledNums); // [2, 1, 3]
}
I'm having issues attempting to remove duplicate lists within a list. For example, using the list below, I need to remove one of the [2, 9] elements from the list.
List listOfLists = [[1, 5], [2, 9], [10, 12], [-1, 4], [2, 9]];
I have tried using toSet, but it only seems to work with a list of int or strings:
List uniqueLists = listOfLists.toSet().toList();
I have been stuck on this all morning, I'm sure its something simple, but the solution isn't coming to me. Any help would be appreciated.
The issue is that == for lists is reference equality, not based on the value of the lists.
for example if you run this program:
void main() {
print([2, 9] == [2, 9]);
}
it will print false.
You can create a HashSet (or LinkedHashSet if you want to preserve the order) with custom equals and hashcode functions.
import 'dart:collection';
import 'package:collection/collection.dart';
void main() {
List<List<int>> listOfLists = [
[1, 5],
[2, 9],
[10, 12],
[-1, 4],
[2, 9],
];
Set<List<int>> uniqueLists = HashSet<List<int>>(
equals: const ListEquality().equals,
hashCode: const ListEquality().hash,
)..addAll(listOfLists);
print(uniqueLists);
}
This solution uses the collection package for the ListEquality class.
This solution traverses the list backwards, and removes the last item when a duplicate is found. To compare lists, _listsAreEqual is used which is taken from this answer.
Less elegant compared to other solutions, but it does not require any additional packages.
bool _listsAreEqual(list1, list2) {
var i=-1;
return list1.every((val) {
i++;
if (val is List && list2[i] is List) {
return _listsAreEqual(val,list2[i]);
} else {
return list2[i] == val;
}
});
}
List _removeDuplicates(list) {
for (var i=list.length-1; i>0; i--) {
for (var j=i-1; j>=0; j--) {
if (_listsAreEqual(list[i], list[j])) {
list.removeAt(i);
break;
}
}
}
return list;
}
void main(){
List listOfLists = [[1, 5], [2, 9], [10, 12], [-1, 4], [2, 9]];
List uniqueLists = _removeDuplicates(listOfLists);
//List uniqueLists = _removeDuplicates(List.from(listOfLists)); // leave original list unmodified
print(uniqueLists);
}
If you do not want the original list to be modified, use the commented statement instead to create a copy of the original list.
Thank you for your help. Not sure why I didn't find this previously, but this does the trick perfectly.
List uniqueList = listOfLists.map((f) => f.toString()).toSet().toList().map((f) => json.decode(f) as List<dynamic>).toList();
I can successfully parse a comma-delimited list with dart-petitparser, but the same code fails when confronted with a space-delimited list:
class MyDefinition extends GrammarDefinition {
#override
Parser start() => throw UnsupportedError('Not yet.');
Parser commas() =>
ref0(number).separatedBy(char(','), includeSeparators: false);
Parser spaces() =>
ref0(number).separatedBy(char(' '), includeSeparators: false);
Parser<int> number() => digit().plus().flatten().trim().map(int.parse);
}
test('commas', () {
var defn = MyDefinition();
var parser = defn.build(start: defn.commas);
expect(parser.parse("7,4,9,5").value, [7, 4, 9, 5]); // PASS
});
test('spaces', () {
var defn = MyDefinition();
var parser = defn.build(start: defn.spaces);
expect(parser.parse("7 4 9 5").value, [7, 4, 9, 5]); // FAIL
});
It fails with the following:
Expected: [7, 4, 9, 5]
Actual: [7]
Which: at location [1] is [7] which shorter than expected
What am I doing wrong? I've dug through a bunch of the example grammars, and didn't find a whitespace-delimited list anywhere.
The .trim() parser in your number parser is consuming whitespaces before and after the number. This prevents the space-delimited list parser from finding a separating whitespace.
I suggest to rewrite as:
Parser spaces() =>
ref0(number).separatedBy(char(' ').plus(), includeSeparators: false);
// add this ^^^^^^^
Parser<int> number() =>
digit().plus().flatten().map(int.parse);
// remove trim, here ^
I have the following test target:
block_test (
name = "full_test",
block = ":block_name"
...
params = {
"PARAM1" : 1,
"PARAM2" : 2,
"PARAM3" : 3
}
)
And I have a struct which defines the possible values of each param:
params_options = {
"param1" : [1, 2, 34],
"param2" : [43, 2 ,54],
"param3" : [3, 5, 6]
}
I would like to have a single target that would run a target like block_test for every possible combination of parameters.
I thought about doing this by creating a macro which will declare a target for every possible combination of parameters, and finally a test target which will depend on those targets.
Is there any better approach? There may be thousands of combinations and so:
I'm afraid I'll get a big mess when querying the build.
I'm afraid that this isn't very performant, with regards to memory utilization.
You can generate a block_test for each set of parameters using list comprehension:
[block_test (
name = "full_test",
block = ":block_name"
...
params = p
) for p in [
{1, 2, 34},
{43, 2 ,54},
{3, 5, 6},
]]
this is my first question here so forgive me if it is not very clear.
I am trying to create an array in swift that will store either arrays of arrays or an integer number.
The array is supposed to be a simple representation of the data I will be using which is basically a tree kind of data structure like so...
Array = [ [[ [2, 3] ], [ 1, 4 ], [ 2 ]],
[ 2 ], [[2, 5], [6, 1] ], 3 ]
In overall the arrays are the branches and the integers are the leaves
I've tried declaring them as optionals like so
var test2 = [[[Int]?]?]()
Or using typedef's but I still can't get it to work.
Also, it should be possible to add new leaves to any of the arrays
Here is a solution based on enum, first declare the enum:
enum Node
{
case leaf(Int)
case branch([Node])
}
You can now write things such as:
let x = Node.leaf(42)
let y = Node.branch([Node.leaf(42), Node.leaf(24)])
However this is going to become laborious very quickly. Fortunately Swift allows conversions from literals, so we add:
extension Node : ExpressibleByIntegerLiteral
{
init(integerLiteral value: Int)
{
self = .leaf(value)
}
}
extension Node : ExpressibleByArrayLiteral
{
init(arrayLiteral elements: Node...)
{
self = .branch(elements)
}
}
And with those added we can now write the above two let statements as:
let x : Node = 42
let y : Node = [42, 24]
which is nicer. However if we print(y) we get:
branch([Node.leaf(42), Node.leaf(24)])
If you wish to pretty print that you can add:
extension Node : CustomStringConvertible
{
var description : String
{
switch self
{
case .leaf(let value):
return value.description
case .branch(let branches):
return branches.description
}
}
}
And now print(y) gives you:
[42, 24]
Finally your example:
let w : Node = [[[[2, 3]], [1, 4], [2]], [2], [[2, 5], [6, 1]], 3]
which prints as:
[[[[2, 3]], [1, 4], [2]], [2], [[2, 5], [6, 1]], 3]
You'll want to complete the enum type, with predicates such as isLeaf etc., but that is the basic idea.
HTH