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 | ### runes()
|
146 |
|
147 | TODO
|
148 |
|
149 | Given a string, decodes UTF-8 into a List of integer "runes" (aka code points).
|
150 |
|
151 | Each rune is in the range `U+0` to `U+110000`, and **excludes** the surrogate
|
152 | range.
|
153 |
|
154 | runes(s, start=-1, end=-1)
|
155 |
|
156 | TODO: How do we signal errors?
|
157 |
|
158 | (`runes()` can be used to implement implemented Python's `ord()`.)
|
159 |
|
160 | ### encodeRunes()
|
161 |
|
162 | TODO
|
163 |
|
164 | Given a List of integer "runes" (aka code points), return a string.
|
165 |
|
166 | (`encodeRunes()` can be used to implement implemented Python's `chr()`.)
|
167 |
|
168 | ### bytes()
|
169 |
|
170 | TODO
|
171 |
|
172 | Given a string, return a List of integer byte values.
|
173 |
|
174 | Each byte is in the range 0 to 255.
|
175 |
|
176 | ### encodeBytes()
|
177 |
|
178 | TODO
|
179 |
|
180 | Given a List of integer byte values, return a string.
|
181 |
|
182 | ## Str
|
183 |
|
184 | ### strcmp()
|
185 |
|
186 | TODO
|
187 |
|
188 | ### split()
|
189 |
|
190 | TODO
|
191 |
|
192 | If no argument is passed, splits by whitespace
|
193 |
|
194 | <!-- respecting Unicode space? -->
|
195 |
|
196 | If a delimiter Str with a single byte is given, splits by that byte.
|
197 |
|
198 | Modes:
|
199 |
|
200 | - Python-like algorithm
|
201 | - Is awk any different?
|
202 | - Split by eggex
|
203 |
|
204 | ### shSplit()
|
205 |
|
206 | Split a string into a List of strings, using the shell algorithm that respects
|
207 | `$IFS`.
|
208 |
|
209 | Prefer `split()` to `shSplit()`.
|
210 |
|
211 |
|
212 | ## List
|
213 |
|
214 | ### join()
|
215 |
|
216 | Given a List, stringify its items, and join them by a separator. The default
|
217 | separator is the empty string.
|
218 |
|
219 | var x = ['a', 'b', 'c']
|
220 |
|
221 | $ echo $[join(x)]
|
222 | abc
|
223 |
|
224 | $ echo $[join(x, ' ')] # optional separator
|
225 | a b c
|
226 |
|
227 |
|
228 | It's also often called with the `=>` chaining operator:
|
229 |
|
230 | var items = [1, 2, 3]
|
231 |
|
232 | json write (items => join()) # => "123"
|
233 | json write (items => join(' ')) # => "1 2 3"
|
234 | json write (items => join(', ')) # => "1, 2, 3"
|
235 |
|
236 |
|
237 | ### any()
|
238 |
|
239 | Returns true if any value in the list is truthy (`x` is truthy if `Bool(x)`
|
240 | returns true).
|
241 |
|
242 | If the list is empty, return false.
|
243 |
|
244 | = any([]) # => false
|
245 | = any([true, false]) # => true
|
246 | = any([false, false]) # => false
|
247 | = any([false, "foo", false]) # => true
|
248 |
|
249 | Note, you will need to `source --builtin list.ysh` to use this function.
|
250 |
|
251 | ### all()
|
252 |
|
253 | Returns true if all values in the list are truthy (`x` is truthy if `Bool(x)`
|
254 | returns true).
|
255 |
|
256 | If the list is empty, return true.
|
257 |
|
258 | = any([]) # => true
|
259 | = any([true, true]) # => true
|
260 | = any([false, true]) # => false
|
261 | = any(["foo", true, true]) # => true
|
262 |
|
263 | Note, you will need to `source --builtin list.ysh` to use this function.
|
264 |
|
265 | ## Word
|
266 |
|
267 | ### glob()
|
268 |
|
269 | See `glob-pat` topic for syntax.
|
270 |
|
271 | ### maybe()
|
272 |
|
273 | ## Math
|
274 |
|
275 | ### abs()
|
276 |
|
277 | Compute the absolute (positive) value of a number (float or int).
|
278 |
|
279 | = abs(-1) # => 1
|
280 | = abs(0) # => 0
|
281 | = abs(1) # => 1
|
282 |
|
283 | Note, you will need to `source --builtin math.ysh` to use this function.
|
284 |
|
285 | ### max()
|
286 |
|
287 | Compute the maximum of 2 or more values.
|
288 |
|
289 | `max` takes two different signatures:
|
290 |
|
291 | 1. `max(a, b)` to return the maximum of `a`, `b`
|
292 | 2. `max(list)` to return the greatest item in the `list`
|
293 |
|
294 | For example:
|
295 |
|
296 | = max(1, 2) # => 2
|
297 | = max([1, 2, 3]) # => 3
|
298 |
|
299 | Note, you will need to `source --builtin math.ysh` to use this function.
|
300 |
|
301 | ### min()
|
302 |
|
303 | Compute the minimum of 2 or more values.
|
304 |
|
305 | `min` takes two different signatures:
|
306 |
|
307 | 1. `min(a, b)` to return the minimum of `a`, `b`
|
308 | 2. `min(list)` to return the least item in the `list`
|
309 |
|
310 | For example:
|
311 |
|
312 | = min(2, 3) # => 2
|
313 | = max([1, 2, 3]) # => 1
|
314 |
|
315 | Note, you will need to `source --builtin math.ysh` to use this function.
|
316 |
|
317 | ### round()
|
318 |
|
319 | ### sum()
|
320 |
|
321 | Computes the sum of all elements in the list.
|
322 |
|
323 | Returns 0 for an empty list.
|
324 |
|
325 | = sum([]) # => 0
|
326 | = sum([0]) # => 0
|
327 | = sum([1, 2, 3]) # => 6
|
328 |
|
329 | Note, you will need to `source --builtin list.ysh` to use this function.
|
330 |
|
331 | ## Serialize
|
332 |
|
333 | ### toJson()
|
334 |
|
335 | Convert an object in memory to JSON text:
|
336 |
|
337 | $ = toJson({name: "alice"})
|
338 | (Str) '{"name":"alice"}'
|
339 |
|
340 | Add indentation by passing the `space` param:
|
341 |
|
342 | $ = toJson([42], space=2)
|
343 | (Str) "[\n 42\n]"
|
344 |
|
345 | Similar to `json write (x)`, except the default value of `space` is 0.
|
346 |
|
347 | See [err-json-encode][] for errors.
|
348 |
|
349 | [err-json-encode]: chap-errors.html#err-json-encode
|
350 |
|
351 | ### fromJson()
|
352 |
|
353 | Convert JSON text to an object in memory:
|
354 |
|
355 | = fromJson('{"name":"alice"}')
|
356 | (Dict) {"name": "alice"}
|
357 |
|
358 | Similar to `json read <<< '{"name": "alice"}'`.
|
359 |
|
360 | See [err-json-decode][] for errors.
|
361 |
|
362 | [err-json-decode]: chap-errors.html#err-json-decode
|
363 |
|
364 | ### toJson8()
|
365 |
|
366 | Like `toJson()`, but it also converts binary data (non-Unicode strings) to
|
367 | J8-style `b'foo \yff'` strings.
|
368 |
|
369 | In contrast, `toJson()` will do a lossy conversion with the Unicode replacement
|
370 | character.
|
371 |
|
372 | See [err-json8-encode][] for errors.
|
373 |
|
374 | [err-json8-encode]: chap-errors.html#err-json8-encode
|
375 |
|
376 | ### fromJson8()
|
377 |
|
378 | Like `fromJson()`, but it also accepts binary data denoted by J8-style `b'foo
|
379 | \yff'` strings.
|
380 |
|
381 | See [err-json8-decode][] for errors.
|
382 |
|
383 | [err-json8-decode]: chap-errors.html#err-json8-decode
|
384 |
|
385 | ## Pattern
|
386 |
|
387 | ### `_group()`
|
388 |
|
389 | Like `Match => group()`, but accesses the global match created by `~`:
|
390 |
|
391 | if ('foo42' ~ / d+ /) {
|
392 | echo $[_group(0)] # => 42
|
393 | }
|
394 |
|
395 | ### `_start()`
|
396 |
|
397 | Like `Match => start()`, but accesses the global match created by `~`:
|
398 |
|
399 | if ('foo42' ~ / d+ /) {
|
400 | echo $[_start(0)] # => 3
|
401 | }
|
402 |
|
403 | ### `_end()`
|
404 |
|
405 | Like `Match => end()`, but accesses the global match created by `~`:
|
406 |
|
407 | if ('foo42' ~ / d+ /) {
|
408 | echo $[_end(0)] # => 5
|
409 | }
|
410 |
|
411 | ## Introspection
|
412 |
|
413 | ### `shvarGet()`
|
414 |
|
415 | Given a variable name, return its value. It uses the "dynamic scope" rule,
|
416 | which looks up the stack for a variable.
|
417 |
|
418 | It's meant to be used with `shvar`:
|
419 |
|
420 | proc proc1 {
|
421 | shvar PATH=/tmp { # temporarily set PATH in this stack frame
|
422 | my-proc
|
423 | }
|
424 |
|
425 | proc2
|
426 | }
|
427 |
|
428 | proc proc2 {
|
429 | proc3
|
430 | }
|
431 |
|
432 | proc proc3 {
|
433 | var path = shvarGet('PATH') # Look up the stack (dynamic scoping)
|
434 | echo $path # => /tmp
|
435 | }
|
436 |
|
437 | proc1
|
438 |
|
439 | Note that `shvar` is usually for string variables, and is analogous to `shopt`
|
440 | for "booleans".
|
441 |
|
442 | If the variable isn't defined, `shvarGet()` returns `null`. So there's no way
|
443 | to distinguish an undefined variable from one that's `null`.
|
444 |
|
445 | ### `getVar()`
|
446 |
|
447 | Given a variable name, return its value.
|
448 |
|
449 | $ var x = 42
|
450 | $ echo $[getVar('x')]
|
451 | 42
|
452 |
|
453 | The variable may be local or global. (Compare with `shvarGet()`.) the "dynamic
|
454 | scope" rule.)
|
455 |
|
456 | If the variable isn't defined, `getVar()` returns `null`. So there's no way to
|
457 | distinguish an undefined variable from one that's `null`.
|
458 |
|
459 | ### `evalExpr()`
|
460 |
|
461 | Given a an expression quotation, evaluate it and return its value:
|
462 |
|
463 | $ var expr = ^[1 + 2]
|
464 |
|
465 | $ = evalExpr(expr)
|
466 | 3
|
467 |
|
468 | ## Hay Config
|
469 |
|
470 | ### parseHay()
|
471 |
|
472 | ### evalHay()
|
473 |
|
474 |
|
475 | ## Hashing
|
476 |
|
477 | ### sha1dc()
|
478 |
|
479 | Git's algorithm.
|
480 |
|
481 | ### sha256()
|
482 |
|
483 |
|
484 | <!--
|
485 |
|
486 | ### Better Syntax
|
487 |
|
488 | These functions give better syntax to existing shell constructs.
|
489 |
|
490 | - `shQuote()` for `printf %q` and `${x@Q}`
|
491 | - `trimLeft()` for `${x#prefix}` and `${x##prefix}`
|
492 | - `trimRight()` for `${x%suffix}` and `${x%%suffix}`
|
493 | - `trimLeftGlob()` and `trimRightGlob()` for slow, legacy glob
|
494 | - `upper()` for `${x^^}`
|
495 | - `lower()` for `${x,,}`
|
496 | - `strftime()`: hidden in `printf`
|
497 |
|
498 | -->
|