How can I combine/merge 2 or more maps in dart into 1 map?
for example I have something like:
var firstMap = {"1":"2"};
var secondMap = {"1":"2"};
var thirdMap = {"1":"2"};
I want:
var finalMap = {"1":"2", "1":"2", "1":"2"};
you can use addAll method of Map object
var firstMap = {"1":"2"};
var secondMap = {"2":"3"};
var thirdMap = {};
thirdMap.addAll(firstMap);
thirdMap.addAll(secondMap);
print(thirdMap);
Or
var thirdMap = {}..addAll(firstMap)..addAll(secondMap);
Update
Since dart sdk 2.3
You can use spread operator ...
final firstMap = {"1":"2"};
final secondMap = {"2":"3"};
final thirdMap = {
...firstMap,
...secondMap,
};
alternate syntax using Map.addAll, Iterable.reduce and cascading operator, for combining a lot of maps:
var combinedMap = mapList.reduce( (map1, map2) => map1..addAll(map2) );
live dartpad example
https://dartpad.dartlang.org/9cd116d07d2f45a9b890b4b1186dcc5e
Another option is using CombinedMapView from package:collection:
new CombinedMapView([firstMap, secondMap])
It doesn't create a merged map, but creates a Map that is a view of both.
I came up with a "single line" solution for an Iterable of Maps:
var finalMap = Map.fromEntries(mapList.expand((map) => map.entries));
var firstMap = {"1":"5"};
var secondMap = {"1":"6"};
var thirdMap = {"2":"7"};
var finalMap = {...firstMap, ...secondMap, ...thirdMap};
// finalMap: {"1":"6", "2":"7"};
Notice that key "1" with value "5" will be overwritten with "6".
Related
I adjust tickscript(kapacitor) for my calculation.
However, no alerts work after few changes.
Before adjustment, it worked without problem.
I wonder why it does not work after adjustment.
To be more specific, I need to get "MACD" of my stock price.
Measurements contain the price of each items.
Anyone knows why?
before it was:
var db = 'telegraf'
var rp = 'autogen'
var measurement = 'exec'
var groupBy = ['symbol']
var whereFilter = lambda: TRUE
var name = 'coin'
var idVar = name + '-{{.Group}}'
var message = ' {{.ID}} {{.Level}} {{.Time}}'
var idTag = 'alertID'
var levelTag = 'level'
var messageField = 'message'
var durationField = 'duration'
var outputDB = 'chronograf'
var outputRP = 'autogen'
var outputMeasurement = 'alerts'
var triggerType = 'threshold'
var crit = 50000000000
var data = stream
|from()
.database(db)
.retentionPolicy(rp)
.measurement(measurement)
.groupBy(groupBy)
.where(whereFilter)
|eval(lambda: "lastPrice")
.as('value')
var trigger = data
|alert()
.crit(lambda: "value" > crit)
.message(message)
.id(idVar)
.idTag(idTag)
.levelTag(levelTag)
.messageField(messageField)
.durationField(durationField)
.stateChangesOnly()
.telegram()
.chatId('####')
.parseMode('Markdown')
trigger
|eval(lambda: float("value"))
.as('value')
.keep()
|influxDBOut()
.create()
.database(outputDB)
.retentionPolicy(outputRP)
.measurement(outputMeasurement)
.tag('alertName', name)
.tag('triggerType', triggerType)
trigger
|httpOut('output')
Now it became:
var db = 'telegraf'
var rp = 'autogen'
var measurement = 'exec'
var groupBy = ['symbol']
var whereFilter = lambda: TRUE
var name = 'coin'
var idVar = name + '-{{.Group}}'
var message = ' {{.ID}} {{.Level}} {{.Time}}'
var idTag = 'alertID'
var levelTag = 'level'
var messageField = 'message'
var durationField = 'duration'
var outputDB = 'chronograf'
var outputRP = 'autogen'
var outputMeasurement = 'alerts'
var triggerType = 'threshold'
var crit = 0
var data = stream
|from()
.database(db)
.retentionPolicy(rp)
.measurement(measurement)
.groupBy(groupBy)
.where(whereFilter)
|eval(lambda: "lastPrice")
.as('v')
var avg12 = data
|window()
.period(3m)
.every(1m)
|mean('x1')
.as('x1a')
var avg26 = data
|window()
.period(5m)
.every(1m)
|mean('x2')
.as('x2a')
var macd = avg12
|join(avg26)
.as('x1a', 'x2a')
|eval(lambda: float('x1a.value' - 'x2a.value'))
.keep()
.as('value')
var trigger = macd
|alert()
.crit(lambda: "value" > crit)
.message(message)
.id(idVar)
.idTag(idTag)
.levelTag(levelTag)
.messageField(messageField)
.durationField(durationField)
.stateChangesOnly()
.telegram()
.chatId('###')
.parseMode('Markdown')
trigger
|eval(lambda: float("value"))
.as('value')
.keep()
|influxDBOut()
.create()
.database(outputDB)
.retentionPolicy(outputRP)
.measurement(outputMeasurement)
.tag('alertName', name)
.tag('triggerType', triggerType)
trigger
|httpOut('output')
Is it possible to build something like this in Linq2DB? Currently I have a problem that ITable<T> is not assignable to either IUpdatable<T> or IValueInsertable<T>. Or maybe what I'm trying to achieve is not a good practice?
var optional1 = Optional<int>.None();
var optional2 = Optional<int>.Of(123);
var table = Database.MyTable; // ITable<MyRecord>
if (optional1.HasValue) table = table.Set(x => x.Optional1, optional1.Value);
if (optional2.HasValue) table = table.Set(x => x.Optional2, optional2.Value);
await table.UpdateAsync();
The same applies for IValueInsertable
var optional1 = Optional<int>.None();
var optional2 = Optional<int>.Of(123);
var table = Database.MyTable; // ITable<MyRecord>
if (optional1.HasValue) table = table.Value(x => x.Optional1, optional1.Value);
if (optional2.HasValue) table = table.Value(x => x.Optional2, optional2.Value);
await table.InsertAsync();
You can use AsUpdatable()
var updatable = Database.MyTable.AsUpdatable();
if (optional1.HasValue) updatable = updatable.Set(x => x.Optional1, optional1.Value);
if (optional2.HasValue) updatable = updatable.Set(x => x.Optional2, optional2.Value);
await updatable.UpdateAsync();
How can I create multiple objects in 1 Step in Dart? Something like:
Class Player{
var Health;
var Level; .... }
Somewhere else:
Player[] player = new Player[20];
How can I do that in Dart?
If you wanna create a lot "Players"... Try this:
var players = List.generate(20, (i) => Player(/* properties */));
Filling in from any source, you can use the "i" as the index.
var players = List.generate(20, (i) {
var sourceRef = source[i];
return Player(
health: sourceRef["health"]
);
});
You can create a list of Player using the following line:
List<Player> player = new List(20);
And then initialize each object of your player list :
for (var i in jsonResponse['participants']) {
player[x] = new Player() ; // add this to your code
var fill = player[x];
fill.health = i['health'];
x++;
}
You can find more information about the proper way of building and initializing list in the official Dart Documentation.
How can I make this code shorter, is there any way? I feel there's too much repeating..
var bokstaver1:Array = new Array("a", "b", "c");
var bokstaver2:Array = ["d","e","f"];
var bokstaver:Array = New Array();
bokstaver[0] = "b";
bokstaver[1] = "i";
bokstaver[2] = "l";
bokstaver[3] = "l";
bokstaver[4] = "e";
I'm all new here so if this is not a way to ask a question on here please don't hasten insults.
You can do it in this easy way:
var bokstaver:Array = "bille".split("");
trace(bokstaver); // outputs: b,i,l,l,e
If I have an array, I can set the keys by doing the following:
var example:Array = new Array();
example[20] = "500,45";
example[324] = "432,23";
If I want to do something with Objects, how would I achieve this?
I tried the following:
var example:Object = [{x:500, y:45}, {x:432, y:23}]; // Works but keys are 0 and 1
var example:Object = [20: {x:500, y:45}, 324: {x:432, y:23}]; // Compile errors
var example:Object = [20]: {x:500, y:45}, [324]: {x:432, y:23}; // Compile errors
var example:Object = [20] {x:500, y:45}, [324] {x:432, y:23}; // Compile errors
Is there a good way to achieve this?
I understand I could do this:
var example:Object = {id20 : {x:500, y:45}, id324: {x:432, y:23} };
But it doesn't suit me.
The [] notation has the same meaning of doing a new Array() so when you are doing:
var example:Object = [{x:500, y:45}, {x:432, y:23}];
you are in fact creating an array with two elements who are object {x:500, y:45} and {x:432, y:23}.
If you want to create an object with key 20 and 324 use the {} notation who is the same a new Object()
So your example became =>
var example:Object = {20: {x:500, y:45}, 324: {x:432, y:23}};
You can do the same as your first example using an Object instead of an Array:
var example:Object = new Object();
example[20] = "500,45";
example[324] = "432,23";