| 1 | ---
|
| 2 | title: Builtin Functions (Oils Reference)
|
| 3 | all_docs_url: ..
|
| 4 | body_css_class: width40
|
| 5 | default_highlighter: oils-sh
|
| 6 | preserve_anchor_case: yes
|
| 7 | ---
|
| 8 |
|
| 9 | <div class="doc-ref-header">
|
| 10 |
|
| 11 | [Oils Reference](index.html) —
|
| 12 | Chapter **Builtin Functions**
|
| 13 |
|
| 14 | </div>
|
| 15 |
|
| 16 | This chapter describes builtin functions (as opposed to [builtin
|
| 17 | commands](chap-builtin-cmd.html).)
|
| 18 |
|
| 19 | <span class="in-progress">(in progress)</span>
|
| 20 |
|
| 21 | <div id="dense-toc">
|
| 22 | </div>
|
| 23 |
|
| 24 | ## Values
|
| 25 |
|
| 26 | ### len()
|
| 27 |
|
| 28 | Returns the
|
| 29 |
|
| 30 | - number of entries in a `List`
|
| 31 | - number of pairs in a `Dict`
|
| 32 | - number of bytes in a `Str`
|
| 33 | - TODO: `countRunes()` can return the number of UTF-8 encoded code points.
|
| 34 |
|
| 35 | ### func/type()
|
| 36 |
|
| 37 | Given an arbitrary value, returns a string representing the value's runtime
|
| 38 | type.
|
| 39 |
|
| 40 | For example:
|
| 41 |
|
| 42 | var d = {'foo': 'bar'}
|
| 43 | var n = 1337
|
| 44 |
|
| 45 | $ = type(d)
|
| 46 | (Str) 'Dict'
|
| 47 |
|
| 48 | $ = type(n)
|
| 49 | (Str) 'Int'
|
| 50 |
|
| 51 | Similar names: [type][]
|
| 52 |
|
| 53 | [type]: chap-index.html#type
|
| 54 |
|
| 55 | ### repeat()
|
| 56 |
|
| 57 | TODO:
|
| 58 |
|
| 59 | = repeat('a', 3)
|
| 60 | (Str) 'aaa'
|
| 61 |
|
| 62 | = repeat(['a'], 3)
|
| 63 | (List) ['a', 'a', 'a']
|
| 64 |
|
| 65 | Note that list elements are NOT copied. They are repeated by reference, which
|
| 66 | means the List can have aliases.
|
| 67 |
|
| 68 | = repeat([[42]], 3)
|
| 69 | (List) [[42], [42], [42]]
|
| 70 |
|
| 71 | Modeled after these Python expressions:
|
| 72 |
|
| 73 | >>> 'a' * 3
|
| 74 | 'aaa'
|
| 75 | >>> ['a'] * 3
|
| 76 | ['a', 'a', 'a']
|
| 77 |
|
| 78 |
|
| 79 | ## Conversions
|
| 80 |
|
| 81 | ### bool()
|
| 82 |
|
| 83 | Returns the truth value of its argument. Similar to `bool()` in python, it
|
| 84 | returns `false` for:
|
| 85 |
|
| 86 | - `false`, `0`, `0.0`, `''`, `{}`, `[]`, and `null`.
|
| 87 |
|
| 88 | Returns `true` for all other values.
|
| 89 |
|
| 90 | ### int()
|
| 91 |
|
| 92 | Given a float, returns the largest integer that is less than its argument (i.e. `floor()`).
|
| 93 |
|
| 94 | $ = int(1.99)
|
| 95 | (Int) 1
|
| 96 |
|
| 97 | Given a string, `Int()` will attempt to convert the string to a base-10
|
| 98 | integer. The base can be overriden by calling with a second argument.
|
| 99 |
|
| 100 | $ = int('10')
|
| 101 | (Int) 10
|
| 102 |
|
| 103 | $ = int('10', 2)
|
| 104 | (Int) 2
|
| 105 |
|
| 106 | ysh$ = Int('foo')
|
| 107 | # fails with an expression error
|
| 108 |
|
| 109 | ### float()
|
| 110 |
|
| 111 | Given an integer, returns the corressponding flaoting point representation.
|
| 112 |
|
| 113 | $ = float(1)
|
| 114 | (Float) 1.0
|
| 115 |
|
| 116 | Given a string, `Float()` will attempt to convert the string to float.
|
| 117 |
|
| 118 | $ = float('1.23')
|
| 119 | (Float) 1.23
|
| 120 |
|
| 121 | ysh$ = float('bar')
|
| 122 | # fails with an expression error
|
| 123 |
|
| 124 | ### str()
|
| 125 |
|
| 126 | Converts a `Float` or `Int` to a string.
|
| 127 |
|
| 128 | ### list()
|
| 129 |
|
| 130 | Given a list, returns a shallow copy of the original.
|
| 131 |
|
| 132 | Given an iterable value (e.g. a range or dictionary), returns a list containing
|
| 133 | one element for each item in the original collection.
|
| 134 |
|
| 135 | $ = list({'a': 1, 'b': 2})
|
| 136 | (List) ['a', 'b']
|
| 137 |
|
| 138 | $ = list(1:5)
|
| 139 | (List) [1, 2, 3, 4, 5]
|
| 140 |
|
| 141 | ### dict()
|
| 142 |
|
| 143 | Given a dictionary, returns a shallow copy of the original.
|
| 144 |
|
| 145 | ### chr()
|
| 146 |
|
| 147 | (not implemented)
|
| 148 |
|
| 149 | Convert an integer to a Str with the corresponding UTF-8 encoded code point.
|
| 150 |
|
| 151 | Integers in the surrogate range are an error.
|
| 152 |
|
| 153 | = chr(97)
|
| 154 | (Str) 'a'
|
| 155 |
|
| 156 | = chr(0x3bc)
|
| 157 | (Str) 'μ'
|
| 158 |
|
| 159 | ### ord()
|
| 160 |
|
| 161 | (not implemented)
|
| 162 |
|
| 163 | Convert a single UTF-8 encoded code point to an integer.
|
| 164 |
|
| 165 | = ord('a')
|
| 166 | (Int) 97
|
| 167 |
|
| 168 | = ord('μ')
|
| 169 | (Int) 956 # same as 0x3bc
|
| 170 |
|
| 171 | <!-- Do we have character literals like #'a' ? Or just use strings. Small
|
| 172 | string optimization helps. -->
|
| 173 |
|
| 174 | ### runes()
|
| 175 |
|
| 176 | TODO: Explicit iterator over runes.
|
| 177 |
|
| 178 | ## Str
|
| 179 |
|
| 180 | ### strcmp()
|
| 181 |
|
| 182 | TODO
|
| 183 |
|
| 184 | ### split()
|
| 185 |
|
| 186 | TODO
|
| 187 |
|
| 188 | If no argument is passed, splits by whitespace
|
| 189 |
|
| 190 | <!-- respecting Unicode space? -->
|
| 191 |
|
| 192 | If a delimiter Str with a single byte is given, splits by that byte.
|
| 193 |
|
| 194 | Modes:
|
| 195 |
|
| 196 | - Python-like algorithm
|
| 197 | - Is awk any different?
|
| 198 | - Split by eggex
|
| 199 |
|
| 200 | ### shSplit()
|
| 201 |
|
| 202 | Split a string into a List of strings, using the shell algorithm that respects
|
| 203 | `$IFS`.
|
| 204 |
|
| 205 | Prefer `split()` to `shSplit()`.
|
| 206 |
|
| 207 |
|
| 208 | ## List
|
| 209 |
|
| 210 | ### join()
|
| 211 |
|
| 212 | Given a List, stringify its items, and join them by a separator. The default
|
| 213 | separator is the empty string.
|
| 214 |
|
| 215 | var x = ['a', 'b', 'c']
|
| 216 |
|
| 217 | $ echo $[join(x)]
|
| 218 | abc
|
| 219 |
|
| 220 | $ echo $[join(x, ' ')] # optional separator
|
| 221 | a b c
|
| 222 |
|
| 223 |
|
| 224 | It's also often called with the `=>` chaining operator:
|
| 225 |
|
| 226 | var items = [1, 2, 3]
|
| 227 |
|
| 228 | json write (items => join()) # => "123"
|
| 229 | json write (items => join(' ')) # => "1 2 3"
|
| 230 | json write (items => join(', ')) # => "1, 2, 3"
|
| 231 |
|
| 232 |
|
| 233 | ### any()
|
| 234 |
|
| 235 | Returns true if any value in the list is truthy (`x` is truthy if `Bool(x)`
|
| 236 | returns true).
|
| 237 |
|
| 238 | If the list is empty, return false.
|
| 239 |
|
| 240 | = any([]) # => false
|
| 241 | = any([true, false]) # => true
|
| 242 | = any([false, false]) # => false
|
| 243 | = any([false, "foo", false]) # => true
|
| 244 |
|
| 245 | Note, you will need to `source --builtin list.ysh` to use this function.
|
| 246 |
|
| 247 | ### all()
|
| 248 |
|
| 249 | Returns true if all values in the list are truthy (`x` is truthy if `Bool(x)`
|
| 250 | returns true).
|
| 251 |
|
| 252 | If the list is empty, return true.
|
| 253 |
|
| 254 | = any([]) # => true
|
| 255 | = any([true, true]) # => true
|
| 256 | = any([false, true]) # => false
|
| 257 | = any(["foo", true, true]) # => true
|
| 258 |
|
| 259 | Note, you will need to `source --builtin list.ysh` to use this function.
|
| 260 |
|
| 261 | ## Word
|
| 262 |
|
| 263 | ### glob()
|
| 264 |
|
| 265 | See `glob-pat` topic for syntax.
|
| 266 |
|
| 267 | ### maybe()
|
| 268 |
|
| 269 | ## Math
|
| 270 |
|
| 271 | ### abs()
|
| 272 |
|
| 273 | Compute the absolute (positive) value of a number (float or int).
|
| 274 |
|
| 275 | = abs(-1) # => 1
|
| 276 | = abs(0) # => 0
|
| 277 | = abs(1) # => 1
|
| 278 |
|
| 279 | Note, you will need to `source --builtin math.ysh` to use this function.
|
| 280 |
|
| 281 | ### max()
|
| 282 |
|
| 283 | Compute the maximum of 2 or more values.
|
| 284 |
|
| 285 | `max` takes two different signatures:
|
| 286 |
|
| 287 | 1. `max(a, b)` to return the maximum of `a`, `b`
|
| 288 | 2. `max(list)` to return the greatest item in the `list`
|
| 289 |
|
| 290 | For example:
|
| 291 |
|
| 292 | = max(1, 2) # => 2
|
| 293 | = max([1, 2, 3]) # => 3
|
| 294 |
|
| 295 | Note, you will need to `source --builtin math.ysh` to use this function.
|
| 296 |
|
| 297 | ### min()
|
| 298 |
|
| 299 | Compute the minimum of 2 or more values.
|
| 300 |
|
| 301 | `min` takes two different signatures:
|
| 302 |
|
| 303 | 1. `min(a, b)` to return the minimum of `a`, `b`
|
| 304 | 2. `min(list)` to return the least item in the `list`
|
| 305 |
|
| 306 | For example:
|
| 307 |
|
| 308 | = min(2, 3) # => 2
|
| 309 | = max([1, 2, 3]) # => 1
|
| 310 |
|
| 311 | Note, you will need to `source --builtin math.ysh` to use this function.
|
| 312 |
|
| 313 | ### round()
|
| 314 |
|
| 315 | ### sum()
|
| 316 |
|
| 317 | Computes the sum of all elements in the list.
|
| 318 |
|
| 319 | Returns 0 for an empty list.
|
| 320 |
|
| 321 | = sum([]) # => 0
|
| 322 | = sum([0]) # => 0
|
| 323 | = sum([1, 2, 3]) # => 6
|
| 324 |
|
| 325 | Note, you will need to `source --builtin list.ysh` to use this function.
|
| 326 |
|
| 327 | ## Serialize
|
| 328 |
|
| 329 | ### toJson()
|
| 330 |
|
| 331 | Convert an object in memory to JSON text:
|
| 332 |
|
| 333 | $ = toJson({name: "alice"})
|
| 334 | (Str) '{"name":"alice"}'
|
| 335 |
|
| 336 | Add indentation by passing the `space` param:
|
| 337 |
|
| 338 | $ = toJson([42], space=2)
|
| 339 | (Str) "[\n 42\n]"
|
| 340 |
|
| 341 | Similar to `json write (x)`, except the default value of `space` is 0.
|
| 342 |
|
| 343 | See [err-json-encode][] for errors.
|
| 344 |
|
| 345 | [err-json-encode]: chap-errors.html#err-json-encode
|
| 346 |
|
| 347 | ### fromJson()
|
| 348 |
|
| 349 | Convert JSON text to an object in memory:
|
| 350 |
|
| 351 | = fromJson('{"name":"alice"}')
|
| 352 | (Dict) {"name": "alice"}
|
| 353 |
|
| 354 | Similar to `json read <<< '{"name": "alice"}'`.
|
| 355 |
|
| 356 | See [err-json-decode][] for errors.
|
| 357 |
|
| 358 | [err-json-decode]: chap-errors.html#err-json-decode
|
| 359 |
|
| 360 | ### toJson8()
|
| 361 |
|
| 362 | Like `toJson()`, but it also converts binary data (non-Unicode strings) to
|
| 363 | J8-style `b'foo \yff'` strings.
|
| 364 |
|
| 365 | In contrast, `toJson()` will do a lossy conversion with the Unicode replacement
|
| 366 | character.
|
| 367 |
|
| 368 | See [err-json8-encode][] for errors.
|
| 369 |
|
| 370 | [err-json8-encode]: chap-errors.html#err-json8-encode
|
| 371 |
|
| 372 | ### fromJson8()
|
| 373 |
|
| 374 | Like `fromJson()`, but it also accepts binary data denoted by J8-style `b'foo
|
| 375 | \yff'` strings.
|
| 376 |
|
| 377 | See [err-json8-decode][] for errors.
|
| 378 |
|
| 379 | [err-json8-decode]: chap-errors.html#err-json8-decode
|
| 380 |
|
| 381 | ## Pattern
|
| 382 |
|
| 383 | ### `_group()`
|
| 384 |
|
| 385 | Like `Match => group()`, but accesses the global match created by `~`:
|
| 386 |
|
| 387 | if ('foo42' ~ / d+ /) {
|
| 388 | echo $[_group(0)] # => 42
|
| 389 | }
|
| 390 |
|
| 391 | ### `_start()`
|
| 392 |
|
| 393 | Like `Match => start()`, but accesses the global match created by `~`:
|
| 394 |
|
| 395 | if ('foo42' ~ / d+ /) {
|
| 396 | echo $[_start(0)] # => 3
|
| 397 | }
|
| 398 |
|
| 399 | ### `_end()`
|
| 400 |
|
| 401 | Like `Match => end()`, but accesses the global match created by `~`:
|
| 402 |
|
| 403 | if ('foo42' ~ / d+ /) {
|
| 404 | echo $[_end(0)] # => 5
|
| 405 | }
|
| 406 |
|
| 407 | ## Introspection
|
| 408 |
|
| 409 | ### `shvarGet()`
|
| 410 |
|
| 411 | Given a variable name, return its value. It uses the "dynamic scope" rule,
|
| 412 | which looks up the stack for a variable.
|
| 413 |
|
| 414 | It's meant to be used with `shvar`:
|
| 415 |
|
| 416 | proc proc1 {
|
| 417 | shvar PATH=/tmp { # temporarily set PATH in this stack frame
|
| 418 | my-proc
|
| 419 | }
|
| 420 |
|
| 421 | proc2
|
| 422 | }
|
| 423 |
|
| 424 | proc proc2 {
|
| 425 | proc3
|
| 426 | }
|
| 427 |
|
| 428 | proc proc3 {
|
| 429 | var path = shvarGet('PATH') # Look up the stack (dynamic scoping)
|
| 430 | echo $path # => /tmp
|
| 431 | }
|
| 432 |
|
| 433 | proc1
|
| 434 |
|
| 435 | Note that `shvar` is usually for string variables, and is analogous to `shopt`
|
| 436 | for "booleans".
|
| 437 |
|
| 438 | If the variable isn't defined, `shvarGet()` returns `null`. So there's no way
|
| 439 | to distinguish an undefined variable from one that's `null`.
|
| 440 |
|
| 441 | ### `getVar()`
|
| 442 |
|
| 443 | Given a variable name, return its value.
|
| 444 |
|
| 445 | $ var x = 42
|
| 446 | $ echo $[getVar('x')]
|
| 447 | 42
|
| 448 |
|
| 449 | The variable may be local or global. (Compare with `shvarGet()`.) the "dynamic
|
| 450 | scope" rule.)
|
| 451 |
|
| 452 | If the variable isn't defined, `getVar()` returns `null`. So there's no way to
|
| 453 | distinguish an undefined variable from one that's `null`.
|
| 454 |
|
| 455 | ### `evalExpr()`
|
| 456 |
|
| 457 | Given a an expression quotation, evaluate it and return its value:
|
| 458 |
|
| 459 | $ var expr = ^[1 + 2]
|
| 460 |
|
| 461 | $ = evalExpr(expr)
|
| 462 | 3
|
| 463 |
|
| 464 | ## Hay Config
|
| 465 |
|
| 466 | ### parseHay()
|
| 467 |
|
| 468 | ### evalHay()
|
| 469 |
|
| 470 |
|
| 471 | ## Hashing
|
| 472 |
|
| 473 | ### sha1dc()
|
| 474 |
|
| 475 | Git's algorithm.
|
| 476 |
|
| 477 | ### sha256()
|
| 478 |
|
| 479 |
|
| 480 | <!--
|
| 481 |
|
| 482 | ### Better Syntax
|
| 483 |
|
| 484 | These functions give better syntax to existing shell constructs.
|
| 485 |
|
| 486 | - `shQuote()` for `printf %q` and `${x@Q}`
|
| 487 | - `trimLeft()` for `${x#prefix}` and `${x##prefix}`
|
| 488 | - `trimRight()` for `${x%suffix}` and `${x%%suffix}`
|
| 489 | - `trimLeftGlob()` and `trimRightGlob()` for slow, legacy glob
|
| 490 | - `upper()` for `${x^^}`
|
| 491 | - `lower()` for `${x,,}`
|
| 492 | - `strftime()`: hidden in `printf`
|
| 493 |
|
| 494 | -->
|