about summary refs log tree commit diff
path: root/lib/lists.nix
diff options
context:
space:
mode:
authorh7x4 <h7x4@nani.wtf>2022-12-17 21:57:17 +0100
committerh7x4 <h7x4@nani.wtf>2023-02-06 20:40:47 +0100
commit7c4abbf80e1471d7844aae825f6d1015ce315a48 (patch)
tree4732d1dfe37370875eed0d8317d02b7b4237948e /lib/lists.nix
parent41169b15c53350d893d3715cf19eed598d3dbec1 (diff)
lib.lists: add `replicate`
`replicate` returns n copies of an element as a list.

Co-Authored-By: Silvan Mosberger <contact@infinisil.com>
Diffstat (limited to 'lib/lists.nix')
-rw-r--r--lib/lists.nix12
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/lists.nix b/lib/lists.nix
index 602b13cf69cff..32b3f06b195f5 100644
--- a/lib/lists.nix
+++ b/lib/lists.nix
@@ -303,6 +303,18 @@ rec {
     else
       genList (n: first + n) (last - first + 1);
 
+  /* Return a list with `n` copies of an element.
+
+    Type: replicate :: int -> a -> [a]
+
+    Example:
+      replicate 3 "a"
+      => [ "a" "a" "a" ]
+      replicate 2 true
+      => [ true true ]
+  */
+  replicate = n: elem: genList (_: elem) n;
+
   /* Splits the elements of a list in two lists, `right` and
      `wrong`, depending on the evaluation of a predicate.