Loading lib/default.nix +1 −0 Original line number Diff line number Diff line Loading @@ -279,6 +279,7 @@ let naturalSort compareLists take takeEnd drop dropEnd sublist Loading lib/lists.nix +34 −0 Original line number Diff line number Diff line Loading @@ -1462,6 +1462,40 @@ rec { */ take = count: sublist 0 count; /** Return the last (at most) N elements of a list. # Inputs `count` : Maximum number of elements to pick `list` : Input list # Type ``` takeEnd :: int -> [a] -> [a] ``` # Examples :::{.example} ## `lib.lists.takeEnd` usage example ```nix takeEnd 2 [ "a" "b" "c" "d" ] => [ "c" "d" ] takeEnd 2 [ ] => [ ] ``` ::: */ takeEnd = n: xs: drop (max 0 (length xs - n)) xs; /** Remove the first (at most) N elements of a list. Loading lib/tests/misc.nix +63 −0 Original line number Diff line number Diff line Loading @@ -1357,6 +1357,69 @@ runTests { ) ]; testTakeEnd = let inherit (lib) takeEnd; in testAllTrue [ ( takeEnd 0 [ 1 2 3 ] == [ ] ) ( takeEnd 1 [ 1 2 3 ] == [ 3 ] ) ( takeEnd 2 [ 1 2 3 ] == [ 2 3 ] ) ( takeEnd 3 [ 1 2 3 ] == [ 1 2 3 ] ) ( takeEnd 4 [ 1 2 3 ] == [ 1 2 3 ] ) (takeEnd 0 [ ] == [ ]) (takeEnd 1 [ ] == [ ]) ( takeEnd (-1) [ 1 2 3 ] == [ ] ) (takeEnd (-1) [ ] == [ ]) ]; testDrop = let inherit (lib) drop; Loading Loading
lib/default.nix +1 −0 Original line number Diff line number Diff line Loading @@ -279,6 +279,7 @@ let naturalSort compareLists take takeEnd drop dropEnd sublist Loading
lib/lists.nix +34 −0 Original line number Diff line number Diff line Loading @@ -1462,6 +1462,40 @@ rec { */ take = count: sublist 0 count; /** Return the last (at most) N elements of a list. # Inputs `count` : Maximum number of elements to pick `list` : Input list # Type ``` takeEnd :: int -> [a] -> [a] ``` # Examples :::{.example} ## `lib.lists.takeEnd` usage example ```nix takeEnd 2 [ "a" "b" "c" "d" ] => [ "c" "d" ] takeEnd 2 [ ] => [ ] ``` ::: */ takeEnd = n: xs: drop (max 0 (length xs - n)) xs; /** Remove the first (at most) N elements of a list. Loading
lib/tests/misc.nix +63 −0 Original line number Diff line number Diff line Loading @@ -1357,6 +1357,69 @@ runTests { ) ]; testTakeEnd = let inherit (lib) takeEnd; in testAllTrue [ ( takeEnd 0 [ 1 2 3 ] == [ ] ) ( takeEnd 1 [ 1 2 3 ] == [ 3 ] ) ( takeEnd 2 [ 1 2 3 ] == [ 2 3 ] ) ( takeEnd 3 [ 1 2 3 ] == [ 1 2 3 ] ) ( takeEnd 4 [ 1 2 3 ] == [ 1 2 3 ] ) (takeEnd 0 [ ] == [ ]) (takeEnd 1 [ ] == [ ]) ( takeEnd (-1) [ 1 2 3 ] == [ ] ) (takeEnd (-1) [ ] == [ ]) ]; testDrop = let inherit (lib) drop; Loading