Using a list to access a function in dart flutter - dart

How do I access a function through a list?
I have something like this:
List processList = [
[001, 'Volume', 0.8,0,0],
[002, 'Complexity', 0.5,0,0],
[003, 'Silence', 0.3,0,0],
[004, 'Notice', 0.3,0,0],
....];
And I want 001, 002 etc to be functions that I can call through
processList[i][0] ();
Like a list of functions...
I cannot figure out the syntax for this somehow, the only thing I can achieve is filling the List with the result of the function, but that is not what I need.

Try
void main() {
List processList = [
[(){print('London');}, 'Volume', 0.8, 0, 0],
[(){print('Aarhus');}, 'Complexity', 0.5, 0, 0],
[(){print('Munich');}, 'Silence', 0.3, 0, 0],
[(){print('LA');}, 'Notice', 0.3, 0, 0],
];
int i = 3;
processList[i][0]();
}
prints LA. Looks at typedefs for ways to declare function signatures.

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.

How can I implement a regression test in bazel?

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

Highcharts Heatmap background color when invalid data

When I render an invalid data like a string on Highcharts Heatmap, it shows the string with a black background. Below is an example of an invalid data:
data: [[0, 0, "A"], [1, 0, "B"], [2, 0, "C"], [3, 0, "D"], [4, 0, "E"]]
Can I change this default behavior and show another background color instead of black?
When you use a unsupported value the ColorAxis.toColor function returns rgb(NaN,NaN,NaN) as the color of the cell.
By writing a rather small wrapper for that function you could intercept string values and return a color of your choice.
For example (JSFiddle demo):
(function (H) {
H.wrap(H.ColorAxis.prototype, 'toColor', function (proceed, value, point) {
if(typeof value === 'string') // String value -> Return pink
return 'rgb(255,105,180)';
else // Normal value -> Proceed as usual
return proceed.apply(this, Array.prototype.slice.call(arguments, 1));
});
}(Highcharts));

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

JsxGraph from Dart using Dart JS interop

I am trying to use the plotting library JSXGraph from Dart using dart js but I have problems.
This is working javascript code
var board = JXG.JSXGraph.initBoard(
'box',
{
boundingbox: [-10, 10, 2, -1],
axis: true,
keepaspectratio: false,
showNavigation: false
}
);
var graph = board.create(
'functiongraph',
[function(x){ return 3*x*x + 5*x + 1;}, -10, 10],
{strokeColor: '#ffff00'}
);
You can try it here on jsFiddle.
I want to do basically the identicall thing but from dart. This is what I have come up with so far:
JsObject jsxGraph = context['JXG']['JSXGraph'];
var board = jsxGraph.callMethod('initBoard', [
'box',
new JsObject.jsify({
'boundingbox': [-10, 10, 2, -1],
'axis': true,
'keepaspectratio': false,
'showNavigation': false
})
]);
var graph = board.callMethod('create', [
'functiongraph',
[(x){return 3*x*x + 5*x + 1;}, -10, 10],
new JsObject.jsify({'strokeColor': '#ffff00'})
]);
I get the plot area and axes but I can't get the actual plot line. Instead, there is an error on the console: Uncaught TypeError: this.Y is not a function.
Could someone, please, point me to what I could be doing wrong? I suspect it is something about the callback function but I really don't know what.
There's a jsify missing in your code:
JsObject jsxGraph = context['JXG']['JSXGraph'];
var board = jsxGraph.callMethod('initBoard', [
'box',
new JsObject.jsify({
'boundingbox': [-10, 10, 2, -1],
'axis': true,
'keepaspectratio': false,
'showNavigation': false
})
]);
var graph = board.callMethod('create', [
'functiongraph',
new JsObject.jsify([(x){return 3*x*x + 5*x + 1;}, -10, 10]},
new JsObject.jsify({'strokeColor': '#ffff00'})
]);

Resources