solutions/A Tour of Nix/33.nix

11 lines
341 B
Nix
Raw Normal View History

2024-04-16 19:13:00 -04:00
with import <nixpkgs> { };
let
list = [["a"] ["b"] ["c"]];
attrList = [{a = 1;} {b = 0;} {a = 2;}];
catAttrs = atr: l: lib.fold (val: _l: if (builtins.hasAttr atr val) then [ (val.${atr}) ] ++ _l else _l) [] l;
in
rec {
example = builtins.concatLists list; #is [ "a" "b" "c" ]
result = catAttrs "a" attrList; #should be [1 2]
}