nixpkgs lib: adding haskell's take

svn path=/nixpkgs/trunk/; revision=18848
This commit is contained in:
Marc Weber 2009-12-08 21:47:14 +00:00
parent 89b59e3fbc
commit 7ef915ebc8
2 changed files with 15 additions and 0 deletions

View File

@ -161,4 +161,10 @@ rec {
in
qs list [];
# haskell's take: take 2 [1 2 3 4] yields [1 2]
take = count: list:
if list == [] || count == 0 then []
else [ (head list) ] ++ take (builtins.sub count 1) (tail list);
}

View File

@ -61,6 +61,15 @@ runTests {
expected = true;
};
testTake = testAllTrue [
([] == (take 0 [ 1 2 3 ]))
([1] == (take 1 [ 1 2 3 ]))
([ 1 2 ] == (take 2 [ 1 2 3 ]))
([ 1 2 3 ] == (take 3 [ 1 2 3 ]))
([ 1 2 3 ] == (take 4 [ 1 2 3 ]))
];
testOverridableDelayableArgsTest = {
expr =
let res1 = defaultOverridableDelayableArgs id {};