Loading lib/default.nix +1 −0 Original line number Diff line number Diff line Loading @@ -295,6 +295,7 @@ let elemAt isList concatAttrValues replaceElemAt ; inherit (self.strings) concatStrings Loading lib/lists.nix +37 −0 Original line number Diff line number Diff line Loading @@ -2019,4 +2019,41 @@ rec { ::: */ concatAttrValues = set: concatLists (attrValues set); /** Replaces a list's nth element with a new element # Inputs `list` : Input list `idx` : index to replace `newElem` : new element to replace with # Type ``` replaceElemAt :: [a] -> int - b -> [a] ``` # Examples :::{.example} ## `replaceElemAt` usage example ```nix lib.replaceElemAt` [1 2 3] 0 "a" => ["a" 2 3] ``` ::: */ replaceElemAt = list: idx: newElem: assert lib.assertMsg (idx >= 0 && idx < length list) "'lists.replaceElemAt' called with index ${toString idx} on a list of size ${toString (length list)}"; genList (i: if i == idx then newElem else elemAt list i) (length list); } lib/tests/misc.nix +13 −0 Original line number Diff line number Diff line Loading @@ -4909,4 +4909,17 @@ runTests { targetTarget = "prefix-tt"; }; }; testReplaceElemAt = { expr = lib.replaceElemAt [ 1 2 3 ] 1 "a"; expected = [ 1 "a" 3 ]; }; testReplaceElemAtOutOfRange = testingThrow (lib.replaceElemAt [ 1 2 3 ] 5 "a"); testReplaceElemAtNegative = testingThrow (lib.replaceElemAt [ 1 2 3 ] (-1) "a"); } Loading
lib/default.nix +1 −0 Original line number Diff line number Diff line Loading @@ -295,6 +295,7 @@ let elemAt isList concatAttrValues replaceElemAt ; inherit (self.strings) concatStrings Loading
lib/lists.nix +37 −0 Original line number Diff line number Diff line Loading @@ -2019,4 +2019,41 @@ rec { ::: */ concatAttrValues = set: concatLists (attrValues set); /** Replaces a list's nth element with a new element # Inputs `list` : Input list `idx` : index to replace `newElem` : new element to replace with # Type ``` replaceElemAt :: [a] -> int - b -> [a] ``` # Examples :::{.example} ## `replaceElemAt` usage example ```nix lib.replaceElemAt` [1 2 3] 0 "a" => ["a" 2 3] ``` ::: */ replaceElemAt = list: idx: newElem: assert lib.assertMsg (idx >= 0 && idx < length list) "'lists.replaceElemAt' called with index ${toString idx} on a list of size ${toString (length list)}"; genList (i: if i == idx then newElem else elemAt list i) (length list); }
lib/tests/misc.nix +13 −0 Original line number Diff line number Diff line Loading @@ -4909,4 +4909,17 @@ runTests { targetTarget = "prefix-tt"; }; }; testReplaceElemAt = { expr = lib.replaceElemAt [ 1 2 3 ] 1 "a"; expected = [ 1 "a" 3 ]; }; testReplaceElemAtOutOfRange = testingThrow (lib.replaceElemAt [ 1 2 3 ] 5 "a"); testReplaceElemAtNegative = testingThrow (lib.replaceElemAt [ 1 2 3 ] (-1) "a"); }