src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_pred.d.elv (about) 1 # Convert a value to boolean. In Elvish, only `$false`, `$nil`, and errors are 2 # booleanly false. Everything else, including 0, empty strings and empty lists, 3 # is booleanly true: 4 # 5 # ```elvish-transcript 6 # ~> bool $true 7 # ▶ $true 8 # ~> bool $false 9 # ▶ $false 10 # ~> bool $ok 11 # ▶ $true 12 # ~> bool ?(fail haha) 13 # ▶ $false 14 # ~> bool '' 15 # ▶ $true 16 # ~> bool [] 17 # ▶ $true 18 # ~> bool abc 19 # ▶ $true 20 # ``` 21 # 22 # See also [`not`](). 23 fn bool {|value| } 24 25 # Boolean negation. Examples: 26 # 27 # ```elvish-transcript 28 # ~> not $true 29 # ▶ $false 30 # ~> not $false 31 # ▶ $true 32 # ~> not $ok 33 # ▶ $false 34 # ~> not ?(fail error) 35 # ▶ $true 36 # ``` 37 # 38 # **Note**: The related logical commands `and` and `or` are implemented as 39 # [special commands](language.html#special-commands) instead, since they do not 40 # always evaluate all their arguments. The `not` command always evaluates its 41 # only argument, and is thus a normal command. 42 # 43 # See also [`bool`](). 44 fn not {|value| } 45 46 # Determine whether all `$value`s have the same identity. Writes `$true` when 47 # given no or one argument. 48 # 49 # The definition of identity is subject to change. Do not rely on its behavior. 50 # 51 # ```elvish-transcript 52 # ~> is a a 53 # ▶ $true 54 # ~> is a b 55 # ▶ $false 56 # ~> is [] [] 57 # ▶ $true 58 # ~> is [a] [a] 59 # ▶ $false 60 # ``` 61 # 62 # See also [`eq`](). 63 # 64 # Etymology: [Python](https://docs.python.org/3/reference/expressions.html#is). 65 fn is {|@values| } 66 67 # Determines whether all `$value`s are equal. Writes `$true` when 68 # given no or one argument. 69 # 70 # Two values are equal when they have the same type and value. 71 # 72 # For complex data structures like lists and maps, comparison is done 73 # recursively. A pseudo-map is equal to another pseudo-map with the same 74 # internal type (which is not exposed to Elvish code now) and value. 75 # 76 # ```elvish-transcript 77 # ~> eq a a 78 # ▶ $true 79 # ~> eq [a] [a] 80 # ▶ $true 81 # ~> eq [&k=v] [&k=v] 82 # ▶ $true 83 # ~> eq a [b] 84 # ▶ $false 85 # ``` 86 # 87 # See also [`is`]() and [`not-eq`](). 88 # 89 # Etymology: [Perl](https://perldoc.perl.org/perlop.html#Equality-Operators). 90 fn eq {|@values| } 91 92 # Determines whether `$a` and `$b` are not equal. Equivalent to `not (eq $a $b)`. 93 # 94 # ```elvish-transcript 95 # ~> not-eq 1 2 96 # ▶ $true 97 # ~> not-eq 1 1 98 # ▶ $false 99 # ``` 100 # 101 # See also [`eq`](). 102 fn not-eq {|a b| } 103 104 # Outputs the number -1 if `$a` is smaller than `$b`, 0 if `$a` is equal to 105 # `$b`, and 1 if `$a` is greater than `$b`. 106 # 107 # The following algorithm is used: 108 # 109 # 1. If `$a` and `$b` have the same type and that type is listed below, they are 110 # compared accordingly: 111 # 112 # - Booleans: `$false` is smaller than `$true`. 113 # 114 # - Typed numbers: Compared numerically, consistent with [`<`](#num-lt) 115 # and [`<=`](#num-le), except that `NaN` values are considered equal to 116 # each other and smaller than all other numbers. 117 # 118 # - Strings: Compared lexicographically by bytes, consistent with 119 # [`<s`](#str-lt) and [`<=s`](#str-le). For UTF-8 encoded strings, this 120 # is equivalent to comparing by codepoints. 121 # 122 # Beware that strings that look like numbers are compared as strings, 123 # not numbers. 124 # 125 # - Lists: Compared lexicographically by elements, with elements compared 126 # recursively. 127 # 128 # 2. If `eq $a $b` is true, `compare $a $b` outputs the number 0. 129 # 130 # 3. Otherwise the behavior depends on the `&total` option: 131 # 132 # - If it is `$false` (the default), `compare` throws an exception 133 # complaning that the two values can't be compared. 134 # 135 # - If it is `$true`, `compare` compares the _types_ of `$a` and `$b`: if 136 # they have the same type, it outputs 0; if they have different types, 137 # it outputs -1 and 1 depending on which type comes first in an internal 138 # ordering of all types. 139 # 140 # The internal ordering of all types is unspecified, but it is 141 # guaranteed to be consistent during the same Elvish session. For 142 # example, if `compare &total $a $b` outputs -1 when `$a` is a number 143 # and `$b` is a string, it will always output -1 for such pairs. 144 # 145 # This creates an artificial [total 146 # order](https://en.wikipedia.org/wiki/Total_order), which is mainly 147 # useful for sorting values of mixed types. 148 # 149 # Examples comparing values of the same type: 150 # 151 # ```elvish-transcript 152 # ~> compare a b 153 # ▶ (num -1) 154 # ~> compare b a 155 # ▶ (num 1) 156 # ~> compare x x 157 # ▶ (num 0) 158 # ~> compare (num 10) (num 1) 159 # ▶ (num 1) 160 # ``` 161 # 162 # Examples comparing values of different types: 163 # 164 # ```elvish-transcript 165 # //skip-test 166 # ~> compare a (num 10) 167 # Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values 168 # [tty]:1:1-18: compare a (num 10) 169 # ~> compare &total a (num 10) 170 # ▶ (num -1) 171 # ~> compare &total (num 10) a 172 # ▶ (num 1) 173 # ``` 174 # 175 # See also [`order`](). 176 fn compare {|&total=$false a b| }