lib.lists: list manipulation functions

General list operations.

lib.lists.singleton

Create a list consisting of a single element. singleton x is sometimes more convenient with respect to indentation than [x] when x spans multiple lines.

Inputs

x

1. Function argument

Type

singleton :: a -> [a]

Examples

Example

lib.lists.singleton usage example

singleton "foo"
=> [ "foo" ]

Located at lib/lists.nix:59 in <nixpkgs>.

lib.lists.forEach

Apply the function to each element in the list. Same as map, but arguments flipped.

Inputs

xs

1. Function argument

f

2. Function argument

Type

forEach :: [a] -> (a -> b) -> [b]

Examples

Example

lib.lists.forEach usage example

forEach [ 1 2 ] (x:
  toString x
)
=> [ "1" "2" ]

Located at lib/lists.nix:94 in <nixpkgs>.

lib.lists.foldr

“right fold” a binary function op between successive elements of list with nul as the starting value, i.e., foldr op nul [x_1 x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul)).

Inputs

op

1. Function argument

nul

2. Function argument

list

3. Function argument

Type

foldr :: (a -> b -> b) -> b -> [a] -> b

Examples

Example

lib.lists.foldr usage example

concat = foldr (a: b: a + b) "z"
concat [ "a" "b" "c" ]
=> "abcz"
# different types
strange = foldr (int: str: toString (int + 1) + str) "a"
strange [ 1 2 3 4 ]
=> "2345a"

Located at lib/lists.nix:137 in <nixpkgs>.

lib.lists.fold

fold is an alias of foldr for historic reasons.

Warning

This function will be removed in 26.05.

Located at lib/lists.nix:152 in <nixpkgs>.

lib.lists.foldl

“left fold”, like foldr, but from the left:

foldl op nul [x_1 x_2 ... x_n] == op (... (op (op nul x_1) x_2) ... x_n).

Inputs

op

1. Function argument

nul

2. Function argument

list

3. Function argument

Type

foldl :: (b -> a -> b) -> b -> [a] -> b

Examples

Example

lib.lists.foldl usage example

lconcat = foldl (a: b: a + b) "z"
lconcat [ "a" "b" "c" ]
=> "zabc"
# different types
lstrange = foldl (str: int: str + toString (int + 1)) "a"
lstrange [ 1 2 3 4 ]
=> "a2345"

Located at lib/lists.nix:195 in <nixpkgs>.

lib.lists.foldl'

Reduce a list by applying a binary operator from left to right, starting with an initial accumulator.

Before each application of the operator, the accumulator value is evaluated. This behavior makes this function stricter than foldl.

Unlike builtins.foldl', the initial accumulator argument is evaluated before the first iteration.

A call like

foldl' op acc₀ [ x₀ x₁ x₂ ... xₙ₋₁ xₙ ]

is (denotationally) equivalent to the following, but with the added benefit that foldl' itself will never overflow the stack.

let
  acc₁   = builtins.seq acc₀   (op acc₀   x₀  );
  acc₂   = builtins.seq acc₁   (op acc₁   x₁  );
  acc₃   = builtins.seq acc₂   (op acc₂   x₂  );
  ...
  accₙ   = builtins.seq accₙ₋₁ (op accₙ₋₁ xₙ₋₁);
  accₙ₊₁ = builtins.seq accₙ   (op accₙ   xₙ  );
in
accₙ₊₁

# Or ignoring builtins.seq
op (op (... (op (op (op acc₀ x₀) x₁) x₂) ...) xₙ₋₁) xₙ

Inputs

op

The binary operation to run, where the two arguments are:

  1. acc: The current accumulator value: Either the initial one for the first iteration, or the result of the previous iteration
  2. x: The corresponding list element for this iteration

acc

The initial accumulator value.

The accumulator value is evaluated in any case before the first iteration starts.

To avoid evaluation even before the list argument is given an eta expansion can be used:

list: lib.foldl' op acc list

list

The list to fold

Type

foldl' :: (a -> b -> a) -> a -> [b] -> a

Examples

Example

lib.lists.foldl' usage example

foldl' (acc: x: acc + x) 0 [1 2 3]
=> 6

Located at lib/lists.nix:278 in <nixpkgs>.

lib.lists.imap0

Map with index starting from 0

Inputs

f

1. Function argument

list

2. Function argument

Type

imap0 :: (Int -> a -> b) -> [a] -> [b]

Examples

Example

lib.lists.imap0 usage example

imap0 (i: v: "${v}-${toString i}") ["a" "b"]
=> [ "a-0" "b-1" ]

Located at lib/lists.nix:318 in <nixpkgs>.

lib.lists.imap1

Map with index starting from 1

Inputs

f

1. Function argument

list

2. Function argument

Type

imap1 :: (Int -> a -> b) -> [a] -> [b]

Examples

Example

lib.lists.imap1 usage example

imap1 (i: v: "${v}-${toString i}") ["a" "b"]
=> [ "a-1" "b-2" ]

Located at lib/lists.nix:350 in <nixpkgs>.

lib.lists.ifilter0

Filter a list for elements that satisfy a predicate function. The predicate function is called with both the index and value for each element. It must return true/false to include/exclude a given element in the result. This function is strict in the result of the predicate function for each element. This function has O(n) complexity.

Also see builtins.filter (available as lib.lists.filter), which can be used instead when the index isn't needed.

Inputs

ipred

The predicate function, it takes two arguments:

    1. (int): the index of the element.
    1. (a): the value of the element.

It must return true/false to include/exclude a given element from the result.

list

The list to filter using the predicate.

Type

ifilter0 :: (Int -> a -> Bool) -> [a] -> [a]

Examples

Example

lib.lists.ifilter0 usage example

ifilter0 (i: v: i == 0 || v > 2) [ 1 2 3 ]
=> [ 1 3 ]

Located at lib/lists.nix:391 in <nixpkgs>.

lib.lists.concatMap

Map and concatenate the result.

Type

concatMap :: (a -> [b]) -> [a] -> [b]

Examples

Example

lib.lists.concatMap usage example

concatMap (x: [x] ++ ["z"]) ["a" "b"]
=> [ "a" "z" "b" "z" ]

Located at lib/lists.nix:417 in <nixpkgs>.

lib.lists.flatten

Flatten the argument into a single list; that is, nested lists are spliced into the top-level lists.

Inputs

x

1. Function argument

Type

flatten :: [a | [a | [a | ...]]] -> [a]

Examples

Example

lib.lists.flatten usage example

flatten [1 [2 [3] 4] 5]
=> [1 2 3 4 5]
flatten 1
=> [1]

Located at lib/lists.nix:448 in <nixpkgs>.

lib.lists.remove

Remove elements equal to e from a list. Useful for buildInputs.

Inputs

e

Element to remove from list

list

The list

Type

remove :: a -> [a] -> [a]

Examples

Example

lib.lists.remove usage example

remove 3 [ 1 3 4 3 ]
=> [ 1 4 ]

Located at lib/lists.nix:480 in <nixpkgs>.

lib.lists.findSingle

Find the sole element in the list matching the specified predicate.

Returns default if no such element exists, or multiple if there are multiple matching elements.

Inputs

pred

Predicate

default

Default value to return if element was not found.

multiple

Default value to return if more than one element was found

list

Input list

Type

findSingle :: (a -> Bool) -> a -> a -> [a] -> a

Examples

Example

lib.lists.findSingle usage example

findSingle (x: x == 3) "none" "multiple" [ 1 3 3 ]
=> "multiple"
findSingle (x: x == 3) "none" "multiple" [ 1 3 ]
=> 3
findSingle (x: x == 3) "none" "multiple" [ 1 9 ]
=> "none"

Located at lib/lists.nix:528 in <nixpkgs>.

lib.lists.findFirstIndex

Find the first index in the list matching the specified predicate or return default if no such element exists.

Inputs

pred

Predicate

default

Default value to return

list

Input list

Type

findFirstIndex :: (a -> Bool) -> b -> [a] -> (Int | b)

Examples

Example

lib.lists.findFirstIndex usage example

findFirstIndex (x: x > 3) null [ 0 6 4 ]
=> 1
findFirstIndex (x: x > 9) null [ 0 6 4 ]
=> null

Located at lib/lists.nix:578 in <nixpkgs>.

lib.lists.findFirst

Find the first element in the list matching the specified predicate or return default if no such element exists.

Inputs

pred

Predicate

default

Default value to return

list

Input list

Type

findFirst :: (a -> Bool) -> a -> [a] -> a

Examples

Example

lib.lists.findFirst usage example

findFirst (x: x > 3) 7 [ 1 6 4 ]
=> 6
findFirst (x: x > 9) 7 [ 1 6 4 ]
=> 7

Located at lib/lists.nix:648 in <nixpkgs>.

lib.lists.any

Returns true if function pred returns true for at least one element of list.

Inputs

pred

Predicate

list

Input list

Type

any :: (a -> Bool) -> [a] -> Bool

Examples

Example

lib.lists.any usage example

any isString [ 1 "a" { } ]
=> true
any isString [ 1 { } ]
=> false

Located at lib/lists.nix:688 in <nixpkgs>.

lib.lists.all

Returns true if function pred returns true for all elements of list.

Inputs

pred

Predicate

list

Input list

Type

all :: (a -> Bool) -> [a] -> Bool

Examples

Example

lib.lists.all usage example

all (x: x < 3) [ 1 2 ]
=> true
all (x: x < 3) [ 1 2 3 ]
=> false

Located at lib/lists.nix:723 in <nixpkgs>.

lib.lists.count

Count how many elements of list match the supplied predicate function.

Inputs

pred

Predicate

Type

count :: (a -> Bool) -> [a] -> Int

Examples

Example

lib.lists.count usage example

count (x: x == 3) [ 3 2 3 4 6 ]
=> 2

Located at lib/lists.nix:752 in <nixpkgs>.

lib.lists.optional

Return a singleton list or an empty list, depending on a boolean value. Useful when building lists with optional elements (e.g. ++ optional (system == "i686-linux") firefox).

Inputs

cond

1. Function argument

elem

2. Function argument

Type

optional :: Bool -> a -> [a]

Examples

Example

lib.lists.optional usage example

optional true "foo"
=> [ "foo" ]
optional false "foo"
=> [ ]

Located at lib/lists.nix:788 in <nixpkgs>.

lib.lists.optionals

Returns a list or an empty list, depending on a boolean value.

Inputs

cond

Condition

elems

List to return if condition is true

Type

optionals :: Bool -> [a] -> [a]

Examples

Example

lib.lists.optionals usage example

optionals true [ 2 3 ]
=> [ 2 3 ]
optionals false [ 2 3 ]
=> [ ]

Located at lib/lists.nix:822 in <nixpkgs>.

lib.lists.toList

If argument is a list, return it; else, wrap it in a singleton list. If you're using this, you should almost certainly reconsider if there isn't a more "well-typed" approach.

Inputs

x

1. Function argument

Type

toList :: (a | [a]) -> [a]

Examples

Example

lib.lists.toList usage example

toList [ 1 2 ]
=> [ 1 2 ]
toList "hi"
=> [ "hi" ]

Located at lib/lists.nix:854 in <nixpkgs>.

lib.lists.range

Returns a list of integers from first up to and including last.

Inputs

first

First integer in the range

last

Last integer in the range

Type

range :: Int -> Int -> [Int]

Examples

Example

lib.lists.range usage example

range 2 4
=> [ 2 3 4 ]
range 3 2
=> [ ]

Located at lib/lists.nix:888 in <nixpkgs>.

lib.lists.replicate

Returns a list with n copies of an element.

Inputs

n

1. Function argument

elem

2. Function argument

Type

replicate :: Int -> a -> [a]

Examples

Example

lib.lists.replicate usage example

replicate 3 "a"
=> [ "a" "a" "a" ]
replicate 2 true
=> [ true true ]

Located at lib/lists.nix:922 in <nixpkgs>.

lib.lists.partition

Splits the elements of a list in two lists, right and wrong, depending on the evaluation of a predicate.

Inputs

pred

Predicate

list

Input list

Type

partition :: (a -> Bool) -> [a] -> { right :: [a]; wrong :: [a]; }

Examples

Example

lib.lists.partition usage example

partition (x: x > 2) [ 5 1 2 3 4 ]
=> { right = [ 5 3 4 ]; wrong = [ 1 2 ]; }

Located at lib/lists.nix:955 in <nixpkgs>.

lib.lists.groupBy'

Splits the elements of a list into many lists, using the return value of a predicate. Predicate should return a string which becomes keys of attrset groupBy returns. groupBy' allows to customise the combining function and initial value

Inputs

op

1. Function argument

nul

2. Function argument

pred

3. Function argument

lst

4. Function argument

Type

groupBy' :: (a -> b -> a) -> a -> (b -> String) -> [b] -> { [String] :: a }

Examples

Example

lib.lists.groupBy' usage example

groupBy (x: boolToString (x > 2)) [ 5 1 2 3 4 ]
=> { true = [ 5 3 4 ]; false = [ 1 2 ]; }
groupBy (x: x.name) [ {name = "icewm"; script = "icewm &";}
                      {name = "xfce";  script = "xfce4-session &";}
                      {name = "icewm"; script = "icewmbg &";}
                      {name = "mate";  script = "gnome-session &";}
                    ]
=> { icewm = [ { name = "icewm"; script = "icewm &"; }
               { name = "icewm"; script = "icewmbg &"; } ];
     mate  = [ { name = "mate";  script = "gnome-session &"; } ];
     xfce  = [ { name = "xfce";  script = "xfce4-session &"; } ];
   }

groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ]
=> { true = 12; false = 3; }

Located at lib/lists.nix:1010 in <nixpkgs>.

lib.lists.zipListsWith

Merges two lists of the same size together. If the sizes aren't the same the merging stops at the shortest. How both lists are merged is defined by the first argument.

Inputs

f

Function to zip elements of both lists

fst

First list

snd

Second list

Type

zipListsWith :: (a -> b -> c) -> [a] -> [b] -> [c]

Examples

Example

lib.lists.zipListsWith usage example

zipListsWith (a: b: a + b) ["h" "l"] ["e" "o"]
=> ["he" "lo"]

Located at lib/lists.nix:1062 in <nixpkgs>.

lib.lists.zipLists

Merges two lists of the same size together. If the sizes aren't the same the merging stops at the shortest.

Inputs

fst

First list

snd

Second list

Type

zipLists :: [a] -> [b] -> [{ fst :: a; snd :: b; }]

Examples

Example

lib.lists.zipLists usage example

zipLists [ 1 2 ] [ "a" "b" ]
=> [ { fst = 1; snd = "a"; } { fst = 2; snd = "b"; } ]

Located at lib/lists.nix:1097 in <nixpkgs>.

lib.lists.reverseList

Reverse the order of the elements of a list.

Inputs

xs

1. Function argument

Type

reverseList :: [a] -> [a]

Examples

Example

lib.lists.reverseList usage example

reverseList [ "b" "o" "j" ]
=> [ "j" "o" "b" ]

Located at lib/lists.nix:1125 in <nixpkgs>.

lib.lists.listDfs

Depth-First Search (DFS) for lists list != [].

before a b == true means that b depends on a (there's an edge from b to a).

Inputs

stopOnCycles

1. Function argument

before

2. Function argument

list

3. Function argument

Type

listDfs :: Bool -> (a -> a -> Bool) -> [a] -> ({ minimal :: a; visited :: [a]; rest :: [a]; } | { cycle :: a; loops :: [a]; visited :: [a]; rest :: [a]; })

Examples

Example

lib.lists.listDfs usage example

listDfs true hasPrefix [ "/home/user" "other" "/" "/home" ]
  == { minimal = "/";                  # minimal element
       visited = [ "/home/user" ];     # seen elements (in reverse order)
       rest    = [ "/home" "other" ];  # everything else
     }

listDfs true hasPrefix [ "/home/user" "other" "/" "/home" "/" ]
  == { cycle   = "/";                  # cycle encountered at this element
       loops   = [ "/" ];              # and continues to these elements
       visited = [ "/" "/home/user" ]; # elements leading to the cycle (in reverse order)
       rest    = [ "/home" "other" ];  # everything else

Located at lib/lists.nix:1179 in <nixpkgs>.

lib.lists.toposort

Sort a list based on a partial ordering using DFS. This implementation is O(N^2), if your ordering is linear, use sort instead.

before a b == true means that b should be after a in the result.

Inputs

before

1. Function argument

list

2. Function argument

Type

toposort :: (a -> a -> Bool) -> [a] -> ({ result :: [a]; } | { cycle :: [a]; loops :: [a]; })

Examples

Example

lib.lists.toposort usage example

toposort hasPrefix [ "/home/user" "other" "/" "/home" ]
  == { result = [ "/" "/home" "/home/user" "other" ]; }

toposort hasPrefix [ "/home/user" "other" "/" "/home" "/" ]
  == { cycle = [ "/home/user" "/" "/" ]; # path leading to a cycle
       loops = [ "/" ]; }                # loops back to these elements

toposort hasPrefix [ "other" "/home/user" "/home" "/" ]
  == { result = [ "other" "/" "/home" "/home/user" ]; }

toposort (a: b: a < b) [ 3 2 1 ] == { result = [ 1 2 3 ]; }

Located at lib/lists.nix:1250 in <nixpkgs>.

lib.lists.sort

Sort a list based on a comparator function which compares two elements and returns true if the first argument is strictly below the second argument. The returned list is sorted in an increasing order. The implementation does a quick-sort.

See also sortOn, which applies the default comparison on a function-derived property, and may be more efficient.

Inputs

comparator

1. Function argument

list

2. Function argument

Type

sort :: (a -> a -> Bool) -> [a] -> [a]

Examples

Example

lib.lists.sort usage example

sort (p: q: p < q) [ 5 3 7 ]
=> [ 3 5 7 ]

Located at lib/lists.nix:1315 in <nixpkgs>.

lib.lists.sortOn

Sort a list based on the default comparison of a derived property b.

The items are returned in b-increasing order.

Performance:

The passed function f is only evaluated once per item, unlike an unprepared sort using f p < f q.

Laws:

sortOn f == sort (p: q: f p < f q)

Inputs

f

1. Function argument

list

2. Function argument

Type

sortOn :: (a -> b) -> [a] -> [a], for comparable b

Examples

Example

lib.lists.sortOn usage example

sortOn stringLength [ "aa" "b" "cccc" ]
=> [ "b" "aa" "cccc" ]

Located at lib/lists.nix:1360 in <nixpkgs>.

lib.lists.compareLists

Compare two lists element-by-element with a comparison function cmp.

List elements are compared pairwise in order by the provided comparison function cmp, the first non-equal pair of elements determines the result.

Note

The < operator can also be used to compare lists using a boolean condition. (e.g. [1 2] < [1 3] is true). See also language operators for more information.

Inputs

cmp

The comparison function a: b: ... must return:

  • 0 if a and b are equal
  • 1 if a is greater than b
  • -1 if a is less than b

See lib.compare for a an example implementation.

a

The first list

b

The second list

Type

compareLists :: (a -> a -> Int) -> [a] -> [a] -> Int

Examples

Example

lib.lists.compareLists usage examples

compareLists lib.compare [] []
=> 0
compareLists lib.compare [] [ "a" ]
=> -1
compareLists lib.compare [ "a" ] []
=> 1
compareLists lib.compare [ "a" "b" ] [ "a" "c" ]
=> -1

Located at lib/lists.nix:1430 in <nixpkgs>.

lib.lists.naturalSort

Sort list using "Natural sorting". Numeric portions of strings are sorted in numeric order.

Inputs

lst

1. Function argument

Type

naturalSort :: [String] -> [String]

Examples

Example

lib.lists.naturalSort usage example

naturalSort ["disk11" "disk8" "disk100" "disk9"]
=> ["disk8" "disk9" "disk11" "disk100"]
naturalSort ["10.46.133.149" "10.5.16.62" "10.54.16.25"]
=> ["10.5.16.62" "10.46.133.149" "10.54.16.25"]
naturalSort ["v0.2" "v0.15" "v0.0.9"]
=> [ "v0.0.9" "v0.2" "v0.15" ]

Located at lib/lists.nix:1473 in <nixpkgs>.

lib.lists.take

Returns the first (at most) N elements of a list.

Inputs

count

Number of elements to take

list

Input list

Type

take :: Int -> [a] -> [a]

Examples

Example

lib.lists.take usage example

take 2 [ "a" "b" "c" "d" ]
=> [ "a" "b" ]
take 2 [ ]
=> [ ]

Located at lib/lists.nix:1517 in <nixpkgs>.

lib.lists.takeEnd

Returns the last (at most) N elements of a list.

Inputs

count

Maximum number of elements to pick

list

Input list

Type

takeEnd :: Int -> [a] -> [a]

Examples

Example

lib.lists.takeEnd usage example

takeEnd 2 [ "a" "b" "c" "d" ]
=> [ "c" "d" ]
takeEnd 2 [ ]
=> [ ]

Located at lib/lists.nix:1556 in <nixpkgs>.

lib.lists.drop

Remove the first (at most) N elements of a list.

Inputs

count

Number of elements to drop

list

Input list

Type

drop :: Int -> [a] -> [a]

Examples

Example

lib.lists.drop usage example

drop 2 [ "a" "b" "c" "d" ]
=> [ "c" "d" ]
drop 2 [ ]
=> [ ]

Located at lib/lists.nix:1596 in <nixpkgs>.

lib.lists.dropEnd

Remove the last (at most) N elements of a list.

Inputs

count

Number of elements to drop

list

Input list

Type

dropEnd :: Int -> [a] -> [a]

Examples

Example

lib.lists.dropEnd usage example

  dropEnd 2 [ "a" "b" "c" "d" ]
  => [ "a" "b" ]
  dropEnd 2 [ ]
  => [ ]

Located at lib/lists.nix:1635 in <nixpkgs>.

lib.lists.hasPrefix

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

Inputs

list1

1. Function argument

list2

2. Function argument

Type

hasPrefix :: [a] -> [a] -> Bool

Examples

Example

lib.lists.hasPrefix usage example

hasPrefix [ 1 2 ] [ 1 2 3 4 ]
=> true
hasPrefix [ 0 1 ] [ 1 2 3 4 ]
=> false

Located at lib/lists.nix:1681 in <nixpkgs>.

lib.lists.removePrefix

Remove the first list as a prefix from the second list. Error if the first list isn't a prefix of the second list.

Inputs

list1

1. Function argument

list2

2. Function argument

Type

removePrefix :: [a] -> [a] -> [a]

Examples

Example

lib.lists.removePrefix usage example

removePrefix [ 1 2 ] [ 1 2 3 4 ]
=> [ 3 4 ]
removePrefix [ 0 1 ] [ 1 2 3 4 ]
=> <error>

Located at lib/lists.nix:1716 in <nixpkgs>.

lib.lists.sublist

Returns a list consisting of at most count elements of list, starting at index start.

Inputs

start

Index at which to start the sublist

count

Number of elements to take

list

Input list

Type

sublist :: Int -> Int -> [a] -> [a]

Examples

Example

lib.lists.sublist usage example

sublist 1 3 [ "a" "b" "c" "d" "e" ]
=> [ "b" "c" "d" ]
sublist 1 3 [ ]
=> [ ]

Located at lib/lists.nix:1760 in <nixpkgs>.

lib.lists.commonPrefix

The common prefix of two lists.

Inputs

list1

1. Function argument

list2

2. Function argument

Type

commonPrefix :: [a] -> [a] -> [a]

Examples

Example

lib.lists.commonPrefix usage example

commonPrefix [ 1 2 3 4 5 6 ] [ 1 2 4 8 ]
=> [ 1 2 ]
commonPrefix [ 1 2 3 ] [ 1 2 3 4 5 ]
=> [ 1 2 3 ]
commonPrefix [ 1 2 3 ] [ 4 5 6 ]
=> [ ]

Located at lib/lists.nix:1808 in <nixpkgs>.

lib.lists.last

Returns the last element of a list.

This function throws an error if the list is empty.

Inputs

list

1. Function argument

Type

last :: [a] -> a

Examples

Example

lib.lists.last usage example

last [ 1 2 3 ]
=> 3

Located at lib/lists.nix:1849 in <nixpkgs>.

lib.lists.init

Returns all elements but the last.

This function throws an error if the list is empty.

Inputs

list

1. Function argument

Type

init :: [a] -> [a]

Examples

Example

lib.lists.init usage example

init [ 1 2 3 ]
=> [ 1 2 ]

Located at lib/lists.nix:1882 in <nixpkgs>.

lib.lists.crossLists

Returns the image of the cross product of some lists by a function.

Examples

Example

lib.lists.crossLists usage example

crossLists (x: y: "${toString x}${toString y}") [[1 2] [3 4]]
=> [ "13" "14" "23" "24" ]

If you have an attrset already, consider mapCartesianProduct:

mapCartesianProduct (x: "${toString x.a}${toString x.b}") { a = [1 2]; b = [3 4]; }
=> [ "13" "14" "23" "24" ]

Located at lib/lists.nix:1907 in <nixpkgs>.

lib.lists.unique

Remove duplicate elements from the list. O(n^2) complexity.

Note

If the list only contains strings and order is not important, the complexity can be reduced to O(n log n) by using lib.lists.uniqueStrings instead.

Inputs

list

Input list

Type

unique :: [a] -> [a]

Examples

Example

lib.lists.unique usage example

unique [ 3 2 3 4 ]
=> [ 3 2 4 ]

Located at lib/lists.nix:1939 in <nixpkgs>.

lib.lists.uniqueStrings

Removes duplicate strings from the list. O(n log n) complexity.

Note

Order is not preserved.

All elements of the list must be strings without context.

This function fails when the list contains a non-string element or a string with context. In that case use lib.lists.unique instead.

Inputs

list

List of strings

Type

uniqueStrings :: [String] -> [String]

Examples

Example

lib.lists.uniqueStrings usage example

uniqueStrings [ "foo" "bar" "foo" ]
=> [ "bar" "foo" ] # order is not preserved

Located at lib/lists.nix:1976 in <nixpkgs>.

lib.lists.allUnique

Check if list contains only unique elements. O(n^2) complexity.

Inputs

list

1. Function argument

Type

allUnique :: [a] -> Bool

Examples

Example

lib.lists.allUnique usage example

allUnique [ 3 2 3 4 ]
=> false
allUnique [ 3 2 4 1 ]
=> true

Located at lib/lists.nix:2006 in <nixpkgs>.

lib.lists.intersectLists

Intersects list list1 and another list (list2).

O(nm) complexity.

Inputs

list1

First list

list2

Second list

Type

intersectLists :: [a] -> [a] -> [a]

Examples

Example

lib.lists.intersectLists usage example

intersectLists [ 1 2 3 ] [ 6 3 2 ]
=> [ 3 2 ]

Located at lib/lists.nix:2040 in <nixpkgs>.

lib.lists.subtractLists

Subtracts list e from another list (list2).

O(nm) complexity.

Inputs

e

First list

list2

Second list

Type

subtractLists :: [a] -> [a] -> [a]

Examples

Example

lib.lists.subtractLists usage example

subtractLists [ 3 2 ] [ 1 2 3 4 5 3 ]
=> [ 1 4 5 ]

Located at lib/lists.nix:2074 in <nixpkgs>.

lib.lists.mutuallyExclusive

Test if two lists have no common element. It should be slightly more efficient than intersectLists a b == [].

Inputs

a

1. Function argument

b

2. Function argument

Type

mutuallyExclusive :: [a] -> [a] -> Bool

Located at lib/lists.nix:2096 in <nixpkgs>.

lib.lists.concatAttrValues

Concatenate all attributes of an attribute set. This assumes that every attribute of the set is a list.

Inputs

set

Attribute set with attributes that are lists

Type

concatAttrValues :: { [String] :: [a] } -> [a]

Examples

Example

lib.concatAttrValues usage example

concatAttrValues { a = [ 1 2 ]; b = [ 3 ]; }
=> [ 1 2 3 ]

Located at lib/lists.nix:2125 in <nixpkgs>.

lib.lists.replaceElemAt

Replaces a list's nth element with a new element

Inputs

list

Input list

idx

index to replace

newElem

new element to replace with

Type

replaceElemAt :: [a] -> Int -> a -> [a]

Examples

Example

replaceElemAt usage example

lib.replaceElemAt` [1 2 3] 0 "a"
=> ["a" 2 3]

Located at lib/lists.nix:2158 in <nixpkgs>.