Add '31.nix'

This commit is contained in:
NaiJi ✨ 2022-04-27 05:54:34 +00:00
parent 95260715e6
commit 01be02f4cc
1 changed files with 15 additions and 0 deletions

15
31.nix Normal file
View File

@ -0,0 +1,15 @@
with import <nixpkgs> { };
let
attrSet = {c = 3; a = 1; b = 2;};
#This is an example function that extracts a single value
getSingleVal = (attrSet: x: attrSet.${x});
#tips: use the map function and access the attribute values
#in the same way as 'getSingleVal'
attrVals = l: s: map (field: s.${field}) l;
in
rec {
example = getSingleVal attrSet "a"; #is [1]
solution = attrVals ["a" "b" "c"] attrSet; #should be [1 2 3]
}