~aasg/nixexprs

ref: 4b5399b1f065b1ba2703e84ac96827ab9b47c5e7 nixexprs/lib/lists.nix -rw-r--r-- 677 bytes
4b5399b1 — Aluísio Augusto Silva Gonçalves ci: Don't rely on `meta.platforms` being set 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{ ... }:

rec {
  /* Return the index of the first element equal to `x` in the list
   * `xs`.
   *
   * Example:
   *     indexOf "c" ["a" "b" "c" "d" "e"]
   *     => 2
   *
   *     indexOf "g" ["a" "b" "c" "d" "e"]
   *     => -1
   */
  indexOf = x: xs:
    let indexOfRec = i: xs:
      if xs == [ ] then -1
      else if (builtins.head xs) == x then i
      else indexOfRec (i + 1) (builtins.tail xs);
    in indexOfRec 0 xs;

  /* Test if $ys ⊆ xs$, that is, if every element of the list `ys` is
   * also in `xs`.
   *
   * Example:
   *     isSubsetOf [1 2 3 4 5] [1 3 5]
   *     => true
   */
  isSubsetOf = xs: ys:
    builtins.all (y: builtins.elem y xs) ys;
}