Commit bc8fbc25 authored by Silvan Mosberger's avatar Silvan Mosberger
Browse files

lib.lists.hasPrefix: init

parent a4f7840e
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -612,6 +612,21 @@ rec {
    # Input list
    list: sublist count (length list) list;

  /* Whether the first list is a prefix of the second list.

  Type: hasPrefix :: [a] -> [a] -> bool

  Example:
    hasPrefix [ 1 2 ] [ 1 2 3 4 ]
    => true
    hasPrefix [ 0 1 ] [ 1 2 3 4 ]
    => false
  */
  hasPrefix =
    list1:
    list2:
    take (length list1) list2 == list1;

  /* Return a list consisting of at most `count` elements of `list`,
     starting at index `start`.

+21 −0
Original line number Diff line number Diff line
@@ -480,6 +480,27 @@ runTests {
    ([ 1 2 3 ] == (take 4 [  1 2 3 ]))
  ];

  testListHasPrefixExample1 = {
    expr = lists.hasPrefix [ 1 2 ] [ 1 2 3 4 ];
    expected = true;
  };
  testListHasPrefixExample2 = {
    expr = lists.hasPrefix [ 0 1 ] [ 1 2 3 4 ];
    expected = false;
  };
  testListHasPrefixLazy = {
    expr = lists.hasPrefix [ 1 ] [ 1 (abort "lib.lists.hasPrefix is not lazy") ];
    expected = true;
  };
  testListHasPrefixEmptyPrefix = {
    expr = lists.hasPrefix [ ] [ 1 2 ];
    expected = true;
  };
  testListHasPrefixEmptyList = {
    expr = lists.hasPrefix [ 1 2 ] [ ];
    expected = false;
  };

  testFoldAttrs = {
    expr = foldAttrs (n: a: [n] ++ a) [] [
    { a = 2; b = 7; }