Iterating over a complex collection in Dart - dart

I would like to be able to iterate over this collection.
import "package:collection/collection.dart";
main() {
EqualityMap edges = new EqualityMap.from(const ListEquality(), {
[1, 'a']: [2, 3],
[2, 'a']: [2],
[3, 'b']: [4, 3],
[4, 'c']: [5]
});
}
I am doing a recursive function so this is not allowed.
edges.keys.forEach((m) {
// some more code
return something;
});
Is it possible to achieve something similar to this?
for(var edge in edges)

Something like this should work:
for (var edgeKey in edges.keys) {
var edge = edges[edgeKey];
// do something
}

Related

Use reduce method in dart to group consecutive elements in the list like in Javascript

I have following codes in javascript
var ArrayLogs = [1, 3, 5, 4, 9, 11, 0, -4, -10];
var newLogArrays = [];
ArrayLogs.reduce(function(result, value, index, array) {
if (index % 2 === 0){
newLogArrays.push(array.slice(index, index + 2));
}
return newLogArrays;
}, []);
Above method outputs:
[[1,3],[5,4],[9,11],[0,-4],[-10]]
I am looking equivalent code in Dart, I know there is reduce method in dart as well, but I am not sure how to use to get similar result as javascript.
Any help will be highly appreciated.
Thanks
If you just want to do it using Dart only, you can do something like the following:
void main() {
final arrayLogs = [1, 3, 5, 4, 9, 11, 0, -4, -10];
final result = arrayLogs.fold<List<List<int>>>([], (list, element) {
if (list.isEmpty || list.last.length > 1) {
return list..add([element]);
} else {
return list..last.add(element);
}
});
print(result);
// [[1, 3], [5, 4], [9, 11], [0, -4], [-10]]
}
I am sure there are some packages which can do this more automatically or cleaner.

Why can't I assign a shuffled array to a new array

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]
}

Removing duplicates from list of lists

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();

Create multidimentional array in swift with different datatypes

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

Grails Flot plugin Example server data not working

I just included the Flot plugin and tried running the sample as mentioned in the documentation.
As per the documentation, the index page looks like this
class FlotController {
def index = {
[data: [[0, 10], [4, 5], null, [6, 2.5], [12, 10]]]
}
}
When rendered in GSP, it
var d4 = \u005b\u005b0\u002c 10\u005d\u002c \u005b4\u002c 5\u005d\u002c null\u002c \u005b6\u002c 2.5\u005d\u002c \u005b12\u002c 10\u005d\u005d;
what is the correct way to define data in the Controller? Thanks
The javascript is as shown below
<g:javascript>
var d1 = [];
for (var i = 0; i < 14; i += 0.5)
d1.push([i, Math.sin(i)]);
var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];
var d3 = [[0, 12], [7, 12], null, [7, 2.5], [12, 2.5]];
var d4 = ${raw(data)}; //line causing the problem
var data = [d1, d2, d3, { label: "server data", data: d4}];
var options = { lines: { show: true }, points: { show: true } };
var d5 = [];
var series = Math.floor(Math.random()*10)+1;
for( var i = 0; i < series; i++) {
d5[i] = { label: "Series"+(i+1), data: Math.floor(Math.random()*100)+1 }
}
var pieOptions = { series: { pie: { show: true } }, legend: { show: false } };
</g:javascript>
EDIT (with the good answer):
For those who have the resources plugin, you need to replace :
<g:javascript></g:javascript>
by
<r:script>...</r:script>
And it works (with or without the function raw()
Previous answer:
In your GSP, you can use the raw() function.
More information here: documentation
But watch out:
"So what happens when you want to stop Grails from escaping some content? There are valid use cases for putting HTML into the database and rendering it as-is, as long as that content is trusted. In such cases, you can tell Grails that the content is safe as should be rendered raw, i.e. without any escaping:"

Resources