From 7ef915ebc88f7784cdec5a9917818cbdfe1a76c4 Mon Sep 17 00:00:00 2001 From: Marc Weber Date: Tue, 8 Dec 2009 21:47:14 +0000 Subject: [PATCH] nixpkgs lib: adding haskell's take svn path=/nixpkgs/trunk/; revision=18848 --- pkgs/lib/lists.nix | 6 ++++++ pkgs/lib/tests.nix | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/pkgs/lib/lists.nix b/pkgs/lib/lists.nix index 3a5ed2628de8..4359662da27b 100644 --- a/pkgs/lib/lists.nix +++ b/pkgs/lib/lists.nix @@ -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); + } diff --git a/pkgs/lib/tests.nix b/pkgs/lib/tests.nix index c5476cbc18c6..9aa04f623608 100644 --- a/pkgs/lib/tests.nix +++ b/pkgs/lib/tests.nix @@ -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 {};