Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion questions.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
{
"topic": "Reimplementation: attrVals",
"path": "reimplementation/attrVals",
"question": " Write your own implementation of the `attrVals` function.\n\n It consumes a list of `attribute names` and an \n `attribute set`. It returnes the values of each \n `attribute name`.\n \n**Note**: Remember that `attrSet ? \"a\"` returns `true` and `attrSet ? \"j\"` => `false`!\n\n**Note**: Remember that `key = a; attrSet.${key}` returns `1`!\n\n###`attrVals` vs `attrValues`: \n\n* `attrVals` -> given a list of names, extract their values from the set and return a list of them\n\n `attrVals [\"a\" \"b\" \"c\"] attrSet; => should be [1 2 3]` \n\n* `attrValues` -> extract all values in the given set and return a list of them\n\n `attrValues attrSet; => [1 2 3 4]`\n\n**Warning:** This is hard!",
"question": " Write your own implementation of the `attrVals` function.\n\n It consumes a list of `attribute names` and an \n `attribute set`. It returnes the values of each \n `attribute name`.\n \n**Note**: Remember that `attrSet ? \"a\"` returns `true` and `attrSet ? \"j\"` => `false`!\n\n**Note**: Remember that `key = \"a\"; attrSet.${key}` returns `1`!\n\n###`attrVals` vs `attrValues`: \n\n* `attrVals` -> given a list of names, extract their values from the set and return a list of them\n\n `attrVals [\"a\" \"b\" \"c\"] attrSet; => should be [1 2 3]` \n\n* `attrValues` -> extract all values in the given set and return a list of them\n\n `attrValues attrSet; => [1 2 3 4]`\n\n**Warning:** This is hard!",
"code": "with import <nixpkgs> { };\nlet\n attrSet = {c = 3; a = 1; b = 2; d=4;};\n\n #tips: use the map function and access the attribute values \n attrVals = XXX;\n\nin\nrec {\n\n solution = attrVals [\"a\" \"b\" \"c\"] attrSet; #should be [1 2 3]\n}\n",
"solution": "\nwith import <nixpkgs> { };\nlet\n attrSet = {c = 3; a = 1; b = 2; d=4;};\n attrVals = kys: se: lib.fold (el: c: if se ? \"${el}\" then [(se.${el})] ++ c else c) [] kys;\n\nin\nrec {\n\n solution = attrVals [\"a\" \"b\" \"c\"] attrSet; #should be [1 2 3]\n}\n\n",
"youtube": "https://www.youtube.com/watch?v=CQAeAyRX1zs&list=PLMr2KA8WWojv-4cgqIiVebml0FrMl8N_-"
Expand Down