solutions/A Tour of Nix/33.nix

11 lines
341 B
Nix

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