builtin_fn_stream.d.elv 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #elvdoc:fn all
  2. #
  3. # ```elvish
  4. # all $inputs?
  5. # ```
  6. #
  7. # Takes [value inputs](#value-inputs), and outputs those values unchanged.
  8. #
  9. # This is an [identity
  10. # function](https://en.wikipedia.org/wiki/Identity_function) for the value
  11. # channel; in other words, `a | all` is equivalent to just `a` if `a` only has
  12. # value output.
  13. #
  14. # This command can be used inside output capture (i.e. `(all)`) to turn value
  15. # inputs into arguments. For example:
  16. #
  17. # ```elvish-transcript
  18. # ~> echo '["foo","bar"] ["lorem","ipsum"]' | from-json
  19. # ▶ [foo bar]
  20. # ▶ [lorem ipsum]
  21. # ~> echo '["foo","bar"] ["lorem","ipsum"]' | from-json | put (all)[0]
  22. # ▶ foo
  23. # ▶ lorem
  24. # ```
  25. #
  26. # The latter pipeline is equivalent to the following:
  27. #
  28. # ```elvish-transcript
  29. # ~> put (echo '["foo","bar"] ["lorem","ipsum"]' | from-json)[0]
  30. # ▶ foo
  31. # ▶ lorem
  32. # ```
  33. #
  34. # In general, when `(all)` appears in the last command of a pipeline, it is
  35. # equivalent to just moving the previous commands of the pipeline into `()`.
  36. # The choice is a stylistic one; the `(all)` variant is longer overall, but can
  37. # be more readable since it allows you to avoid putting an excessively long
  38. # pipeline inside an output capture, and keeps the data flow within the
  39. # pipeline.
  40. #
  41. # Putting the value capture inside `[]` (i.e. `[(all)]`) is useful for storing
  42. # all value inputs in a list for further processing:
  43. #
  44. # ```elvish-transcript
  45. # ~> fn f { var inputs = [(all)]; put $inputs[1] }
  46. # ~> put foo bar baz | f
  47. # ▶ bar
  48. # ```
  49. #
  50. # The `all` command can also take "inputs" from an iterable argument. This can
  51. # be used to flatten lists or strings (although not recursively):
  52. #
  53. # ```elvish-transcript
  54. # ~> all [foo [lorem ipsum]]
  55. # ▶ foo
  56. # ▶ [lorem ipsum]
  57. # ~> all foo
  58. # ▶ f
  59. # ▶ o
  60. # ▶ o
  61. # ```
  62. #
  63. # This can be used together with `(one)` to turn a single iterable value in the
  64. # pipeline into its elements:
  65. #
  66. # ```elvish-transcript
  67. # ~> echo '["foo","bar","lorem"]' | from-json
  68. # ▶ [foo bar lorem]
  69. # ~> echo '["foo","bar","lorem"]' | from-json | all (one)
  70. # ▶ foo
  71. # ▶ bar
  72. # ▶ lorem
  73. # ```
  74. #
  75. # When given byte inputs, the `all` command currently functions like
  76. # [`from-lines`](#from-lines), although this behavior is subject to change:
  77. #
  78. # ```elvish-transcript
  79. # ~> print "foo\nbar\n" | all
  80. # ▶ foo
  81. # ▶ bar
  82. # ```
  83. #
  84. # @cf one
  85. #elvdoc:fn one
  86. #
  87. # ```elvish
  88. # one $inputs?
  89. # ```
  90. #
  91. # Takes exactly one [value input](#value-inputs) and outputs it. If there are
  92. # more than one value inputs, raises an exception.
  93. #
  94. # This function can be used in a similar way to [`all`](#all), but is a better
  95. # choice when you expect that there is exactly one output.
  96. #
  97. # @cf all
  98. #elvdoc:fn take
  99. #
  100. # ```elvish
  101. # take $n $inputs?
  102. # ```
  103. #
  104. # Outputs the first `$n` [value inputs](#value-inputs). If `$n` is larger than
  105. # the number of value inputs, outputs everything.
  106. #
  107. # Examples:
  108. #
  109. # ```elvish-transcript
  110. # ~> range 2 | take 10
  111. # ▶ 0
  112. # ▶ 1
  113. # ~> take 3 [a b c d e]
  114. # ▶ a
  115. # ▶ b
  116. # ▶ c
  117. # ~> use str
  118. # ~> str:split ' ' 'how are you?' | take 1
  119. # ▶ how
  120. # ```
  121. #
  122. # Etymology: Haskell.
  123. #
  124. # @cf drop
  125. #elvdoc:fn drop
  126. #
  127. # ```elvish
  128. # drop $n $inputs?
  129. # ```
  130. #
  131. # Ignores the first `$n` [value inputs](#value-inputs) and outputs the rest.
  132. # If `$n` is larger than the number of value inputs, outputs nothing.
  133. #
  134. # Example:
  135. #
  136. # ```elvish-transcript
  137. # ~> range 10 | drop 8
  138. # ▶ (num 8)
  139. # ▶ (num 9)
  140. # ~> range 2 | drop 10
  141. # ~> drop 2 [a b c d e]
  142. # ▶ c
  143. # ▶ d
  144. # ▶ e
  145. # ~> use str
  146. # ~> str:split ' ' 'how are you?' | drop 1
  147. # ▶ are
  148. # ▶ 'you?'
  149. # ```
  150. #
  151. # Etymology: Haskell.
  152. #
  153. # @cf take
  154. #elvdoc:fn compact
  155. #
  156. # ```elvish
  157. # compact $inputs?
  158. # ```
  159. #
  160. # Replaces consecutive runs of equal values with a single copy. Similar to the
  161. # `uniq` command on Unix.
  162. #
  163. # Examples:
  164. #
  165. # ```elvish-transcript
  166. # ~> put a a b b c | compact
  167. # ▶ a
  168. # ▶ b
  169. # ▶ c
  170. # ~> compact [a a b b c]
  171. # ▶ a
  172. # ▶ b
  173. # ▶ c
  174. # ~> put a b a | compact
  175. # ▶ a
  176. # ▶ b
  177. # ▶ a
  178. # ```
  179. #elvdoc:fn count
  180. #
  181. # ```elvish
  182. # count $input-list?
  183. # ```
  184. #
  185. # Count the number of inputs.
  186. #
  187. # Examples:
  188. #
  189. # ```elvish-transcript
  190. # ~> count lorem # count bytes in a string
  191. # ▶ 5
  192. # ~> count [lorem ipsum]
  193. # ▶ 2
  194. # ~> range 100 | count
  195. # ▶ 100
  196. # ~> seq 100 | count
  197. # ▶ 100
  198. # ```
  199. #elvdoc:fn order
  200. #
  201. # ```elvish
  202. # order &less-than=$nil &key=$nil &reverse=$false $inputs?
  203. # ```
  204. #
  205. # Outputs the [value inputs](#value-inputs) sorted in ascending order. The
  206. # sorting process is guaranteed to be
  207. # [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
  208. #
  209. # The `&less-than` option, if given, establishes the ordering of the items. Its
  210. # value should be a function that takes two arguments and outputs a single
  211. # boolean indicating whether the first argument is less than the second
  212. # argument. If the function throws an exception, `order` rethrows the exception
  213. # without outputting any value.
  214. #
  215. # If `&less-than` is `$nil` (the default), a builtin comparator equivalent to
  216. # `{|a b| == -1 (compare $a $b) }` is used.
  217. #
  218. # The `&key` option, if given, is a function that gets called with each item to
  219. # be sorted. It must output a single value, which is used for comparison in
  220. # place of the original value. If the function throws an exception, `order`
  221. # rethrows the exception.
  222. #
  223. # Use of `&key` can usually be rewritten to use `&less-than` instead, but using
  224. # `&key` is usually faster because the callback is only called once for each
  225. # element, whereas the `&less-than` callback is called O(n*lg(n)) times on
  226. # average.
  227. #
  228. # If `&key` and `&less-than` are both specified, the output of the `&key`
  229. # callback for each element is passed to the `&less-than` callback.
  230. #
  231. # The `&reverse` option, if true, reverses the order of output.
  232. #
  233. # Examples:
  234. #
  235. # ```elvish-transcript
  236. # ~> put foo bar ipsum | order
  237. # ▶ bar
  238. # ▶ foo
  239. # ▶ ipsum
  240. # ~> order [(num 10) (num 1) (num 5)]
  241. # ▶ (num 1)
  242. # ▶ (num 5)
  243. # ▶ (num 10)
  244. # ~> order [[a b] [a] [b b] [a c]]
  245. # ▶ [a]
  246. # ▶ [a b]
  247. # ▶ [a c]
  248. # ▶ [b b]
  249. # ~> order &reverse [a c b]
  250. # ▶ c
  251. # ▶ b
  252. # ▶ a
  253. # ~> put [0 x] [1 a] [2 b] | order &key={|l| put $l[1]}
  254. # ▶ [1 a]
  255. # ▶ [2 b]
  256. # ▶ [0 x]
  257. # ~> order &less-than={|a b| eq $a x } [l x o r x e x m]
  258. # ▶ x
  259. # ▶ x
  260. # ▶ x
  261. # ▶ l
  262. # ▶ o
  263. # ▶ r
  264. # ▶ e
  265. # ▶ m
  266. # ```
  267. #
  268. # Beware that strings that look like numbers are treated as strings, not
  269. # numbers. To sort strings as numbers, use an explicit `&key` or `&less-than`:
  270. #
  271. # ```elvish-transcript
  272. # ~> order [5 1 10]
  273. # ▶ 1
  274. # ▶ 10
  275. # ▶ 5
  276. # ~> order &key=$num~ [5 1 10]
  277. # ▶ 1
  278. # ▶ 5
  279. # ▶ 10
  280. # ~> order &less-than=$"<~" [5 1 10]
  281. # ▶ 1
  282. # ▶ 5
  283. # ▶ 10
  284. # ```
  285. #
  286. # (The `$"<~"` syntax is a reference to [the `<` function](#num-cmp).)
  287. #
  288. # @cf compare