Eager print in Nix in the repl - nix

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

Related

Iterate through multiple lists using locals in Terraform code

I have the following list of Resource Group names and Virtual Machine Names, I will like to create a map using locals so I can iterate through it using each.key and each.value in my code.
VARIABLES:
variable "vm_names" {
default = [
"vm1",
"vm2",
"vm3",
"vm4",
"vm5"
]
variable "rg_names" {
default = [
"rg1",
"rg2",
"rg3",
"rg4",
"rg5"
]
LOCALS:
locals {
vm = var.vm_names
rg = var.rg_names
vmrg=[for k,v in zipmap(local.vm, local.rg):{
vm = k
rg = v
}]
}
RESULT:
+ vmrg = [
+ {
+ rg = "rg1"
+ vm = "vm1"
},
+ {
+ rg = "rg2"
+ vm = "vm2"
},
+ {
+ rg = "rg3"
+ vm = "vm3"
},
+ {
+ rg = "rg4"
+ vm = "vm4"
},
+ {
+ rg = "rg5"
+ vm = "vm5"
},
]
DESIRED RESULT:
vmrg = {
vm1 = "rg1"
vm2 = "rg2"
vm3 = "rg3"
vm4 = "rg4"
vm5 = "rg5"
}
Actually, it is much simpler and you were already close to figuring it out. The only thing you need is the zipmap built-in function:
vmrg = zipmap(local.vm, local.rg)
will yield:
> local.vmrg
{
"vm1" = "rg1"
"vm2" = "rg2"
"vm3" = "rg3"
"vm4" = "rg4"
"vm5" = "rg5"
}

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

Sorting an dictionary of dictionary in swift3 based on particular key

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

Resources