builtin_fn_container.d.elv 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #elvdoc:fn ns
  2. #
  3. # ```elvish
  4. # ns $map
  5. # ```
  6. #
  7. # Constructs a namespace from `$map`, using the keys as variable names and the
  8. # values as their values. Examples:
  9. #
  10. # ```elvish-transcript
  11. # ~> var n = (ns [&name=value])
  12. # ~> put $n[name]
  13. # ▶ value
  14. # ~> var n: = (ns [&name=value])
  15. # ~> put $n:name
  16. # ▶ value
  17. # ```
  18. #elvdoc:fn make-map
  19. #
  20. # ```elvish
  21. # make-map $input?
  22. # ```
  23. #
  24. # Outputs a map from the [value inputs](#value-inputs), each of which must be
  25. # an iterable value with with two elements. The first element of each value
  26. # is used as the key, and the second element is used as the value.
  27. #
  28. # If the same key appears multiple times, the last value is used.
  29. #
  30. # Examples:
  31. #
  32. # ```elvish-transcript
  33. # ~> make-map [[k v]]
  34. # ▶ [&k=v]
  35. # ~> make-map [[k v1] [k v2]]
  36. # ▶ [&k=v2]
  37. # ~> put [k1 v1] [k2 v2] | make-map
  38. # ▶ [&k1=v1 &k2=v2]
  39. # ~> put aA bB | make-map
  40. # ▶ [&a=A &b=B]
  41. # ```
  42. #elvdoc:fn assoc
  43. #
  44. # ```elvish
  45. # assoc $container $k $v
  46. # ```
  47. #
  48. # Output a slightly modified version of `$container`, such that its value at `$k`
  49. # is `$v`. Applies to both lists and to maps.
  50. #
  51. # When `$container` is a list, `$k` may be a negative index. However, slice is not
  52. # yet supported.
  53. #
  54. # ```elvish-transcript
  55. # ~> assoc [foo bar quux] 0 lorem
  56. # ▶ [lorem bar quux]
  57. # ~> assoc [foo bar quux] -1 ipsum
  58. # ▶ [foo bar ipsum]
  59. # ~> assoc [&k=v] k v2
  60. # ▶ [&k=v2]
  61. # ~> assoc [&k=v] k2 v2
  62. # ▶ [&k2=v2 &k=v]
  63. # ```
  64. #
  65. # Etymology: [Clojure](https://clojuredocs.org/clojure.core/assoc).
  66. #
  67. # @cf dissoc
  68. #elvdoc:fn dissoc
  69. #
  70. # ```elvish
  71. # dissoc $map $k
  72. # ```
  73. #
  74. # Output a slightly modified version of `$map`, with the key `$k` removed. If
  75. # `$map` does not contain `$k` as a key, the same map is returned.
  76. #
  77. # ```elvish-transcript
  78. # ~> dissoc [&foo=bar &lorem=ipsum] foo
  79. # ▶ [&lorem=ipsum]
  80. # ~> dissoc [&foo=bar &lorem=ipsum] k
  81. # ▶ [&lorem=ipsum &foo=bar]
  82. # ```
  83. #
  84. # @cf assoc
  85. #elvdoc:fn has-value
  86. #
  87. # ```elvish
  88. # has-value $container $value
  89. # ```
  90. #
  91. # Determine whether `$value` is a value in `$container`.
  92. #
  93. # Examples, maps:
  94. #
  95. # ```elvish-transcript
  96. # ~> has-value [&k1=v1 &k2=v2] v1
  97. # ▶ $true
  98. # ~> has-value [&k1=v1 &k2=v2] k1
  99. # ▶ $false
  100. # ```
  101. #
  102. # Examples, lists:
  103. #
  104. # ```elvish-transcript
  105. # ~> has-value [v1 v2] v1
  106. # ▶ $true
  107. # ~> has-value [v1 v2] k1
  108. # ▶ $false
  109. # ```
  110. #
  111. # Examples, strings:
  112. #
  113. # ```elvish-transcript
  114. # ~> has-value ab b
  115. # ▶ $true
  116. # ~> has-value ab c
  117. # ▶ $false
  118. # ```
  119. #elvdoc:fn has-key
  120. #
  121. # ```elvish
  122. # has-key $container $key
  123. # ```
  124. #
  125. # Determine whether `$key` is a key in `$container`. A key could be a map key or
  126. # an index on a list or string. This includes a range of indexes.
  127. #
  128. # Examples, maps:
  129. #
  130. # ```elvish-transcript
  131. # ~> has-key [&k1=v1 &k2=v2] k1
  132. # ▶ $true
  133. # ~> has-key [&k1=v1 &k2=v2] v1
  134. # ▶ $false
  135. # ```
  136. #
  137. # Examples, lists:
  138. #
  139. # ```elvish-transcript
  140. # ~> has-key [v1 v2] 0
  141. # ▶ $true
  142. # ~> has-key [v1 v2] 1
  143. # ▶ $true
  144. # ~> has-key [v1 v2] 2
  145. # ▶ $false
  146. # ~> has-key [v1 v2] 0:2
  147. # ▶ $true
  148. # ~> has-key [v1 v2] 0:3
  149. # ▶ $false
  150. # ```
  151. #
  152. # Examples, strings:
  153. #
  154. # ```elvish-transcript
  155. # ~> has-key ab 0
  156. # ▶ $true
  157. # ~> has-key ab 1
  158. # ▶ $true
  159. # ~> has-key ab 2
  160. # ▶ $false
  161. # ~> has-key ab 0:2
  162. # ▶ $true
  163. # ~> has-key ab 0:3
  164. # ▶ $false
  165. # ```
  166. #elvdoc:fn keys
  167. #
  168. # ```elvish
  169. # keys $map
  170. # ```
  171. #
  172. # Put all keys of `$map` on the structured stdout.
  173. #
  174. # Example:
  175. #
  176. # ```elvish-transcript
  177. # ~> keys [&a=foo &b=bar &c=baz]
  178. # ▶ a
  179. # ▶ c
  180. # ▶ b
  181. # ```
  182. #
  183. # Note that there is no guaranteed order for the keys of a map.