Sorting an dictionary of dictionary in swift3 based on particular key - ios

I am receiving the following response from API, I need to sort this dictionary of dictionary based on the displayOrder key. So as per the below code you can see currently the order is [SALE, OFFER, DISCOUNT, SOLD] which I want to be in order [SALE, SOLD, OFFER, DISCOUNT].
["SALE": {
displayOrder = 1;
segId = 2;
chg = 2;
}, "OFFER": {
displayOrder = 3;
segId = 4;
chg = 2;
}, "DISCOUNT": {
displayOrder = 4;
segId = 1;
chg = 1;
}, "SOLD": {
displayOrder = 2;
segId = 2;
chg = 1;
}]

Simple sort it with sorted function like:
let dic = ["SALE": [
"displayOrder" : 1,
"segId" : 2,
"chg" : 2,
], "OFFER": [
"displayOrder" : 3,
"segId" : 4,
"chg" : 2,
], "DISCOUNT": [
"displayOrder" : 4,
"segId" : 1,
"chg" : 1,
], "SOLD": [
"displayOrder" : 2,
"segId" : 2,
"chg" : 1,
]]
dic.sorted { (lhs, rhs) -> Bool in
lhs.value["displayOrder"]! < rhs.value["displayOrder"]!
}

Related

Eager print in Nix in the repl

The documentation flake-utils has the following example as a doc
eachSystem [ system.x86_64-linux ] (system: { hello = 42; })
# => { hello = { x86_64-linux = 42; }; }
eachSystem allSystems (system: { hello = 42; })
# => {
hello.aarch64-darwin = 42,
hello.aarch64-genode = 42,
hello.aarch64-linux = 42,
...
hello.x86_64-redox = 42,
hello.x86_64-solaris = 42,
hello.x86_64-windows = 42
}
as far as I can tel, one has to
> nix repl
nix-repl> e = builtins.getFlake("github:numtide/flake-utils")
nix-repl> with e.outputs.lib;
eachSystem [ system.x86_64-linux ] (system: { hello = 42; })
to get a result value (one can also do :a e.outputs.lib to "Add attributes from resulting set to scope" and not use the with ..; line )
{ hello = { ... }; }
Is there a way to "eagerly print" the value ?
What you are looking for is :p:
> { a = 3; b = [ 1 2 3 ]; }
{ a = 3; b = [ ... ]; }
> :p { a = 3; b = [ 1 2 3 ]; }
{ a = 3; b = [ 1 2 3 ]; }
>

Get the sum of numbers for similar element in a list of object in Dart?

I need to iterate over a list of object and sum all "num" for every "title"
var obj1 = MyObject(tNum: 10, tTitle: 'Hi');
var obj2 = MyObject(tNum: 9, tTitle: 'Hi');
var obj3 = MyObject(tNum: 8, tTitle: 'Hello');
var obj4 = MyObject(tNum: 7, tTitle: 'Hello');
var obj5 = MyObject(tNum: 12, tTitle: 'Good');
var myList = [obj1, obj2, obj3, obj4, obj5];
var myListIter = myList.iterator;
var initialTitle = '';
var finalSum = 0;
while (myListIter.moveNext()) {
print(myListIter.current);
if (myListIter.current.title!.compareTo(initialTitle).isEven) {
finalSum = finalSum + myListIter.current.num!;
}else{
print('--- ${myListIter.current.title!} : $finalSum');
}
}
}
class MyObject {
int? num;
String? title;
MyObject({tNum, tTitle}) {
num = tNum;
title = tTitle;
}
}
The output:
--- Hi : 0
--- Hi : 0
--- Hello : 0
--- Hello : 0
--- Good : 0
Expected output:
--- Hi : 19
--- Hello : 15
--- Good : 12
What is the simplest way to do that?
q,nfczej,pvledv;lzecaz
var myMap = <String, int>{};
myList.forEach((obj) => myMap[obj.title!] = (myMap[obj.title] ?? 0) + obj.num!);
print(myMap);
Output:
{Hi: 19, Hello: 15, Good: 12}

Changing value in table in Lua

I am trying to make a table and want to change a value in that table for a particular key. The thing is when I do change the key it does change for all the keys.
function dump(o, nb)
if nb == nil then
nb = 0
end
if type(o) == 'table' then
local s = ''
for i = 1, nb + 1, 1 do
s = s .. " "
end
s = '{\n'
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
for i = 1, nb, 1 do
s = s .. " "
end
s = s .. '['..k..'] = ' .. dump(v, nb + 1) .. ',\n'
end
for i = 1, nb, 1 do
s = s .. " "
end
return s .. '}'
else
return tostring(o)
end
end
Config={}
PlayersStatusTable={}
Config.DefaultStatus = {
hunger = 1000000,
thirst = 1000000,
}
local timeNow = os.clock()
PlayersStatusTable[12] = Config.DefaultStatus
PlayersStatusTable[112] = Config.DefaultStatus
PlayersStatusTable[54] = Config.DefaultStatus
for playerId, details in pairs(PlayersStatusTable) do
print("playerid1",playerId)
print(dump(PlayersStatusTable))
print(dump(PlayersStatusTable[112]))
print(dump(PlayersStatusTable[112].hunger))
PlayersStatusTable[112].hunger = 5
end
the output is this:
playerid1 112
{
[112] = {
["thirst"] = 1000000,
["hunger"] = 1000000,
},
[54] = {
["thirst"] = 1000000,
["hunger"] = 1000000,
},
[12] = {
["thirst"] = 1000000,
["hunger"] = 1000000,
},
}
{
["thirst"] = 1000000,
["hunger"] = 1000000,
}
1000000
playerid1 54
{
[112] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
[54] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
[12] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
}
{
["thirst"] = 1000000,
["hunger"] = 5,
}
5
playerid1 12
{
[112] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
[54] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
[12] = {
["thirst"] = 1000000,
["hunger"] = 5,
},
}
{
["thirst"] = 1000000,
["hunger"] = 5,
}
5
I just want the hunger of id 112 to be 5.
You're assigning the same table to all 3 keys, so they all point to the same table that's being changed. You need to ensure that you're creating a new table when you assign to each key.
local function shallowCopy(t)
local result = {}
for k, v in pairs(t) do
result[k] = v
end
return result
end
PlayersStatusTable[12] = shallowCopy(Config.DefaultStatus)

How to print a table's contents within a table? [Lua]

What I want to do is ONLY print a table's contents within a table. For example:
local stats = {
table1 = {
tTable1 =
{
data = 1
},
tTable2 =
{
data2 = 2
},
tTable3 =
{
data3 = 3
},
}
}
I don't really care about table1 or all the tTables but rather the information in the data variables. How can I print them?
This is a snippet of my real code:
local stats = {
[1] = {
[1] = {
[1] = 1,
[2] = -1,
[3] = -1,
["n"] = 3,
},
[2] = {
[1] = nuclearcraft:cooler,
[2] = 10,
["n"] = 2,
},
["n"] = 2,
},
[2] = {
[1] = {
[1] = 2,
[2] = -1,
[3] = -1,
["n"] = 3,
},
[2] = {
[1] = nuclearcraft:cell_block,
[2] = 0,
["n"] = 2,
},
["n"] = 2,
},
[3] = {
[1] = {
[1] = 3,
[2] = -1,
[3] = -1,
["n"] = 3,
},
[2] = {
[1] = nuclearcraft:cooler,
[2] = 10,
["n"] = 2,
},
["n"] = 2,
},
}
This code actually goes on for a bit longer than this. In the real code, I don't care for any of the data except for the areas where it says "nuclearcraft" and the number beneath it.
recursive table traversal is suitable for this case:
local function TablePrint(t)
for k,v in pairs(t) do
if type(v)=="table" then
print(k)
TablePrint(v)
else
print('\t',k,v)
end
end
end
TablePrint(stats)
result:
table1
tTable3
data3 3
tTable2
data2 2
tTable1
data 1
keep in mind that the order of non-index values in the table is not defined

nix function to merge attributes / records recursively and concatenate arrays

Does someone know such function that merges list of records
if all values to merge are records - merge them recursively
if all values to merge are arrays - concatenate arrays
If values can't be merged - the latter value is preferred
Example 1:
recursiveMergeAttrs [
{ a = "x"; c = "m"; list = [1]; }
{ a = "y"; b = "z"; list = [2]; }
]
returns
{ a = "y"; b = "z"; c="m"; list = [1 2] }
Example 2
recursiveMergeAttrs [
{
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/hda";
}
{
boot.loader.grub.device = "";
}
]
returns
{
boot.loader.grub.enable = true;
boot.loader.grub.device = "";
}
P.S.
recursiveUpdate is not working
recursiveMergeAttrs = listOfAttrsets: lib.fold (attrset: acc: lib.recursiveUpdate attrset acc) {} listOfAttrsets
recursiveMergeAttrs [ { a = "x"; c = "m"; list = [1]; } { a = "y"; b = "z"; list = [2]; } ]
returns
{ a = "y"; b = "z"; c = "m"; list = [ 2 ]; }
Did it
{ lib, ... }:
with lib;
/*
Merges list of records, concatenates arrays, if two values can't be merged - the latter is preferred
Example 1:
recursiveMerge [
{ a = "x"; c = "m"; list = [1]; }
{ a = "y"; b = "z"; list = [2]; }
]
returns
{ a = "y"; b = "z"; c="m"; list = [1 2] }
Example 2:
recursiveMerge [
{
a.a = [1];
a.b = 1;
a.c = [1 1];
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/hda";
}
{
a.a = [2];
a.b = 2;
a.c = [1 2];
boot.loader.grub.device = "";
}
]
returns
{
a = {
a = [ 1 2 ];
b = 2;
c = [ 1 2 ];
};
boot = {
loader = {
grub = {
device = "";
enable = true;
};
};
};
}
*/
let
recursiveMerge = attrList:
let f = attrPath:
zipAttrsWith (n: values:
if tail values == []
then head values
else if all isList values
then unique (concatLists values)
else if all isAttrs values
then f (attrPath ++ [n]) values
else last values
);
in f [] attrList;
in
recursiveMerge

Resources