builtin_fn_misc.d.elv 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #elvdoc:fn nop
  2. #
  3. # ```elvish
  4. # nop &any-opt= $value...
  5. # ```
  6. #
  7. # Accepts arbitrary arguments and options and does exactly nothing.
  8. #
  9. # Examples:
  10. #
  11. # ```elvish-transcript
  12. # ~> nop
  13. # ~> nop a b c
  14. # ~> nop &k=v
  15. # ```
  16. #
  17. # Etymology: Various languages, in particular NOP in
  18. # [assembly languages](https://en.wikipedia.org/wiki/NOP).
  19. #elvdoc:fn kind-of
  20. #
  21. # ```elvish
  22. # kind-of $value...
  23. # ```
  24. #
  25. # Output the kinds of `$value`s. Example:
  26. #
  27. # ```elvish-transcript
  28. # ~> kind-of lorem [] [&]
  29. # ▶ string
  30. # ▶ list
  31. # ▶ map
  32. # ```
  33. #
  34. # The terminology and definition of "kind" is subject to change.
  35. #elvdoc:fn constantly
  36. #
  37. # ```elvish
  38. # constantly $value...
  39. # ```
  40. #
  41. # Output a function that takes no arguments and outputs `$value`s when called.
  42. # Examples:
  43. #
  44. # ```elvish-transcript
  45. # ~> var f = (constantly lorem ipsum)
  46. # ~> $f
  47. # ▶ lorem
  48. # ▶ ipsum
  49. # ```
  50. #
  51. # The above example is equivalent to simply `var f = { put lorem ipsum }`;
  52. # it is most useful when the argument is **not** a literal value, e.g.
  53. #
  54. # ```elvish-transcript
  55. # ~> var f = (constantly (uname))
  56. # ~> $f
  57. # ▶ Darwin
  58. # ~> $f
  59. # ▶ Darwin
  60. # ```
  61. #
  62. # The above code only calls `uname` once when defining `$f`. In contrast, if
  63. # `$f` is defined as `var f = { put (uname) }`, every time you invoke `$f`,
  64. # `uname` will be called.
  65. #
  66. # Etymology: [Clojure](https://clojuredocs.org/clojure.core/constantly).
  67. #elvdoc:fn call
  68. #
  69. # ```elvish
  70. # call $fn $args $opts
  71. # ```
  72. #
  73. # Calls `$fn` with `$args` as the arguments, and `$opts` as the option. Useful
  74. # for calling a function with dynamic option keys.
  75. #
  76. # Example:
  77. #
  78. # ```elvish-transcript
  79. # ~> var f = {|a &k1=v1 &k2=v2| put $a $k1 $k2 }
  80. # ~> call $f [foo] [&k1=bar]
  81. # ▶ foo
  82. # ▶ bar
  83. # ▶ v2
  84. # ```
  85. #elvdoc:fn resolve
  86. #
  87. # ```elvish
  88. # resolve $command
  89. # ```
  90. #
  91. # Output what `$command` resolves to in symbolic form. Command resolution is
  92. # described in the [language reference](language.html#ordinary-command).
  93. #
  94. # Example:
  95. #
  96. # ```elvish-transcript
  97. # ~> resolve echo
  98. # ▶ <builtin echo>
  99. # ~> fn f { }
  100. # ~> resolve f
  101. # ▶ <closure 0xc4201c24d0>
  102. # ~> resolve cat
  103. # ▶ <external cat>
  104. # ```
  105. #elvdoc:fn eval
  106. #
  107. # ```elvish
  108. # eval $code &ns=$nil &on-end=$nil
  109. # ```
  110. #
  111. # Evaluates `$code`, which should be a string. The evaluation happens in a
  112. # new, restricted namespace, whose initial set of variables can be specified by
  113. # the `&ns` option. After evaluation completes, the new namespace is passed to
  114. # the callback specified by `&on-end` if it is not nil.
  115. #
  116. # The namespace specified by `&ns` is never modified; it will not be affected
  117. # by the creation or deletion of variables by `$code`. However, the values of
  118. # the variables may be mutated by `$code`.
  119. #
  120. # If the `&ns` option is `$nil` (the default), a temporary namespace built by
  121. # amalgamating the local and upvalue scopes of the caller is used.
  122. #
  123. # If `$code` fails to parse or compile, the parse error or compilation error is
  124. # raised as an exception.
  125. #
  126. # Basic examples that do not modify the namespace or any variable:
  127. #
  128. # ```elvish-transcript
  129. # ~> eval 'put x'
  130. # ▶ x
  131. # ~> var x = foo
  132. # ~> eval 'put $x'
  133. # ▶ foo
  134. # ~> var ns = (ns [&x=bar])
  135. # ~> eval &ns=$ns 'put $x'
  136. # ▶ bar
  137. # ```
  138. #
  139. # Examples that modify existing variables:
  140. #
  141. # ```elvish-transcript
  142. # ~> var y = foo
  143. # ~> eval 'set y = bar'
  144. # ~> put $y
  145. # ▶ bar
  146. # ```
  147. #
  148. # Examples that creates new variables and uses the callback to access it:
  149. #
  150. # ```elvish-transcript
  151. # ~> eval 'var z = lorem'
  152. # ~> put $z
  153. # compilation error: variable $z not found
  154. # [ttz 2], line 1: put $z
  155. # ~> var saved-ns = $nil
  156. # ~> eval &on-end={|ns| set saved-ns = $ns } 'var z = lorem'
  157. # ~> put $saved-ns[z]
  158. # ▶ lorem
  159. # ```
  160. #
  161. # Note that when using variables from an outer scope, only those
  162. # that have been referenced are captured as upvalues (see [closure
  163. # semantics](language.html#closure-semantics)) and thus accessible to `eval`:
  164. #
  165. # ```elvish-transcript
  166. # ~> var a b
  167. # ~> fn f {|code| nop $a; eval $code }
  168. # ~> f 'echo $a'
  169. # $nil
  170. # ~> f 'echo $b'
  171. # Exception: compilation error: variable $b not found
  172. # [eval 2], line 1: echo $b
  173. # Traceback: [... omitted ...]
  174. # ```
  175. #elvdoc:fn use-mod
  176. #
  177. # ```elvish
  178. # use-mod $use-spec
  179. # ```
  180. #
  181. # Imports a module, and outputs the namespace for the module.
  182. #
  183. # Most code should use the [use](language.html#importing-modules-with-use)
  184. # special command instead.
  185. #
  186. # Examples:
  187. #
  188. # ```elvish-transcript
  189. # ~> echo 'var x = value' > a.elv
  190. # ~> put (use-mod ./a)[x]
  191. # ▶ value
  192. # ```
  193. #elvdoc:fn deprecate
  194. #
  195. # ```elvish
  196. # deprecate $msg
  197. # ```
  198. #
  199. # Shows the given deprecation message to stderr. If called from a function
  200. # or module, also shows the call site of the function or import site of the
  201. # module. Does nothing if the combination of the call site and the message has
  202. # been shown before.
  203. #
  204. # ```elvish-transcript
  205. # ~> deprecate msg
  206. # deprecation: msg
  207. # ~> fn f { deprecate msg }
  208. # ~> f
  209. # deprecation: msg
  210. # [tty 19], line 1: f
  211. # ~> exec
  212. # ~> deprecate msg
  213. # deprecation: msg
  214. # ~> fn f { deprecate msg }
  215. # ~> f
  216. # deprecation: msg
  217. # [tty 3], line 1: f
  218. # ~> f # a different call site; shows deprecate message
  219. # deprecation: msg
  220. # [tty 4], line 1: f
  221. # ~> fn g { f }
  222. # ~> g
  223. # deprecation: msg
  224. # [tty 5], line 1: fn g { f }
  225. # ~> g # same call site, no more deprecation message
  226. # ```
  227. #elvdoc:fn sleep
  228. #
  229. # ```elvish
  230. # sleep $duration
  231. # ```
  232. #
  233. # Pauses for at least the specified duration. The actual pause duration depends
  234. # on the system.
  235. #
  236. # This only affects the current Elvish context. It does not affect any other
  237. # contexts that might be executing in parallel as a consequence of a command
  238. # such as [`peach`](#peach).
  239. #
  240. # A duration can be a simple [number](language.html#number) (with optional
  241. # fractional value) without an explicit unit suffix, with an implicit unit of
  242. # seconds.
  243. #
  244. # A duration can also be a string written as a sequence of decimal numbers,
  245. # each with optional fraction, plus a unit suffix. For example, "300ms",
  246. # "1.5h" or "1h45m7s". Valid time units are "ns", "us" (or "µs"), "ms", "s",
  247. # "m", "h".
  248. #
  249. # Passing a negative duration causes an exception; this is different from the
  250. # typical BSD or GNU `sleep` command that silently exits with a success status
  251. # without pausing when given a negative duration.
  252. #
  253. # See the [Go documentation](https://golang.org/pkg/time/#ParseDuration) for
  254. # more information about how durations are parsed.
  255. #
  256. # Examples:
  257. #
  258. # ```elvish-transcript
  259. # ~> sleep 0.1 # sleeps 0.1 seconds
  260. # ~> sleep 100ms # sleeps 0.1 seconds
  261. # ~> sleep 1.5m # sleeps 1.5 minutes
  262. # ~> sleep 1m30s # sleeps 1.5 minutes
  263. # ~> sleep -1
  264. # Exception: sleep duration must be >= zero
  265. # [tty 8], line 1: sleep -1
  266. # ```
  267. #elvdoc:fn time
  268. #
  269. # ```elvish
  270. # time &on-end=$nil $callable
  271. # ```
  272. #
  273. # Runs the callable, and call `$on-end` with the duration it took, as a
  274. # number in seconds. If `$on-end` is `$nil` (the default), prints the
  275. # duration in human-readable form.
  276. #
  277. # If `$callable` throws an exception, the exception is propagated after the
  278. # on-end or default printing is done.
  279. #
  280. # If `$on-end` throws an exception, it is propagated, unless `$callable` has
  281. # already thrown an exception.
  282. #
  283. # Example:
  284. #
  285. # ```elvish-transcript
  286. # ~> time { sleep 1 }
  287. # 1.006060647s
  288. # ~> time { sleep 0.01 }
  289. # 1.288977ms
  290. # ~> var t = ''
  291. # ~> time &on-end={|x| set t = $x } { sleep 1 }
  292. # ~> put $t
  293. # ▶ (num 1.000925004)
  294. # ~> time &on-end={|x| set t = $x } { sleep 0.01 }
  295. # ~> put $t
  296. # ▶ (num 0.011030208)
  297. # ```
  298. #
  299. # @cf benchmark
  300. #elvdoc:fn benchmark
  301. #
  302. # ```elvish
  303. # benchmark &min-runs=5 &min-time=1s &on-end=$nil &on-run-end=$nil $callable
  304. # ```
  305. #
  306. # Runs `$callable` repeatedly, and reports statistics about how long each run
  307. # takes.
  308. #
  309. # If the `&on-end` callback is not given, `benchmark` prints the average,
  310. # standard deviation, minimum and maximum of the time it took to run
  311. # `$callback`, and the number of runs. If the `&on-end` callback is given,
  312. # `benchmark` instead calls it with a map containing these metrics, keyed by
  313. # `avg`, `stddev`, `min`, `max` and `runs`. Each duration value (i.e. all
  314. # except `runs`) is given as the number of seconds.
  315. #
  316. # The number of runs is controlled by `&min-runs` and `&min-time`. The
  317. # `$callable` is run at least `&min-runs` times, **and** when the total
  318. # duration is at least `&min-time`.
  319. #
  320. # The `&min-runs` option must be a non-negative integer within the range of the
  321. # machine word.
  322. #
  323. # The `&min-time` option must be a string representing a non-negative duration,
  324. # specified as a sequence of decimal numbers with a unit suffix (the numbers
  325. # may have fractional parts), such as "300ms", "1.5h" and "1h45m7s". Valid time
  326. # units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
  327. #
  328. # If `&on-run-end` is given, it is called after each call to `$callable`, with
  329. # the time that call took, given as the number of seconds.
  330. #
  331. # If `$callable` throws an exception, `benchmark` terminates and propagates the
  332. # exception after the `&on-end` callback (or the default printing behavior)
  333. # finishes. The duration of the call that throws an exception is not passed to
  334. # `&on-run-end`, nor is it included when calculating the statistics for
  335. # `&on-end`. If the first call to `$callable` throws an exception and `&on-end`
  336. # is `$nil`, nothing is printed and any `&on-end` callback is not called.
  337. #
  338. # If `&on-run-end` is given and throws an exception, `benchmark` terminates and
  339. # propagates the exception after the `&on-end` callback (or the default
  340. # printing behavior) finishes, unless `$callable` has already thrown an
  341. # exception
  342. #
  343. # If `&on-end` throws an exception, the exception is propagated, unless
  344. # `$callable` or `&on-run-end` has already thrown an exception.
  345. #
  346. # Example:
  347. #
  348. # ```elvish-transcript
  349. # ~> benchmark { }
  350. # 98ns ± 382ns (min 0s, max 210.417µs, 10119226 runs)
  351. # ~> benchmark &on-end={|m| put $m[avg]} { }
  352. # ▶ (num 9.8e-08)
  353. # ~> benchmark &on-run-end={|d| echo $d} { sleep 0.3 }
  354. # 0.301123625
  355. # 0.30123775
  356. # 0.30119075
  357. # 0.300629166
  358. # 0.301260333
  359. # 301.088324ms ± 234.298µs (min 300.629166ms, max 301.260333ms, 5 runs)
  360. # ```
  361. #
  362. # @cf time
  363. #elvdoc:fn -ifaddrs
  364. #
  365. # ```elvish
  366. # -ifaddrs
  367. # ```
  368. #
  369. # Output all IP addresses of the current host.
  370. #
  371. # This should be part of a networking module instead of the builtin module.