Use builtins.genList
This fixes the quadratic complexity of functions like imap.
This commit is contained in:
parent
273d9ffd6a
commit
5976d393fb
@ -4,7 +4,7 @@ with import ./trivial.nix;
|
|||||||
|
|
||||||
rec {
|
rec {
|
||||||
|
|
||||||
inherit (builtins) head tail length isList elemAt concatLists filter elem;
|
inherit (builtins) head tail length isList elemAt concatLists filter elem genList;
|
||||||
|
|
||||||
|
|
||||||
# Create a list consisting of a single element. `singleton x' is
|
# Create a list consisting of a single element. `singleton x' is
|
||||||
@ -42,9 +42,13 @@ rec {
|
|||||||
foldl' = builtins.foldl' or foldl;
|
foldl' = builtins.foldl' or foldl;
|
||||||
|
|
||||||
|
|
||||||
# map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
|
# Map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
|
||||||
# ["a-1" "b-2"]'
|
# ["a-1" "b-2"]'. FIXME: why does this start to count at 1?
|
||||||
imap = f: list:
|
imap =
|
||||||
|
if builtins ? genList then
|
||||||
|
f: list: genList (n: f (n + 1) (elemAt list n)) (length list)
|
||||||
|
else
|
||||||
|
f: list:
|
||||||
let
|
let
|
||||||
len = length list;
|
len = length list;
|
||||||
imap' = n:
|
imap' = n:
|
||||||
@ -120,7 +124,14 @@ rec {
|
|||||||
|
|
||||||
|
|
||||||
# Return a list of integers from `first' up to and including `last'.
|
# Return a list of integers from `first' up to and including `last'.
|
||||||
range = first: last:
|
range =
|
||||||
|
if builtins ? genList then
|
||||||
|
first: last:
|
||||||
|
if first > last
|
||||||
|
then []
|
||||||
|
else genList (n: first + n) (last - first + 1)
|
||||||
|
else
|
||||||
|
first: last:
|
||||||
if last < first
|
if last < first
|
||||||
then []
|
then []
|
||||||
else [first] ++ range (first + 1) last;
|
else [first] ++ range (first + 1) last;
|
||||||
@ -136,11 +147,13 @@ rec {
|
|||||||
) { right = []; wrong = []; };
|
) { right = []; wrong = []; };
|
||||||
|
|
||||||
|
|
||||||
zipListsWith = f: fst: snd:
|
zipListsWith =
|
||||||
|
if builtins ? genList then
|
||||||
|
f: fst: snd: genList (n: f (elemAt fst n) (elemAt snd n)) (min (length fst) (length snd))
|
||||||
|
else
|
||||||
|
f: fst: snd:
|
||||||
let
|
let
|
||||||
len1 = length fst;
|
len = min (length fst) (length snd);
|
||||||
len2 = length snd;
|
|
||||||
len = if len1 < len2 then len1 else len2;
|
|
||||||
zipListsWith' = n:
|
zipListsWith' = n:
|
||||||
if n != len then
|
if n != len then
|
||||||
[ (f (elemAt fst n) (elemAt snd n)) ]
|
[ (f (elemAt fst n) (elemAt snd n)) ]
|
||||||
@ -151,8 +164,12 @@ rec {
|
|||||||
zipLists = zipListsWith (fst: snd: { inherit fst snd; });
|
zipLists = zipListsWith (fst: snd: { inherit fst snd; });
|
||||||
|
|
||||||
|
|
||||||
# Reverse the order of the elements of a list. FIXME: O(n^2)!
|
# Reverse the order of the elements of a list.
|
||||||
reverseList = fold (e: acc: acc ++ [ e ]) [];
|
reverseList =
|
||||||
|
if builtins ? genList then
|
||||||
|
xs: let l = length xs; in genList (n: elemAt xs (l - n - 1)) l
|
||||||
|
else
|
||||||
|
fold (e: acc: acc ++ [ e ]) [];
|
||||||
|
|
||||||
|
|
||||||
# Sort a list based on a comparator function which compares two
|
# Sort a list based on a comparator function which compares two
|
||||||
@ -177,7 +194,11 @@ rec {
|
|||||||
|
|
||||||
|
|
||||||
# Return the first (at most) N elements of a list.
|
# Return the first (at most) N elements of a list.
|
||||||
take = count: list:
|
take =
|
||||||
|
if builtins ? genList then
|
||||||
|
count: sublist 0 count
|
||||||
|
else
|
||||||
|
count: list:
|
||||||
let
|
let
|
||||||
len = length list;
|
len = length list;
|
||||||
take' = n:
|
take' = n:
|
||||||
@ -189,7 +210,11 @@ rec {
|
|||||||
|
|
||||||
|
|
||||||
# Remove the first (at most) N elements of a list.
|
# Remove the first (at most) N elements of a list.
|
||||||
drop = count: list:
|
drop =
|
||||||
|
if builtins ? genList then
|
||||||
|
count: list: sublist count (length list) list
|
||||||
|
else
|
||||||
|
count: list:
|
||||||
let
|
let
|
||||||
len = length list;
|
len = length list;
|
||||||
drop' = n:
|
drop' = n:
|
||||||
@ -200,6 +225,17 @@ rec {
|
|||||||
in drop' (len - 1);
|
in drop' (len - 1);
|
||||||
|
|
||||||
|
|
||||||
|
# Return a list consisting of at most ‘count’ elements of ‘list’,
|
||||||
|
# starting at index ‘start’.
|
||||||
|
sublist = start: count: list:
|
||||||
|
let len = length list; in
|
||||||
|
genList
|
||||||
|
(n: elemAt list (n + start))
|
||||||
|
(if start >= len then 0
|
||||||
|
else if start + count > len then len - start
|
||||||
|
else count);
|
||||||
|
|
||||||
|
|
||||||
# Return the last element of a list.
|
# Return the last element of a list.
|
||||||
last = list:
|
last = list:
|
||||||
assert list != []; elemAt list (length list - 1);
|
assert list != []; elemAt list (length list - 1);
|
||||||
@ -211,9 +247,11 @@ rec {
|
|||||||
|
|
||||||
deepSeqList = xs: y: if any (x: deepSeq x false) xs then y else y;
|
deepSeqList = xs: y: if any (x: deepSeq x false) xs then y else y;
|
||||||
|
|
||||||
|
|
||||||
crossLists = f: foldl (fs: args: concatMap (f: map f args) fs) [f];
|
crossLists = f: foldl (fs: args: concatMap (f: map f args) fs) [f];
|
||||||
|
|
||||||
# Remove duplicate elements from the list
|
|
||||||
|
# Remove duplicate elements from the list. O(n^2) complexity.
|
||||||
unique = list:
|
unique = list:
|
||||||
if list == [] then
|
if list == [] then
|
||||||
[]
|
[]
|
||||||
@ -223,9 +261,12 @@ rec {
|
|||||||
xs = unique (drop 1 list);
|
xs = unique (drop 1 list);
|
||||||
in [x] ++ remove x xs;
|
in [x] ++ remove x xs;
|
||||||
|
|
||||||
# Intersects list 'e' and another list
|
|
||||||
|
# Intersects list 'e' and another list. O(nm) complexity.
|
||||||
intersectLists = e: filter (x: elem x e);
|
intersectLists = e: filter (x: elem x e);
|
||||||
|
|
||||||
# Subtracts list 'e' from another list
|
|
||||||
|
# Subtracts list 'e' from another list. O(nm) complexity.
|
||||||
subtractLists = e: filter (x: !(elem x e));
|
subtractLists = e: filter (x: !(elem x e));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user