skill-universe 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (334) hide show
  1. package/README.md +24 -0
  2. package/bin/skillc.mjs +3 -0
  3. package/build/dev/javascript/argv/_gleam_artefacts/argv.cache +0 -0
  4. package/build/dev/javascript/argv/_gleam_artefacts/argv.cache_inline +0 -0
  5. package/build/dev/javascript/argv/_gleam_artefacts/argv.cache_meta +0 -0
  6. package/build/dev/javascript/argv/argv.erl +12 -0
  7. package/build/dev/javascript/argv/argv.mjs +31 -0
  8. package/build/dev/javascript/argv/argv_ffi.erl +27 -0
  9. package/build/dev/javascript/argv/argv_ffi.mjs +20 -0
  10. package/build/dev/javascript/argv/gleam.mjs +1 -0
  11. package/build/dev/javascript/filepath/_gleam_artefacts/filepath.cache +0 -0
  12. package/build/dev/javascript/filepath/_gleam_artefacts/filepath.cache_inline +0 -0
  13. package/build/dev/javascript/filepath/_gleam_artefacts/filepath.cache_meta +0 -0
  14. package/build/dev/javascript/filepath/filepath.erl +425 -0
  15. package/build/dev/javascript/filepath/filepath.mjs +529 -0
  16. package/build/dev/javascript/filepath/filepath_ffi.erl +9 -0
  17. package/build/dev/javascript/filepath/filepath_ffi.mjs +6 -0
  18. package/build/dev/javascript/filepath/gleam.mjs +1 -0
  19. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bit_array.cache +0 -0
  20. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bit_array.cache_inline +0 -0
  21. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bit_array.cache_meta +0 -0
  22. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bool.cache +0 -0
  23. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bool.cache_inline +0 -0
  24. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bool.cache_meta +0 -0
  25. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bytes_tree.cache +0 -0
  26. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bytes_tree.cache_inline +0 -0
  27. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bytes_tree.cache_meta +0 -0
  28. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dict.cache +0 -0
  29. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dict.cache_inline +0 -0
  30. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dict.cache_meta +0 -0
  31. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic.cache +0 -0
  32. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic.cache_inline +0 -0
  33. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic.cache_meta +0 -0
  34. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic@decode.cache +0 -0
  35. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic@decode.cache_inline +0 -0
  36. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic@decode.cache_meta +0 -0
  37. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@float.cache +0 -0
  38. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@float.cache_inline +0 -0
  39. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@float.cache_meta +0 -0
  40. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@function.cache +0 -0
  41. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@function.cache_inline +0 -0
  42. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@function.cache_meta +0 -0
  43. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@int.cache +0 -0
  44. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@int.cache_inline +0 -0
  45. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@int.cache_meta +0 -0
  46. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@io.cache +0 -0
  47. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@io.cache_inline +0 -0
  48. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@io.cache_meta +0 -0
  49. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@list.cache +0 -0
  50. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@list.cache_inline +0 -0
  51. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@list.cache_meta +0 -0
  52. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@option.cache +0 -0
  53. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@option.cache_inline +0 -0
  54. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@option.cache_meta +0 -0
  55. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@order.cache +0 -0
  56. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@order.cache_inline +0 -0
  57. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@order.cache_meta +0 -0
  58. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@pair.cache +0 -0
  59. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@pair.cache_inline +0 -0
  60. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@pair.cache_meta +0 -0
  61. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@result.cache +0 -0
  62. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@result.cache_inline +0 -0
  63. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@result.cache_meta +0 -0
  64. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@set.cache +0 -0
  65. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@set.cache_inline +0 -0
  66. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@set.cache_meta +0 -0
  67. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string.cache +0 -0
  68. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string.cache_inline +0 -0
  69. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string.cache_meta +0 -0
  70. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string_tree.cache +0 -0
  71. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string_tree.cache_inline +0 -0
  72. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string_tree.cache_meta +0 -0
  73. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@uri.cache +0 -0
  74. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@uri.cache_inline +0 -0
  75. package/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@uri.cache_meta +0 -0
  76. package/build/dev/javascript/gleam_stdlib/dict.mjs +710 -0
  77. package/build/dev/javascript/gleam_stdlib/gleam/bit_array.mjs +285 -0
  78. package/build/dev/javascript/gleam_stdlib/gleam/bool.mjs +313 -0
  79. package/build/dev/javascript/gleam_stdlib/gleam/bytes_tree.mjs +225 -0
  80. package/build/dev/javascript/gleam_stdlib/gleam/dict.mjs +456 -0
  81. package/build/dev/javascript/gleam_stdlib/gleam/dynamic/decode.mjs +985 -0
  82. package/build/dev/javascript/gleam_stdlib/gleam/dynamic.mjs +35 -0
  83. package/build/dev/javascript/gleam_stdlib/gleam/float.mjs +572 -0
  84. package/build/dev/javascript/gleam_stdlib/gleam/function.mjs +6 -0
  85. package/build/dev/javascript/gleam_stdlib/gleam/int.mjs +832 -0
  86. package/build/dev/javascript/gleam_stdlib/gleam/io.mjs +8 -0
  87. package/build/dev/javascript/gleam_stdlib/gleam/list.mjs +3160 -0
  88. package/build/dev/javascript/gleam_stdlib/gleam/option.mjs +418 -0
  89. package/build/dev/javascript/gleam_stdlib/gleam/order.mjs +178 -0
  90. package/build/dev/javascript/gleam_stdlib/gleam/pair.mjs +102 -0
  91. package/build/dev/javascript/gleam_stdlib/gleam/result.mjs +480 -0
  92. package/build/dev/javascript/gleam_stdlib/gleam/set.mjs +416 -0
  93. package/build/dev/javascript/gleam_stdlib/gleam/string.mjs +723 -0
  94. package/build/dev/javascript/gleam_stdlib/gleam/string_tree.mjs +133 -0
  95. package/build/dev/javascript/gleam_stdlib/gleam/uri.mjs +1155 -0
  96. package/build/dev/javascript/gleam_stdlib/gleam.mjs +1 -0
  97. package/build/dev/javascript/gleam_stdlib/gleam@bit_array.erl +347 -0
  98. package/build/dev/javascript/gleam_stdlib/gleam@bool.erl +352 -0
  99. package/build/dev/javascript/gleam_stdlib/gleam@bytes_tree.erl +211 -0
  100. package/build/dev/javascript/gleam_stdlib/gleam@dict.erl +521 -0
  101. package/build/dev/javascript/gleam_stdlib/gleam@dynamic.erl +106 -0
  102. package/build/dev/javascript/gleam_stdlib/gleam@dynamic@decode.erl +1112 -0
  103. package/build/dev/javascript/gleam_stdlib/gleam@float.erl +764 -0
  104. package/build/dev/javascript/gleam_stdlib/gleam@function.erl +18 -0
  105. package/build/dev/javascript/gleam_stdlib/gleam@int.erl +1004 -0
  106. package/build/dev/javascript/gleam_stdlib/gleam@io.erl +80 -0
  107. package/build/dev/javascript/gleam_stdlib/gleam@list.erl +2832 -0
  108. package/build/dev/javascript/gleam_stdlib/gleam@option.erl +413 -0
  109. package/build/dev/javascript/gleam_stdlib/gleam@order.erl +200 -0
  110. package/build/dev/javascript/gleam_stdlib/gleam@pair.erl +110 -0
  111. package/build/dev/javascript/gleam_stdlib/gleam@result.erl +532 -0
  112. package/build/dev/javascript/gleam_stdlib/gleam@set.erl +433 -0
  113. package/build/dev/javascript/gleam_stdlib/gleam@string.erl +1012 -0
  114. package/build/dev/javascript/gleam_stdlib/gleam@string_tree.erl +207 -0
  115. package/build/dev/javascript/gleam_stdlib/gleam@uri.erl +1049 -0
  116. package/build/dev/javascript/gleam_stdlib/gleam_stdlib.erl +529 -0
  117. package/build/dev/javascript/gleam_stdlib/gleam_stdlib.mjs +1050 -0
  118. package/build/dev/javascript/gleam_version +1 -0
  119. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit.cache +0 -0
  120. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit.cache_inline +0 -0
  121. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit.cache_meta +0 -0
  122. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@internal@gleam_panic.cache +0 -0
  123. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@internal@gleam_panic.cache_inline +0 -0
  124. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@internal@gleam_panic.cache_meta +0 -0
  125. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@internal@reporting.cache +0 -0
  126. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@internal@reporting.cache_inline +0 -0
  127. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@internal@reporting.cache_meta +0 -0
  128. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@should.cache +0 -0
  129. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@should.cache_inline +0 -0
  130. package/build/dev/javascript/gleeunit/_gleam_artefacts/gleeunit@should.cache_meta +0 -0
  131. package/build/dev/javascript/gleeunit/gleam.mjs +1 -0
  132. package/build/dev/javascript/gleeunit/gleeunit/internal/gleam_panic.mjs +180 -0
  133. package/build/dev/javascript/gleeunit/gleeunit/internal/gleeunit_gleam_panic_ffi.erl +49 -0
  134. package/build/dev/javascript/gleeunit/gleeunit/internal/gleeunit_gleam_panic_ffi.mjs +91 -0
  135. package/build/dev/javascript/gleeunit/gleeunit/internal/reporting.mjs +256 -0
  136. package/build/dev/javascript/gleeunit/gleeunit/should.mjs +135 -0
  137. package/build/dev/javascript/gleeunit/gleeunit.erl +89 -0
  138. package/build/dev/javascript/gleeunit/gleeunit.mjs +62 -0
  139. package/build/dev/javascript/gleeunit/gleeunit@internal@gleam_panic.erl +56 -0
  140. package/build/dev/javascript/gleeunit/gleeunit@internal@reporting.erl +343 -0
  141. package/build/dev/javascript/gleeunit/gleeunit@should.erl +153 -0
  142. package/build/dev/javascript/gleeunit/gleeunit_ffi.erl +21 -0
  143. package/build/dev/javascript/gleeunit/gleeunit_ffi.mjs +100 -0
  144. package/build/dev/javascript/gleeunit/gleeunit_progress.erl +72 -0
  145. package/build/dev/javascript/prelude.mjs +1575 -0
  146. package/build/dev/javascript/simplifile/_gleam_artefacts/simplifile.cache +0 -0
  147. package/build/dev/javascript/simplifile/_gleam_artefacts/simplifile.cache_inline +0 -0
  148. package/build/dev/javascript/simplifile/_gleam_artefacts/simplifile.cache_meta +0 -0
  149. package/build/dev/javascript/simplifile/gleam.mjs +1 -0
  150. package/build/dev/javascript/simplifile/simplifile.erl +893 -0
  151. package/build/dev/javascript/simplifile/simplifile.mjs +893 -0
  152. package/build/dev/javascript/simplifile/simplifile_erl.erl +244 -0
  153. package/build/dev/javascript/simplifile/simplifile_js.mjs +400 -0
  154. package/build/dev/javascript/skillc/_gleam_artefacts/cli_test.cache +0 -0
  155. package/build/dev/javascript/skillc/_gleam_artefacts/cli_test.cache_inline +0 -0
  156. package/build/dev/javascript/skillc/_gleam_artefacts/cli_test.cache_meta +0 -0
  157. package/build/dev/javascript/skillc/_gleam_artefacts/cli_test.cache_warnings +0 -0
  158. package/build/dev/javascript/skillc/_gleam_artefacts/compiler_test.cache +0 -0
  159. package/build/dev/javascript/skillc/_gleam_artefacts/compiler_test.cache_inline +0 -0
  160. package/build/dev/javascript/skillc/_gleam_artefacts/compiler_test.cache_meta +0 -0
  161. package/build/dev/javascript/skillc/_gleam_artefacts/compiler_test.cache_warnings +0 -0
  162. package/build/dev/javascript/skillc/_gleam_artefacts/config_test.cache +0 -0
  163. package/build/dev/javascript/skillc/_gleam_artefacts/config_test.cache_inline +0 -0
  164. package/build/dev/javascript/skillc/_gleam_artefacts/config_test.cache_meta +0 -0
  165. package/build/dev/javascript/skillc/_gleam_artefacts/config_test.cache_warnings +0 -0
  166. package/build/dev/javascript/skillc/_gleam_artefacts/importer_test.cache +0 -0
  167. package/build/dev/javascript/skillc/_gleam_artefacts/importer_test.cache_inline +0 -0
  168. package/build/dev/javascript/skillc/_gleam_artefacts/importer_test.cache_meta +0 -0
  169. package/build/dev/javascript/skillc/_gleam_artefacts/importer_test.cache_warnings +0 -0
  170. package/build/dev/javascript/skillc/_gleam_artefacts/parser_test.cache +0 -0
  171. package/build/dev/javascript/skillc/_gleam_artefacts/parser_test.cache_inline +0 -0
  172. package/build/dev/javascript/skillc/_gleam_artefacts/parser_test.cache_meta +0 -0
  173. package/build/dev/javascript/skillc/_gleam_artefacts/parser_test.cache_warnings +0 -0
  174. package/build/dev/javascript/skillc/_gleam_artefacts/path_test.cache +0 -0
  175. package/build/dev/javascript/skillc/_gleam_artefacts/path_test.cache_inline +0 -0
  176. package/build/dev/javascript/skillc/_gleam_artefacts/path_test.cache_meta +0 -0
  177. package/build/dev/javascript/skillc/_gleam_artefacts/path_test.cache_warnings +0 -0
  178. package/build/dev/javascript/skillc/_gleam_artefacts/provider_test.cache +0 -0
  179. package/build/dev/javascript/skillc/_gleam_artefacts/provider_test.cache_inline +0 -0
  180. package/build/dev/javascript/skillc/_gleam_artefacts/provider_test.cache_meta +0 -0
  181. package/build/dev/javascript/skillc/_gleam_artefacts/provider_test.cache_warnings +0 -0
  182. package/build/dev/javascript/skillc/_gleam_artefacts/registry_test.cache +0 -0
  183. package/build/dev/javascript/skillc/_gleam_artefacts/registry_test.cache_inline +0 -0
  184. package/build/dev/javascript/skillc/_gleam_artefacts/registry_test.cache_meta +0 -0
  185. package/build/dev/javascript/skillc/_gleam_artefacts/registry_test.cache_warnings +0 -0
  186. package/build/dev/javascript/skillc/_gleam_artefacts/scaffold_test.cache +0 -0
  187. package/build/dev/javascript/skillc/_gleam_artefacts/scaffold_test.cache_inline +0 -0
  188. package/build/dev/javascript/skillc/_gleam_artefacts/scaffold_test.cache_meta +0 -0
  189. package/build/dev/javascript/skillc/_gleam_artefacts/scaffold_test.cache_warnings +0 -0
  190. package/build/dev/javascript/skillc/_gleam_artefacts/skillc.cache +0 -0
  191. package/build/dev/javascript/skillc/_gleam_artefacts/skillc.cache_inline +0 -0
  192. package/build/dev/javascript/skillc/_gleam_artefacts/skillc.cache_meta +0 -0
  193. package/build/dev/javascript/skillc/_gleam_artefacts/skillc.cache_warnings +0 -0
  194. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@compiler.cache +0 -0
  195. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@compiler.cache_inline +0 -0
  196. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@compiler.cache_meta +0 -0
  197. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@compiler.cache_warnings +0 -0
  198. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@config.cache +0 -0
  199. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@config.cache_inline +0 -0
  200. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@config.cache_meta +0 -0
  201. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@config.cache_warnings +0 -0
  202. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@error.cache +0 -0
  203. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@error.cache_inline +0 -0
  204. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@error.cache_meta +0 -0
  205. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@error.cache_warnings +0 -0
  206. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@fs.cache +0 -0
  207. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@fs.cache_inline +0 -0
  208. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@fs.cache_meta +0 -0
  209. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@fs.cache_warnings +0 -0
  210. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@importer.cache +0 -0
  211. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@importer.cache_inline +0 -0
  212. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@importer.cache_meta +0 -0
  213. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@importer.cache_warnings +0 -0
  214. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@parser.cache +0 -0
  215. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@parser.cache_inline +0 -0
  216. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@parser.cache_meta +0 -0
  217. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@parser.cache_warnings +0 -0
  218. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@path.cache +0 -0
  219. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@path.cache_inline +0 -0
  220. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@path.cache_meta +0 -0
  221. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@path.cache_warnings +0 -0
  222. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@platform.cache +0 -0
  223. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@platform.cache_inline +0 -0
  224. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@platform.cache_meta +0 -0
  225. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@platform.cache_warnings +0 -0
  226. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@provider.cache +0 -0
  227. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@provider.cache_inline +0 -0
  228. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@provider.cache_meta +0 -0
  229. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@provider.cache_warnings +0 -0
  230. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@registry.cache +0 -0
  231. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@registry.cache_inline +0 -0
  232. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@registry.cache_meta +0 -0
  233. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@registry.cache_warnings +0 -0
  234. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@scaffold.cache +0 -0
  235. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@scaffold.cache_inline +0 -0
  236. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@scaffold.cache_meta +0 -0
  237. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@scaffold.cache_warnings +0 -0
  238. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@semver.cache +0 -0
  239. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@semver.cache_inline +0 -0
  240. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@semver.cache_meta +0 -0
  241. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@semver.cache_warnings +0 -0
  242. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@shell.cache +0 -0
  243. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@shell.cache_inline +0 -0
  244. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@shell.cache_meta +0 -0
  245. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@shell.cache_warnings +0 -0
  246. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@template.cache +0 -0
  247. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@template.cache_inline +0 -0
  248. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@template.cache_meta +0 -0
  249. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@template.cache_warnings +0 -0
  250. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@types.cache +0 -0
  251. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@types.cache_inline +0 -0
  252. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@types.cache_meta +0 -0
  253. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@types.cache_warnings +0 -0
  254. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@version_constraint.cache +0 -0
  255. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@version_constraint.cache_inline +0 -0
  256. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@version_constraint.cache_meta +0 -0
  257. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@version_constraint.cache_warnings +0 -0
  258. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@yaml.cache +0 -0
  259. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@yaml.cache_inline +0 -0
  260. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@yaml.cache_meta +0 -0
  261. package/build/dev/javascript/skillc/_gleam_artefacts/skillc@yaml.cache_warnings +0 -0
  262. package/build/dev/javascript/skillc/_gleam_artefacts/skillc_test.cache +0 -0
  263. package/build/dev/javascript/skillc/_gleam_artefacts/skillc_test.cache_inline +0 -0
  264. package/build/dev/javascript/skillc/_gleam_artefacts/skillc_test.cache_meta +0 -0
  265. package/build/dev/javascript/skillc/_gleam_artefacts/skillc_test.cache_warnings +0 -0
  266. package/build/dev/javascript/skillc/_gleam_artefacts/template_test.cache +0 -0
  267. package/build/dev/javascript/skillc/_gleam_artefacts/template_test.cache_inline +0 -0
  268. package/build/dev/javascript/skillc/_gleam_artefacts/template_test.cache_meta +0 -0
  269. package/build/dev/javascript/skillc/_gleam_artefacts/template_test.cache_warnings +0 -0
  270. package/build/dev/javascript/skillc/_gleam_artefacts/version_constraint_test.cache +0 -0
  271. package/build/dev/javascript/skillc/_gleam_artefacts/version_constraint_test.cache_inline +0 -0
  272. package/build/dev/javascript/skillc/_gleam_artefacts/version_constraint_test.cache_meta +0 -0
  273. package/build/dev/javascript/skillc/_gleam_artefacts/version_constraint_test.cache_warnings +0 -0
  274. package/build/dev/javascript/skillc/cli_test.mjs +461 -0
  275. package/build/dev/javascript/skillc/compiler_test.mjs +3089 -0
  276. package/build/dev/javascript/skillc/config_test.mjs +308 -0
  277. package/build/dev/javascript/skillc/gleam.main.mjs +2 -0
  278. package/build/dev/javascript/skillc/gleam.mjs +1 -0
  279. package/build/dev/javascript/skillc/importer_test.mjs +1643 -0
  280. package/build/dev/javascript/skillc/parser_test.mjs +1601 -0
  281. package/build/dev/javascript/skillc/path_test.mjs +26 -0
  282. package/build/dev/javascript/skillc/provider_test.mjs +221 -0
  283. package/build/dev/javascript/skillc/registry_test.mjs +359 -0
  284. package/build/dev/javascript/skillc/scaffold_test.mjs +405 -0
  285. package/build/dev/javascript/skillc/skillc/compiler.mjs +724 -0
  286. package/build/dev/javascript/skillc/skillc/config.mjs +172 -0
  287. package/build/dev/javascript/skillc/skillc/error.mjs +145 -0
  288. package/build/dev/javascript/skillc/skillc/fs.mjs +40 -0
  289. package/build/dev/javascript/skillc/skillc/importer.mjs +975 -0
  290. package/build/dev/javascript/skillc/skillc/parser.mjs +427 -0
  291. package/build/dev/javascript/skillc/skillc/path.mjs +52 -0
  292. package/build/dev/javascript/skillc/skillc/platform.mjs +44 -0
  293. package/build/dev/javascript/skillc/skillc/provider.mjs +86 -0
  294. package/build/dev/javascript/skillc/skillc/registry.mjs +701 -0
  295. package/build/dev/javascript/skillc/skillc/scaffold.mjs +117 -0
  296. package/build/dev/javascript/skillc/skillc/semver.mjs +293 -0
  297. package/build/dev/javascript/skillc/skillc/shell.mjs +14 -0
  298. package/build/dev/javascript/skillc/skillc/template.mjs +1177 -0
  299. package/build/dev/javascript/skillc/skillc/types.mjs +249 -0
  300. package/build/dev/javascript/skillc/skillc/version_constraint.mjs +290 -0
  301. package/build/dev/javascript/skillc/skillc/yaml.mjs +51 -0
  302. package/build/dev/javascript/skillc/skillc.mjs +873 -0
  303. package/build/dev/javascript/skillc/skillc_ffi.mjs +30 -0
  304. package/build/dev/javascript/skillc/skillc_test.mjs +5 -0
  305. package/build/dev/javascript/skillc/template_test.mjs +1638 -0
  306. package/build/dev/javascript/skillc/version_constraint_test.mjs +1325 -0
  307. package/build/dev/javascript/yay/_gleam_artefacts/yay.cache +0 -0
  308. package/build/dev/javascript/yay/_gleam_artefacts/yay.cache_inline +0 -0
  309. package/build/dev/javascript/yay/_gleam_artefacts/yay.cache_meta +0 -0
  310. package/build/dev/javascript/yay/gleam.mjs +1 -0
  311. package/build/dev/javascript/yay/yaml_ffi.erl +106 -0
  312. package/build/dev/javascript/yay/yaml_ffi.mjs +373 -0
  313. package/build/dev/javascript/yay/yay.erl +1218 -0
  314. package/build/dev/javascript/yay/yay.mjs +1403 -0
  315. package/package.json +28 -0
  316. package/src/skillc/compiler.gleam +561 -0
  317. package/src/skillc/config.gleam +91 -0
  318. package/src/skillc/error.gleam +37 -0
  319. package/src/skillc/fs.gleam +31 -0
  320. package/src/skillc/importer.gleam +709 -0
  321. package/src/skillc/parser.gleam +279 -0
  322. package/src/skillc/path.gleam +30 -0
  323. package/src/skillc/platform.gleam +25 -0
  324. package/src/skillc/provider.gleam +58 -0
  325. package/src/skillc/registry.gleam +472 -0
  326. package/src/skillc/scaffold.gleam +112 -0
  327. package/src/skillc/semver.gleam +165 -0
  328. package/src/skillc/shell.gleam +12 -0
  329. package/src/skillc/template.gleam +885 -0
  330. package/src/skillc/types.gleam +93 -0
  331. package/src/skillc/version_constraint.gleam +163 -0
  332. package/src/skillc/yaml.gleam +27 -0
  333. package/src/skillc.gleam +466 -0
  334. package/src/skillc_ffi.mjs +30 -0
@@ -0,0 +1,3160 @@
1
+ import {
2
+ Ok,
3
+ Error,
4
+ toList,
5
+ Empty as $Empty,
6
+ prepend as listPrepend,
7
+ CustomType as $CustomType,
8
+ makeError,
9
+ divideFloat,
10
+ isEqual,
11
+ } from "../gleam.mjs";
12
+ import * as $dict from "../gleam/dict.mjs";
13
+ import * as $float from "../gleam/float.mjs";
14
+ import * as $int from "../gleam/int.mjs";
15
+ import * as $order from "../gleam/order.mjs";
16
+
17
+ const FILEPATH = "src/gleam/list.gleam";
18
+
19
+ export class Continue extends $CustomType {
20
+ constructor($0) {
21
+ super();
22
+ this[0] = $0;
23
+ }
24
+ }
25
+ export const ContinueOrStop$Continue = ($0) => new Continue($0);
26
+ export const ContinueOrStop$isContinue = (value) => value instanceof Continue;
27
+ export const ContinueOrStop$Continue$0 = (value) => value[0];
28
+
29
+ export class Stop extends $CustomType {
30
+ constructor($0) {
31
+ super();
32
+ this[0] = $0;
33
+ }
34
+ }
35
+ export const ContinueOrStop$Stop = ($0) => new Stop($0);
36
+ export const ContinueOrStop$isStop = (value) => value instanceof Stop;
37
+ export const ContinueOrStop$Stop$0 = (value) => value[0];
38
+
39
+ class Ascending extends $CustomType {}
40
+
41
+ class Descending extends $CustomType {}
42
+
43
+ const min_positive = 2.2250738585072014e-308;
44
+
45
+ function length_loop(loop$list, loop$count) {
46
+ while (true) {
47
+ let list = loop$list;
48
+ let count = loop$count;
49
+ if (list instanceof $Empty) {
50
+ return count;
51
+ } else {
52
+ let list$1 = list.tail;
53
+ loop$list = list$1;
54
+ loop$count = count + 1;
55
+ }
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Counts the number of elements in a given list.
61
+ *
62
+ * This function has to traverse the list to determine the number of elements,
63
+ * so it runs in linear time.
64
+ *
65
+ * This function is natively implemented by the virtual machine and is highly
66
+ * optimised.
67
+ *
68
+ * ## Examples
69
+ *
70
+ * ```gleam
71
+ * length([])
72
+ * // -> 0
73
+ * ```
74
+ *
75
+ * ```gleam
76
+ * length([1])
77
+ * // -> 1
78
+ * ```
79
+ *
80
+ * ```gleam
81
+ * length([1, 2])
82
+ * // -> 2
83
+ * ```
84
+ */
85
+ export function length(list) {
86
+ return length_loop(list, 0);
87
+ }
88
+
89
+ function count_loop(loop$list, loop$predicate, loop$acc) {
90
+ while (true) {
91
+ let list = loop$list;
92
+ let predicate = loop$predicate;
93
+ let acc = loop$acc;
94
+ if (list instanceof $Empty) {
95
+ return acc;
96
+ } else {
97
+ let first$1 = list.head;
98
+ let rest$1 = list.tail;
99
+ let $ = predicate(first$1);
100
+ if ($) {
101
+ loop$list = rest$1;
102
+ loop$predicate = predicate;
103
+ loop$acc = acc + 1;
104
+ } else {
105
+ loop$list = rest$1;
106
+ loop$predicate = predicate;
107
+ loop$acc = acc;
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Counts the number of elements in a given list satisfying a given predicate.
115
+ *
116
+ * This function has to traverse the list to determine the number of elements,
117
+ * so it runs in linear time.
118
+ *
119
+ * ## Examples
120
+ *
121
+ * ```gleam
122
+ * count([], fn(a) { a > 0 })
123
+ * // -> 0
124
+ * ```
125
+ *
126
+ * ```gleam
127
+ * count([1], fn(a) { a > 0 })
128
+ * // -> 1
129
+ * ```
130
+ *
131
+ * ```gleam
132
+ * count([1, 2, 3], int.is_odd)
133
+ * // -> 2
134
+ * ```
135
+ */
136
+ export function count(list, predicate) {
137
+ return count_loop(list, predicate, 0);
138
+ }
139
+
140
+ /**
141
+ * Reverses a list and prepends it to another list.
142
+ * This function runs in linear time, proportional to the length of the list
143
+ * to prepend.
144
+ *
145
+ * @ignore
146
+ */
147
+ function reverse_and_prepend(loop$prefix, loop$suffix) {
148
+ while (true) {
149
+ let prefix = loop$prefix;
150
+ let suffix = loop$suffix;
151
+ if (prefix instanceof $Empty) {
152
+ return suffix;
153
+ } else {
154
+ let first$1 = prefix.head;
155
+ let rest$1 = prefix.tail;
156
+ loop$prefix = rest$1;
157
+ loop$suffix = listPrepend(first$1, suffix);
158
+ }
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Creates a new list from a given list containing the same elements but in the
164
+ * opposite order.
165
+ *
166
+ * This function has to traverse the list to create the new reversed list, so
167
+ * it runs in linear time.
168
+ *
169
+ * This function is natively implemented by the virtual machine and is highly
170
+ * optimised.
171
+ *
172
+ * ## Examples
173
+ *
174
+ * ```gleam
175
+ * reverse([])
176
+ * // -> []
177
+ * ```
178
+ *
179
+ * ```gleam
180
+ * reverse([1])
181
+ * // -> [1]
182
+ * ```
183
+ *
184
+ * ```gleam
185
+ * reverse([1, 2])
186
+ * // -> [2, 1]
187
+ * ```
188
+ */
189
+ export function reverse(list) {
190
+ return reverse_and_prepend(list, toList([]));
191
+ }
192
+
193
+ /**
194
+ * Determines whether or not the list is empty.
195
+ *
196
+ * This function runs in constant time.
197
+ *
198
+ * ## Examples
199
+ *
200
+ * ```gleam
201
+ * is_empty([])
202
+ * // -> True
203
+ * ```
204
+ *
205
+ * ```gleam
206
+ * is_empty([1])
207
+ * // -> False
208
+ * ```
209
+ *
210
+ * ```gleam
211
+ * is_empty([1, 1])
212
+ * // -> False
213
+ * ```
214
+ */
215
+ export function is_empty(list) {
216
+ return isEqual(list, toList([]));
217
+ }
218
+
219
+ /**
220
+ * Determines whether or not a given element exists within a given list.
221
+ *
222
+ * This function traverses the list to find the element, so it runs in linear
223
+ * time.
224
+ *
225
+ * ## Examples
226
+ *
227
+ * ```gleam
228
+ * [] |> contains(any: 0)
229
+ * // -> False
230
+ * ```
231
+ *
232
+ * ```gleam
233
+ * [0] |> contains(any: 0)
234
+ * // -> True
235
+ * ```
236
+ *
237
+ * ```gleam
238
+ * [1] |> contains(any: 0)
239
+ * // -> False
240
+ * ```
241
+ *
242
+ * ```gleam
243
+ * [1, 1] |> contains(any: 0)
244
+ * // -> False
245
+ * ```
246
+ *
247
+ * ```gleam
248
+ * [1, 0] |> contains(any: 0)
249
+ * // -> True
250
+ * ```
251
+ */
252
+ export function contains(loop$list, loop$elem) {
253
+ while (true) {
254
+ let list = loop$list;
255
+ let elem = loop$elem;
256
+ if (list instanceof $Empty) {
257
+ return false;
258
+ } else {
259
+ let first$1 = list.head;
260
+ if (isEqual(first$1, elem)) {
261
+ return true;
262
+ } else {
263
+ let rest$1 = list.tail;
264
+ loop$list = rest$1;
265
+ loop$elem = elem;
266
+ }
267
+ }
268
+ }
269
+ }
270
+
271
+ /**
272
+ * Gets the first element from the start of the list, if there is one.
273
+ *
274
+ * ## Examples
275
+ *
276
+ * ```gleam
277
+ * first([])
278
+ * // -> Error(Nil)
279
+ * ```
280
+ *
281
+ * ```gleam
282
+ * first([0])
283
+ * // -> Ok(0)
284
+ * ```
285
+ *
286
+ * ```gleam
287
+ * first([1, 2])
288
+ * // -> Ok(1)
289
+ * ```
290
+ */
291
+ export function first(list) {
292
+ if (list instanceof $Empty) {
293
+ return new Error(undefined);
294
+ } else {
295
+ let first$1 = list.head;
296
+ return new Ok(first$1);
297
+ }
298
+ }
299
+
300
+ /**
301
+ * Returns the list minus the first element. If the list is empty, `Error(Nil)` is
302
+ * returned.
303
+ *
304
+ * This function runs in constant time and does not make a copy of the list.
305
+ *
306
+ * ## Examples
307
+ *
308
+ * ```gleam
309
+ * rest([])
310
+ * // -> Error(Nil)
311
+ * ```
312
+ *
313
+ * ```gleam
314
+ * rest([0])
315
+ * // -> Ok([])
316
+ * ```
317
+ *
318
+ * ```gleam
319
+ * rest([1, 2])
320
+ * // -> Ok([2])
321
+ * ```
322
+ */
323
+ export function rest(list) {
324
+ if (list instanceof $Empty) {
325
+ return new Error(undefined);
326
+ } else {
327
+ let rest$1 = list.tail;
328
+ return new Ok(rest$1);
329
+ }
330
+ }
331
+
332
+ /**
333
+ * Groups the elements from the given list by the given key function.
334
+ *
335
+ * Does not preserve the initial value order.
336
+ *
337
+ * ## Examples
338
+ *
339
+ * ```gleam
340
+ * import gleam/dict
341
+ *
342
+ * [Ok(3), Error("Wrong"), Ok(200), Ok(73)]
343
+ * |> group(by: fn(i) {
344
+ * case i {
345
+ * Ok(_) -> "Successful"
346
+ * Error(_) -> "Failed"
347
+ * }
348
+ * })
349
+ * |> dict.to_list
350
+ * // -> [
351
+ * // #("Failed", [Error("Wrong")]),
352
+ * // #("Successful", [Ok(73), Ok(200), Ok(3)])
353
+ * // ]
354
+ * ```
355
+ *
356
+ * ```gleam
357
+ * import gleam/dict
358
+ *
359
+ * group([1,2,3,4,5], by: fn(i) { i - i / 3 * 3 })
360
+ * |> dict.to_list
361
+ * // -> [#(0, [3]), #(1, [4, 1]), #(2, [5, 2])]
362
+ * ```
363
+ */
364
+ export function group(list, key) {
365
+ return $dict.group(key, list);
366
+ }
367
+
368
+ function filter_loop(loop$list, loop$fun, loop$acc) {
369
+ while (true) {
370
+ let list = loop$list;
371
+ let fun = loop$fun;
372
+ let acc = loop$acc;
373
+ if (list instanceof $Empty) {
374
+ return reverse(acc);
375
+ } else {
376
+ let first$1 = list.head;
377
+ let rest$1 = list.tail;
378
+ let _block;
379
+ let $ = fun(first$1);
380
+ if ($) {
381
+ _block = listPrepend(first$1, acc);
382
+ } else {
383
+ _block = acc;
384
+ }
385
+ let new_acc = _block;
386
+ loop$list = rest$1;
387
+ loop$fun = fun;
388
+ loop$acc = new_acc;
389
+ }
390
+ }
391
+ }
392
+
393
+ /**
394
+ * Returns a new list containing only the elements from the first list for
395
+ * which the given functions returns `True`.
396
+ *
397
+ * ## Examples
398
+ *
399
+ * ```gleam
400
+ * filter([2, 4, 6, 1], fn(x) { x > 2 })
401
+ * // -> [4, 6]
402
+ * ```
403
+ *
404
+ * ```gleam
405
+ * filter([2, 4, 6, 1], fn(x) { x > 6 })
406
+ * // -> []
407
+ * ```
408
+ */
409
+ export function filter(list, predicate) {
410
+ return filter_loop(list, predicate, toList([]));
411
+ }
412
+
413
+ function filter_map_loop(loop$list, loop$fun, loop$acc) {
414
+ while (true) {
415
+ let list = loop$list;
416
+ let fun = loop$fun;
417
+ let acc = loop$acc;
418
+ if (list instanceof $Empty) {
419
+ return reverse(acc);
420
+ } else {
421
+ let first$1 = list.head;
422
+ let rest$1 = list.tail;
423
+ let _block;
424
+ let $ = fun(first$1);
425
+ if ($ instanceof Ok) {
426
+ let first$2 = $[0];
427
+ _block = listPrepend(first$2, acc);
428
+ } else {
429
+ _block = acc;
430
+ }
431
+ let new_acc = _block;
432
+ loop$list = rest$1;
433
+ loop$fun = fun;
434
+ loop$acc = new_acc;
435
+ }
436
+ }
437
+ }
438
+
439
+ /**
440
+ * Returns a new list containing only the elements from the first list for
441
+ * which the given functions returns `Ok(_)`.
442
+ *
443
+ * ## Examples
444
+ *
445
+ * ```gleam
446
+ * filter_map([2, 4, 6, 1], Error)
447
+ * // -> []
448
+ * ```
449
+ *
450
+ * ```gleam
451
+ * filter_map([2, 4, 6, 1], fn(x) { Ok(x + 1) })
452
+ * // -> [3, 5, 7, 2]
453
+ * ```
454
+ */
455
+ export function filter_map(list, fun) {
456
+ return filter_map_loop(list, fun, toList([]));
457
+ }
458
+
459
+ function map_loop(loop$list, loop$fun, loop$acc) {
460
+ while (true) {
461
+ let list = loop$list;
462
+ let fun = loop$fun;
463
+ let acc = loop$acc;
464
+ if (list instanceof $Empty) {
465
+ return reverse(acc);
466
+ } else {
467
+ let first$1 = list.head;
468
+ let rest$1 = list.tail;
469
+ loop$list = rest$1;
470
+ loop$fun = fun;
471
+ loop$acc = listPrepend(fun(first$1), acc);
472
+ }
473
+ }
474
+ }
475
+
476
+ /**
477
+ * Returns a new list containing the results of applying the supplied function to each element.
478
+ *
479
+ * ## Examples
480
+ *
481
+ * ```gleam
482
+ * map([2, 4, 6], fn(x) { x * 2 })
483
+ * // -> [4, 8, 12]
484
+ * ```
485
+ */
486
+ export function map(list, fun) {
487
+ return map_loop(list, fun, toList([]));
488
+ }
489
+
490
+ function map2_loop(loop$list1, loop$list2, loop$fun, loop$acc) {
491
+ while (true) {
492
+ let list1 = loop$list1;
493
+ let list2 = loop$list2;
494
+ let fun = loop$fun;
495
+ let acc = loop$acc;
496
+ if (list1 instanceof $Empty) {
497
+ return reverse(acc);
498
+ } else if (list2 instanceof $Empty) {
499
+ return reverse(acc);
500
+ } else {
501
+ let a = list1.head;
502
+ let as_ = list1.tail;
503
+ let b = list2.head;
504
+ let bs = list2.tail;
505
+ loop$list1 = as_;
506
+ loop$list2 = bs;
507
+ loop$fun = fun;
508
+ loop$acc = listPrepend(fun(a, b), acc);
509
+ }
510
+ }
511
+ }
512
+
513
+ /**
514
+ * Combines two lists into a single list using the given function.
515
+ *
516
+ * If a list is longer than the other, the extra elements are dropped.
517
+ *
518
+ * ## Examples
519
+ *
520
+ * ```gleam
521
+ * map2([1, 2, 3], [4, 5, 6], fn(x, y) { x + y })
522
+ * // -> [5, 7, 9]
523
+ * ```
524
+ *
525
+ * ```gleam
526
+ * map2([1, 2], ["a", "b", "c"], fn(i, x) { #(i, x) })
527
+ * // -> [#(1, "a"), #(2, "b")]
528
+ * ```
529
+ */
530
+ export function map2(list1, list2, fun) {
531
+ return map2_loop(list1, list2, fun, toList([]));
532
+ }
533
+
534
+ function map_fold_loop(loop$list, loop$fun, loop$acc, loop$list_acc) {
535
+ while (true) {
536
+ let list = loop$list;
537
+ let fun = loop$fun;
538
+ let acc = loop$acc;
539
+ let list_acc = loop$list_acc;
540
+ if (list instanceof $Empty) {
541
+ return [acc, reverse(list_acc)];
542
+ } else {
543
+ let first$1 = list.head;
544
+ let rest$1 = list.tail;
545
+ let $ = fun(acc, first$1);
546
+ let acc$1;
547
+ let first$2;
548
+ acc$1 = $[0];
549
+ first$2 = $[1];
550
+ loop$list = rest$1;
551
+ loop$fun = fun;
552
+ loop$acc = acc$1;
553
+ loop$list_acc = listPrepend(first$2, list_acc);
554
+ }
555
+ }
556
+ }
557
+
558
+ /**
559
+ * Similar to `map` but also lets you pass around an accumulated value.
560
+ *
561
+ * ## Examples
562
+ *
563
+ * ```gleam
564
+ * map_fold(
565
+ * over: [1, 2, 3],
566
+ * from: 100,
567
+ * with: fn(memo, i) { #(memo + i, i * 2) }
568
+ * )
569
+ * // -> #(106, [2, 4, 6])
570
+ * ```
571
+ */
572
+ export function map_fold(list, initial, fun) {
573
+ return map_fold_loop(list, fun, initial, toList([]));
574
+ }
575
+
576
+ function index_map_loop(loop$list, loop$fun, loop$index, loop$acc) {
577
+ while (true) {
578
+ let list = loop$list;
579
+ let fun = loop$fun;
580
+ let index = loop$index;
581
+ let acc = loop$acc;
582
+ if (list instanceof $Empty) {
583
+ return reverse(acc);
584
+ } else {
585
+ let first$1 = list.head;
586
+ let rest$1 = list.tail;
587
+ let acc$1 = listPrepend(fun(first$1, index), acc);
588
+ loop$list = rest$1;
589
+ loop$fun = fun;
590
+ loop$index = index + 1;
591
+ loop$acc = acc$1;
592
+ }
593
+ }
594
+ }
595
+
596
+ /**
597
+ * Similar to `map`, but the supplied function will also be passed the index
598
+ * of the element being mapped as an additional argument.
599
+ *
600
+ * The index starts at 0, so the first element is 0, the second is 1, and so
601
+ * on.
602
+ *
603
+ * ## Examples
604
+ *
605
+ * ```gleam
606
+ * index_map(["a", "b"], fn(x, i) { #(i, x) })
607
+ * // -> [#(0, "a"), #(1, "b")]
608
+ * ```
609
+ */
610
+ export function index_map(list, fun) {
611
+ return index_map_loop(list, fun, 0, toList([]));
612
+ }
613
+
614
+ function try_map_loop(loop$list, loop$fun, loop$acc) {
615
+ while (true) {
616
+ let list = loop$list;
617
+ let fun = loop$fun;
618
+ let acc = loop$acc;
619
+ if (list instanceof $Empty) {
620
+ return new Ok(reverse(acc));
621
+ } else {
622
+ let first$1 = list.head;
623
+ let rest$1 = list.tail;
624
+ let $ = fun(first$1);
625
+ if ($ instanceof Ok) {
626
+ let first$2 = $[0];
627
+ loop$list = rest$1;
628
+ loop$fun = fun;
629
+ loop$acc = listPrepend(first$2, acc);
630
+ } else {
631
+ return $;
632
+ }
633
+ }
634
+ }
635
+ }
636
+
637
+ /**
638
+ * Takes a function that returns a `Result` and applies it to each element in a
639
+ * given list in turn.
640
+ *
641
+ * If the function returns `Ok(new_value)` for all elements in the list then a
642
+ * list of the new values is returned.
643
+ *
644
+ * If the function returns `Error(reason)` for any of the elements then it is
645
+ * returned immediately. None of the elements in the list are processed after
646
+ * one returns an `Error`.
647
+ *
648
+ * ## Examples
649
+ *
650
+ * ```gleam
651
+ * try_map([1, 2, 3], fn(x) { Ok(x + 2) })
652
+ * // -> Ok([3, 4, 5])
653
+ * ```
654
+ *
655
+ * ```gleam
656
+ * try_map([1, 2, 3], fn(_) { Error(0) })
657
+ * // -> Error(0)
658
+ * ```
659
+ *
660
+ * ```gleam
661
+ * try_map([[1], [2, 3]], first)
662
+ * // -> Ok([1, 2])
663
+ * ```
664
+ *
665
+ * ```gleam
666
+ * try_map([[1], [], [2]], first)
667
+ * // -> Error(Nil)
668
+ * ```
669
+ */
670
+ export function try_map(list, fun) {
671
+ return try_map_loop(list, fun, toList([]));
672
+ }
673
+
674
+ /**
675
+ * Returns a list that is the given list with up to the given number of
676
+ * elements removed from the front of the list.
677
+ *
678
+ * If the list has less than the number of elements an empty list is
679
+ * returned.
680
+ *
681
+ * This function runs in linear time but does not copy the list.
682
+ *
683
+ * ## Examples
684
+ *
685
+ * ```gleam
686
+ * drop([1, 2, 3, 4], 2)
687
+ * // -> [3, 4]
688
+ * ```
689
+ *
690
+ * ```gleam
691
+ * drop([1, 2, 3, 4], 9)
692
+ * // -> []
693
+ * ```
694
+ */
695
+ export function drop(loop$list, loop$n) {
696
+ while (true) {
697
+ let list = loop$list;
698
+ let n = loop$n;
699
+ let $ = n <= 0;
700
+ if ($) {
701
+ return list;
702
+ } else {
703
+ if (list instanceof $Empty) {
704
+ return list;
705
+ } else {
706
+ let rest$1 = list.tail;
707
+ loop$list = rest$1;
708
+ loop$n = n - 1;
709
+ }
710
+ }
711
+ }
712
+ }
713
+
714
+ function take_loop(loop$list, loop$n, loop$acc) {
715
+ while (true) {
716
+ let list = loop$list;
717
+ let n = loop$n;
718
+ let acc = loop$acc;
719
+ let $ = n <= 0;
720
+ if ($) {
721
+ return reverse(acc);
722
+ } else {
723
+ if (list instanceof $Empty) {
724
+ return reverse(acc);
725
+ } else {
726
+ let first$1 = list.head;
727
+ let rest$1 = list.tail;
728
+ loop$list = rest$1;
729
+ loop$n = n - 1;
730
+ loop$acc = listPrepend(first$1, acc);
731
+ }
732
+ }
733
+ }
734
+ }
735
+
736
+ /**
737
+ * Returns a list containing the first given number of elements from the given
738
+ * list.
739
+ *
740
+ * If the list has less than the number of elements then the full list is
741
+ * returned.
742
+ *
743
+ * This function runs in linear time.
744
+ *
745
+ * ## Examples
746
+ *
747
+ * ```gleam
748
+ * take([1, 2, 3, 4], 2)
749
+ * // -> [1, 2]
750
+ * ```
751
+ *
752
+ * ```gleam
753
+ * take([1, 2, 3, 4], 9)
754
+ * // -> [1, 2, 3, 4]
755
+ * ```
756
+ */
757
+ export function take(list, n) {
758
+ return take_loop(list, n, toList([]));
759
+ }
760
+
761
+ /**
762
+ * Returns a new empty list.
763
+ *
764
+ * ## Examples
765
+ *
766
+ * ```gleam
767
+ * new()
768
+ * // -> []
769
+ * ```
770
+ */
771
+ export function new$() {
772
+ return toList([]);
773
+ }
774
+
775
+ /**
776
+ * Returns the given item wrapped in a list.
777
+ *
778
+ * ## Examples
779
+ *
780
+ * ```gleam
781
+ * wrap(1)
782
+ * // -> [1]
783
+ *
784
+ * wrap(["a", "b", "c"])
785
+ * // -> [["a", "b", "c"]]
786
+ *
787
+ * wrap([[]])
788
+ * // -> [[[]]]
789
+ * ```
790
+ */
791
+ export function wrap(item) {
792
+ return toList([item]);
793
+ }
794
+
795
+ function append_loop(loop$first, loop$second) {
796
+ while (true) {
797
+ let first = loop$first;
798
+ let second = loop$second;
799
+ if (first instanceof $Empty) {
800
+ return second;
801
+ } else {
802
+ let first$1 = first.head;
803
+ let rest$1 = first.tail;
804
+ loop$first = rest$1;
805
+ loop$second = listPrepend(first$1, second);
806
+ }
807
+ }
808
+ }
809
+
810
+ /**
811
+ * Joins one list onto the end of another.
812
+ *
813
+ * This function runs in linear time, and it traverses and copies the first
814
+ * list.
815
+ *
816
+ * ## Examples
817
+ *
818
+ * ```gleam
819
+ * append([1, 2], [3])
820
+ * // -> [1, 2, 3]
821
+ * ```
822
+ */
823
+ export function append(first, second) {
824
+ return append_loop(reverse(first), second);
825
+ }
826
+
827
+ /**
828
+ * Prefixes an item to a list. This can also be done using the dedicated
829
+ * syntax instead.
830
+ *
831
+ * ```gleam
832
+ * let existing_list = [2, 3, 4]
833
+ *
834
+ * [1, ..existing_list]
835
+ * // -> [1, 2, 3, 4]
836
+ *
837
+ * prepend(to: existing_list, this: 1)
838
+ * // -> [1, 2, 3, 4]
839
+ * ```
840
+ */
841
+ export function prepend(list, item) {
842
+ return listPrepend(item, list);
843
+ }
844
+
845
+ function flatten_loop(loop$lists, loop$acc) {
846
+ while (true) {
847
+ let lists = loop$lists;
848
+ let acc = loop$acc;
849
+ if (lists instanceof $Empty) {
850
+ return reverse(acc);
851
+ } else {
852
+ let list = lists.head;
853
+ let further_lists = lists.tail;
854
+ loop$lists = further_lists;
855
+ loop$acc = reverse_and_prepend(list, acc);
856
+ }
857
+ }
858
+ }
859
+
860
+ /**
861
+ * Joins a list of lists into a single list.
862
+ *
863
+ * This function traverses all elements twice on the JavaScript target.
864
+ * This function traverses all elements once on the Erlang target.
865
+ *
866
+ * ## Examples
867
+ *
868
+ * ```gleam
869
+ * flatten([[1], [2, 3], []])
870
+ * // -> [1, 2, 3]
871
+ * ```
872
+ */
873
+ export function flatten(lists) {
874
+ return flatten_loop(lists, toList([]));
875
+ }
876
+
877
+ /**
878
+ * Maps the list with the given function into a list of lists, and then flattens it.
879
+ *
880
+ * ## Examples
881
+ *
882
+ * ```gleam
883
+ * flat_map([2, 4, 6], fn(x) { [x, x + 1] })
884
+ * // -> [2, 3, 4, 5, 6, 7]
885
+ * ```
886
+ */
887
+ export function flat_map(list, fun) {
888
+ return flatten(map(list, fun));
889
+ }
890
+
891
+ /**
892
+ * Reduces a list of elements into a single value by calling a given function
893
+ * on each element, going from left to right.
894
+ *
895
+ * `fold([1, 2, 3], 0, add)` is the equivalent of
896
+ * `add(add(add(0, 1), 2), 3)`.
897
+ *
898
+ * This function runs in linear time.
899
+ */
900
+ export function fold(loop$list, loop$initial, loop$fun) {
901
+ while (true) {
902
+ let list = loop$list;
903
+ let initial = loop$initial;
904
+ let fun = loop$fun;
905
+ if (list instanceof $Empty) {
906
+ return initial;
907
+ } else {
908
+ let first$1 = list.head;
909
+ let rest$1 = list.tail;
910
+ loop$list = rest$1;
911
+ loop$initial = fun(initial, first$1);
912
+ loop$fun = fun;
913
+ }
914
+ }
915
+ }
916
+
917
+ /**
918
+ * Reduces a list of elements into a single value by calling a given function
919
+ * on each element, going from right to left.
920
+ *
921
+ * `fold_right([1, 2, 3], 0, add)` is the equivalent of
922
+ * `add(add(add(0, 3), 2), 1)`.
923
+ *
924
+ * This function runs in linear time.
925
+ *
926
+ * Unlike `fold` this function is not tail recursive. Where possible use
927
+ * `fold` instead as it will use less memory.
928
+ */
929
+ export function fold_right(list, initial, fun) {
930
+ if (list instanceof $Empty) {
931
+ return initial;
932
+ } else {
933
+ let first$1 = list.head;
934
+ let rest$1 = list.tail;
935
+ return fun(fold_right(rest$1, initial, fun), first$1);
936
+ }
937
+ }
938
+
939
+ function index_fold_loop(loop$over, loop$acc, loop$with, loop$index) {
940
+ while (true) {
941
+ let over = loop$over;
942
+ let acc = loop$acc;
943
+ let with$ = loop$with;
944
+ let index = loop$index;
945
+ if (over instanceof $Empty) {
946
+ return acc;
947
+ } else {
948
+ let first$1 = over.head;
949
+ let rest$1 = over.tail;
950
+ loop$over = rest$1;
951
+ loop$acc = with$(acc, first$1, index);
952
+ loop$with = with$;
953
+ loop$index = index + 1;
954
+ }
955
+ }
956
+ }
957
+
958
+ /**
959
+ * Like `fold` but the folding function also receives the index of the current element.
960
+ *
961
+ * ## Examples
962
+ *
963
+ * ```gleam
964
+ * ["a", "b", "c"]
965
+ * |> index_fold("", fn(acc, item, index) {
966
+ * acc <> int.to_string(index) <> ":" <> item <> " "
967
+ * })
968
+ * // -> "0:a 1:b 2:c"
969
+ * ```
970
+ *
971
+ * ```gleam
972
+ * [10, 20, 30]
973
+ * |> index_fold(0, fn(acc, item, index) { acc + item * index })
974
+ * // -> 80
975
+ * ```
976
+ */
977
+ export function index_fold(list, initial, fun) {
978
+ return index_fold_loop(list, initial, fun, 0);
979
+ }
980
+
981
+ /**
982
+ * A variant of fold that might fail.
983
+ *
984
+ * The folding function should return `Result(accumulator, error)`.
985
+ * If the returned value is `Ok(accumulator)` try_fold will try the next value in the list.
986
+ * If the returned value is `Error(error)` try_fold will stop and return that error.
987
+ *
988
+ * ## Examples
989
+ *
990
+ * ```gleam
991
+ * [1, 2, 3, 4]
992
+ * |> try_fold(0, fn(acc, i) {
993
+ * case i < 3 {
994
+ * True -> Ok(acc + i)
995
+ * False -> Error(Nil)
996
+ * }
997
+ * })
998
+ * // -> Error(Nil)
999
+ * ```
1000
+ */
1001
+ export function try_fold(loop$list, loop$initial, loop$fun) {
1002
+ while (true) {
1003
+ let list = loop$list;
1004
+ let initial = loop$initial;
1005
+ let fun = loop$fun;
1006
+ if (list instanceof $Empty) {
1007
+ return new Ok(initial);
1008
+ } else {
1009
+ let first$1 = list.head;
1010
+ let rest$1 = list.tail;
1011
+ let $ = fun(initial, first$1);
1012
+ if ($ instanceof Ok) {
1013
+ let result = $[0];
1014
+ loop$list = rest$1;
1015
+ loop$initial = result;
1016
+ loop$fun = fun;
1017
+ } else {
1018
+ return $;
1019
+ }
1020
+ }
1021
+ }
1022
+ }
1023
+
1024
+ /**
1025
+ * A variant of fold that allows to stop folding earlier.
1026
+ *
1027
+ * The folding function should return `ContinueOrStop(accumulator)`.
1028
+ * If the returned value is `Continue(accumulator)` fold_until will try the next value in the list.
1029
+ * If the returned value is `Stop(accumulator)` fold_until will stop and return that accumulator.
1030
+ *
1031
+ * ## Examples
1032
+ *
1033
+ * ```gleam
1034
+ * [1, 2, 3, 4]
1035
+ * |> fold_until(0, fn(acc, i) {
1036
+ * case i < 3 {
1037
+ * True -> Continue(acc + i)
1038
+ * False -> Stop(acc)
1039
+ * }
1040
+ * })
1041
+ * // -> 3
1042
+ * ```
1043
+ */
1044
+ export function fold_until(loop$list, loop$initial, loop$fun) {
1045
+ while (true) {
1046
+ let list = loop$list;
1047
+ let initial = loop$initial;
1048
+ let fun = loop$fun;
1049
+ if (list instanceof $Empty) {
1050
+ return initial;
1051
+ } else {
1052
+ let first$1 = list.head;
1053
+ let rest$1 = list.tail;
1054
+ let $ = fun(initial, first$1);
1055
+ if ($ instanceof Continue) {
1056
+ let next_accumulator = $[0];
1057
+ loop$list = rest$1;
1058
+ loop$initial = next_accumulator;
1059
+ loop$fun = fun;
1060
+ } else {
1061
+ let b = $[0];
1062
+ return b;
1063
+ }
1064
+ }
1065
+ }
1066
+ }
1067
+
1068
+ /**
1069
+ * Finds the first element in a given list for which the given function returns
1070
+ * `True`.
1071
+ *
1072
+ * Returns `Error(Nil)` if no such element is found.
1073
+ *
1074
+ * ## Examples
1075
+ *
1076
+ * ```gleam
1077
+ * find([1, 2, 3], fn(x) { x > 2 })
1078
+ * // -> Ok(3)
1079
+ * ```
1080
+ *
1081
+ * ```gleam
1082
+ * find([1, 2, 3], fn(x) { x > 4 })
1083
+ * // -> Error(Nil)
1084
+ * ```
1085
+ *
1086
+ * ```gleam
1087
+ * find([], fn(_) { True })
1088
+ * // -> Error(Nil)
1089
+ * ```
1090
+ */
1091
+ export function find(loop$list, loop$is_desired) {
1092
+ while (true) {
1093
+ let list = loop$list;
1094
+ let is_desired = loop$is_desired;
1095
+ if (list instanceof $Empty) {
1096
+ return new Error(undefined);
1097
+ } else {
1098
+ let first$1 = list.head;
1099
+ let rest$1 = list.tail;
1100
+ let $ = is_desired(first$1);
1101
+ if ($) {
1102
+ return new Ok(first$1);
1103
+ } else {
1104
+ loop$list = rest$1;
1105
+ loop$is_desired = is_desired;
1106
+ }
1107
+ }
1108
+ }
1109
+ }
1110
+
1111
+ /**
1112
+ * Finds the first element in a given list for which the given function returns
1113
+ * `Ok(new_value)`, then returns the wrapped `new_value`.
1114
+ *
1115
+ * Returns `Error(Nil)` if no such element is found.
1116
+ *
1117
+ * ## Examples
1118
+ *
1119
+ * ```gleam
1120
+ * find_map([[], [2], [3]], first)
1121
+ * // -> Ok(2)
1122
+ * ```
1123
+ *
1124
+ * ```gleam
1125
+ * find_map([[], []], first)
1126
+ * // -> Error(Nil)
1127
+ * ```
1128
+ *
1129
+ * ```gleam
1130
+ * find_map([], first)
1131
+ * // -> Error(Nil)
1132
+ * ```
1133
+ */
1134
+ export function find_map(loop$list, loop$fun) {
1135
+ while (true) {
1136
+ let list = loop$list;
1137
+ let fun = loop$fun;
1138
+ if (list instanceof $Empty) {
1139
+ return new Error(undefined);
1140
+ } else {
1141
+ let first$1 = list.head;
1142
+ let rest$1 = list.tail;
1143
+ let $ = fun(first$1);
1144
+ if ($ instanceof Ok) {
1145
+ return $;
1146
+ } else {
1147
+ loop$list = rest$1;
1148
+ loop$fun = fun;
1149
+ }
1150
+ }
1151
+ }
1152
+ }
1153
+
1154
+ /**
1155
+ * Returns `True` if the given function returns `True` for all the elements in
1156
+ * the given list. If the function returns `False` for any of the elements it
1157
+ * immediately returns `False` without checking the rest of the list.
1158
+ *
1159
+ * ## Examples
1160
+ *
1161
+ * ```gleam
1162
+ * all([], fn(x) { x > 3 })
1163
+ * // -> True
1164
+ * ```
1165
+ *
1166
+ * ```gleam
1167
+ * all([4, 5], fn(x) { x > 3 })
1168
+ * // -> True
1169
+ * ```
1170
+ *
1171
+ * ```gleam
1172
+ * all([4, 3], fn(x) { x > 3 })
1173
+ * // -> False
1174
+ * ```
1175
+ */
1176
+ export function all(loop$list, loop$predicate) {
1177
+ while (true) {
1178
+ let list = loop$list;
1179
+ let predicate = loop$predicate;
1180
+ if (list instanceof $Empty) {
1181
+ return true;
1182
+ } else {
1183
+ let first$1 = list.head;
1184
+ let rest$1 = list.tail;
1185
+ let $ = predicate(first$1);
1186
+ if ($) {
1187
+ loop$list = rest$1;
1188
+ loop$predicate = predicate;
1189
+ } else {
1190
+ return $;
1191
+ }
1192
+ }
1193
+ }
1194
+ }
1195
+
1196
+ /**
1197
+ * Returns `True` if the given function returns `True` for any the elements in
1198
+ * the given list. If the function returns `True` for any of the elements it
1199
+ * immediately returns `True` without checking the rest of the list.
1200
+ *
1201
+ * ## Examples
1202
+ *
1203
+ * ```gleam
1204
+ * any([], fn(x) { x > 3 })
1205
+ * // -> False
1206
+ * ```
1207
+ *
1208
+ * ```gleam
1209
+ * any([4, 5], fn(x) { x > 3 })
1210
+ * // -> True
1211
+ * ```
1212
+ *
1213
+ * ```gleam
1214
+ * any([4, 3], fn(x) { x > 4 })
1215
+ * // -> False
1216
+ * ```
1217
+ *
1218
+ * ```gleam
1219
+ * any([3, 4], fn(x) { x > 3 })
1220
+ * // -> True
1221
+ * ```
1222
+ */
1223
+ export function any(loop$list, loop$predicate) {
1224
+ while (true) {
1225
+ let list = loop$list;
1226
+ let predicate = loop$predicate;
1227
+ if (list instanceof $Empty) {
1228
+ return false;
1229
+ } else {
1230
+ let first$1 = list.head;
1231
+ let rest$1 = list.tail;
1232
+ let $ = predicate(first$1);
1233
+ if ($) {
1234
+ return $;
1235
+ } else {
1236
+ loop$list = rest$1;
1237
+ loop$predicate = predicate;
1238
+ }
1239
+ }
1240
+ }
1241
+ }
1242
+
1243
+ function zip_loop(loop$one, loop$other, loop$acc) {
1244
+ while (true) {
1245
+ let one = loop$one;
1246
+ let other = loop$other;
1247
+ let acc = loop$acc;
1248
+ if (one instanceof $Empty) {
1249
+ return reverse(acc);
1250
+ } else if (other instanceof $Empty) {
1251
+ return reverse(acc);
1252
+ } else {
1253
+ let first_one = one.head;
1254
+ let rest_one = one.tail;
1255
+ let first_other = other.head;
1256
+ let rest_other = other.tail;
1257
+ loop$one = rest_one;
1258
+ loop$other = rest_other;
1259
+ loop$acc = listPrepend([first_one, first_other], acc);
1260
+ }
1261
+ }
1262
+ }
1263
+
1264
+ /**
1265
+ * Takes two lists and returns a single list of 2-element tuples.
1266
+ *
1267
+ * If one of the lists is longer than the other, the remaining elements from
1268
+ * the longer list are not used.
1269
+ *
1270
+ * ## Examples
1271
+ *
1272
+ * ```gleam
1273
+ * zip([], [])
1274
+ * // -> []
1275
+ * ```
1276
+ *
1277
+ * ```gleam
1278
+ * zip([1, 2], [3])
1279
+ * // -> [#(1, 3)]
1280
+ * ```
1281
+ *
1282
+ * ```gleam
1283
+ * zip([1], [3, 4])
1284
+ * // -> [#(1, 3)]
1285
+ * ```
1286
+ *
1287
+ * ```gleam
1288
+ * zip([1, 2], [3, 4])
1289
+ * // -> [#(1, 3), #(2, 4)]
1290
+ * ```
1291
+ */
1292
+ export function zip(list, other) {
1293
+ return zip_loop(list, other, toList([]));
1294
+ }
1295
+
1296
+ function strict_zip_loop(loop$one, loop$other, loop$acc) {
1297
+ while (true) {
1298
+ let one = loop$one;
1299
+ let other = loop$other;
1300
+ let acc = loop$acc;
1301
+ if (one instanceof $Empty) {
1302
+ if (other instanceof $Empty) {
1303
+ return new Ok(reverse(acc));
1304
+ } else {
1305
+ return new Error(undefined);
1306
+ }
1307
+ } else if (other instanceof $Empty) {
1308
+ return new Error(undefined);
1309
+ } else {
1310
+ let first_one = one.head;
1311
+ let rest_one = one.tail;
1312
+ let first_other = other.head;
1313
+ let rest_other = other.tail;
1314
+ loop$one = rest_one;
1315
+ loop$other = rest_other;
1316
+ loop$acc = listPrepend([first_one, first_other], acc);
1317
+ }
1318
+ }
1319
+ }
1320
+
1321
+ /**
1322
+ * Takes two lists and returns a single list of 2-element tuples.
1323
+ *
1324
+ * If one of the lists is longer than the other, an `Error` is returned.
1325
+ *
1326
+ * ## Examples
1327
+ *
1328
+ * ```gleam
1329
+ * strict_zip([], [])
1330
+ * // -> Ok([])
1331
+ * ```
1332
+ *
1333
+ * ```gleam
1334
+ * strict_zip([1, 2], [3])
1335
+ * // -> Error(Nil)
1336
+ * ```
1337
+ *
1338
+ * ```gleam
1339
+ * strict_zip([1], [3, 4])
1340
+ * // -> Error(Nil)
1341
+ * ```
1342
+ *
1343
+ * ```gleam
1344
+ * strict_zip([1, 2], [3, 4])
1345
+ * // -> Ok([#(1, 3), #(2, 4)])
1346
+ * ```
1347
+ */
1348
+ export function strict_zip(list, other) {
1349
+ return strict_zip_loop(list, other, toList([]));
1350
+ }
1351
+
1352
+ function unzip_loop(loop$input, loop$one, loop$other) {
1353
+ while (true) {
1354
+ let input = loop$input;
1355
+ let one = loop$one;
1356
+ let other = loop$other;
1357
+ if (input instanceof $Empty) {
1358
+ return [reverse(one), reverse(other)];
1359
+ } else {
1360
+ let rest$1 = input.tail;
1361
+ let first_one = input.head[0];
1362
+ let first_other = input.head[1];
1363
+ loop$input = rest$1;
1364
+ loop$one = listPrepend(first_one, one);
1365
+ loop$other = listPrepend(first_other, other);
1366
+ }
1367
+ }
1368
+ }
1369
+
1370
+ /**
1371
+ * Takes a single list of 2-element tuples and returns two lists.
1372
+ *
1373
+ * ## Examples
1374
+ *
1375
+ * ```gleam
1376
+ * unzip([#(1, 2), #(3, 4)])
1377
+ * // -> #([1, 3], [2, 4])
1378
+ * ```
1379
+ *
1380
+ * ```gleam
1381
+ * unzip([])
1382
+ * // -> #([], [])
1383
+ * ```
1384
+ */
1385
+ export function unzip(input) {
1386
+ return unzip_loop(input, toList([]), toList([]));
1387
+ }
1388
+
1389
+ function intersperse_loop(loop$list, loop$separator, loop$acc) {
1390
+ while (true) {
1391
+ let list = loop$list;
1392
+ let separator = loop$separator;
1393
+ let acc = loop$acc;
1394
+ if (list instanceof $Empty) {
1395
+ return reverse(acc);
1396
+ } else {
1397
+ let first$1 = list.head;
1398
+ let rest$1 = list.tail;
1399
+ loop$list = rest$1;
1400
+ loop$separator = separator;
1401
+ loop$acc = listPrepend(first$1, listPrepend(separator, acc));
1402
+ }
1403
+ }
1404
+ }
1405
+
1406
+ /**
1407
+ * Inserts a given value between each existing element in a given list.
1408
+ *
1409
+ * This function runs in linear time and copies the list.
1410
+ *
1411
+ * ## Examples
1412
+ *
1413
+ * ```gleam
1414
+ * intersperse([1, 1, 1], 2)
1415
+ * // -> [1, 2, 1, 2, 1]
1416
+ * ```
1417
+ *
1418
+ * ```gleam
1419
+ * intersperse([], 2)
1420
+ * // -> []
1421
+ * ```
1422
+ */
1423
+ export function intersperse(list, elem) {
1424
+ if (list instanceof $Empty) {
1425
+ return list;
1426
+ } else {
1427
+ let $ = list.tail;
1428
+ if ($ instanceof $Empty) {
1429
+ return list;
1430
+ } else {
1431
+ let first$1 = list.head;
1432
+ let rest$1 = $;
1433
+ return intersperse_loop(rest$1, elem, toList([first$1]));
1434
+ }
1435
+ }
1436
+ }
1437
+
1438
+ function unique_loop(loop$list, loop$seen, loop$acc) {
1439
+ while (true) {
1440
+ let list = loop$list;
1441
+ let seen = loop$seen;
1442
+ let acc = loop$acc;
1443
+ if (list instanceof $Empty) {
1444
+ return reverse(acc);
1445
+ } else {
1446
+ let first$1 = list.head;
1447
+ let rest$1 = list.tail;
1448
+ let $ = $dict.has_key(seen, first$1);
1449
+ if ($) {
1450
+ loop$list = rest$1;
1451
+ loop$seen = seen;
1452
+ loop$acc = acc;
1453
+ } else {
1454
+ loop$list = rest$1;
1455
+ loop$seen = $dict.insert(seen, first$1, undefined);
1456
+ loop$acc = listPrepend(first$1, acc);
1457
+ }
1458
+ }
1459
+ }
1460
+ }
1461
+
1462
+ /**
1463
+ * Removes any duplicate elements from a given list.
1464
+ *
1465
+ * This function returns in loglinear time.
1466
+ *
1467
+ * ## Examples
1468
+ *
1469
+ * ```gleam
1470
+ * unique([1, 1, 1, 4, 7, 3, 3, 4])
1471
+ * // -> [1, 4, 7, 3]
1472
+ * ```
1473
+ */
1474
+ export function unique(list) {
1475
+ return unique_loop(list, $dict.new$(), toList([]));
1476
+ }
1477
+
1478
+ /**
1479
+ * Given a list it returns slices of it that are locally sorted in ascending
1480
+ * order.
1481
+ *
1482
+ * Imagine you have this list:
1483
+ *
1484
+ * ```
1485
+ * [1, 2, 3, 2, 1, 0]
1486
+ * ^^^^^^^ ^^^^^^^ This is a slice in descending order
1487
+ * |
1488
+ * | This is a slice that is sorted in ascending order
1489
+ * ```
1490
+ *
1491
+ * So the produced result will contain these two slices, each one sorted in
1492
+ * ascending order: `[[1, 2, 3], [0, 1, 2]]`.
1493
+ *
1494
+ * - `growing` is an accumulator with the current slice being grown
1495
+ * - `direction` is the growing direction of the slice being grown, it could
1496
+ * either be ascending or strictly descending
1497
+ * - `prev` is the previous element that needs to be added to the growing slice
1498
+ * it is carried around to check whether we have to keep growing the current
1499
+ * slice or not
1500
+ * - `acc` is the accumulator containing the slices sorted in ascending order
1501
+ *
1502
+ * @ignore
1503
+ */
1504
+ function sequences(
1505
+ loop$list,
1506
+ loop$compare,
1507
+ loop$growing,
1508
+ loop$direction,
1509
+ loop$prev,
1510
+ loop$acc
1511
+ ) {
1512
+ while (true) {
1513
+ let list = loop$list;
1514
+ let compare = loop$compare;
1515
+ let growing = loop$growing;
1516
+ let direction = loop$direction;
1517
+ let prev = loop$prev;
1518
+ let acc = loop$acc;
1519
+ let growing$1 = listPrepend(prev, growing);
1520
+ if (list instanceof $Empty) {
1521
+ if (direction instanceof Ascending) {
1522
+ return listPrepend(reverse(growing$1), acc);
1523
+ } else {
1524
+ return listPrepend(growing$1, acc);
1525
+ }
1526
+ } else {
1527
+ let new$1 = list.head;
1528
+ let rest$1 = list.tail;
1529
+ let $ = compare(prev, new$1);
1530
+ if (direction instanceof Ascending) {
1531
+ if ($ instanceof $order.Lt) {
1532
+ loop$list = rest$1;
1533
+ loop$compare = compare;
1534
+ loop$growing = growing$1;
1535
+ loop$direction = direction;
1536
+ loop$prev = new$1;
1537
+ loop$acc = acc;
1538
+ } else if ($ instanceof $order.Eq) {
1539
+ loop$list = rest$1;
1540
+ loop$compare = compare;
1541
+ loop$growing = growing$1;
1542
+ loop$direction = direction;
1543
+ loop$prev = new$1;
1544
+ loop$acc = acc;
1545
+ } else {
1546
+ let _block;
1547
+ if (direction instanceof Ascending) {
1548
+ _block = listPrepend(reverse(growing$1), acc);
1549
+ } else {
1550
+ _block = listPrepend(growing$1, acc);
1551
+ }
1552
+ let acc$1 = _block;
1553
+ if (rest$1 instanceof $Empty) {
1554
+ return listPrepend(toList([new$1]), acc$1);
1555
+ } else {
1556
+ let next = rest$1.head;
1557
+ let rest$2 = rest$1.tail;
1558
+ let _block$1;
1559
+ let $1 = compare(new$1, next);
1560
+ if ($1 instanceof $order.Lt) {
1561
+ _block$1 = new Ascending();
1562
+ } else if ($1 instanceof $order.Eq) {
1563
+ _block$1 = new Ascending();
1564
+ } else {
1565
+ _block$1 = new Descending();
1566
+ }
1567
+ let direction$1 = _block$1;
1568
+ loop$list = rest$2;
1569
+ loop$compare = compare;
1570
+ loop$growing = toList([new$1]);
1571
+ loop$direction = direction$1;
1572
+ loop$prev = next;
1573
+ loop$acc = acc$1;
1574
+ }
1575
+ }
1576
+ } else if ($ instanceof $order.Lt) {
1577
+ let _block;
1578
+ if (direction instanceof Ascending) {
1579
+ _block = listPrepend(reverse(growing$1), acc);
1580
+ } else {
1581
+ _block = listPrepend(growing$1, acc);
1582
+ }
1583
+ let acc$1 = _block;
1584
+ if (rest$1 instanceof $Empty) {
1585
+ return listPrepend(toList([new$1]), acc$1);
1586
+ } else {
1587
+ let next = rest$1.head;
1588
+ let rest$2 = rest$1.tail;
1589
+ let _block$1;
1590
+ let $1 = compare(new$1, next);
1591
+ if ($1 instanceof $order.Lt) {
1592
+ _block$1 = new Ascending();
1593
+ } else if ($1 instanceof $order.Eq) {
1594
+ _block$1 = new Ascending();
1595
+ } else {
1596
+ _block$1 = new Descending();
1597
+ }
1598
+ let direction$1 = _block$1;
1599
+ loop$list = rest$2;
1600
+ loop$compare = compare;
1601
+ loop$growing = toList([new$1]);
1602
+ loop$direction = direction$1;
1603
+ loop$prev = next;
1604
+ loop$acc = acc$1;
1605
+ }
1606
+ } else if ($ instanceof $order.Eq) {
1607
+ let _block;
1608
+ if (direction instanceof Ascending) {
1609
+ _block = listPrepend(reverse(growing$1), acc);
1610
+ } else {
1611
+ _block = listPrepend(growing$1, acc);
1612
+ }
1613
+ let acc$1 = _block;
1614
+ if (rest$1 instanceof $Empty) {
1615
+ return listPrepend(toList([new$1]), acc$1);
1616
+ } else {
1617
+ let next = rest$1.head;
1618
+ let rest$2 = rest$1.tail;
1619
+ let _block$1;
1620
+ let $1 = compare(new$1, next);
1621
+ if ($1 instanceof $order.Lt) {
1622
+ _block$1 = new Ascending();
1623
+ } else if ($1 instanceof $order.Eq) {
1624
+ _block$1 = new Ascending();
1625
+ } else {
1626
+ _block$1 = new Descending();
1627
+ }
1628
+ let direction$1 = _block$1;
1629
+ loop$list = rest$2;
1630
+ loop$compare = compare;
1631
+ loop$growing = toList([new$1]);
1632
+ loop$direction = direction$1;
1633
+ loop$prev = next;
1634
+ loop$acc = acc$1;
1635
+ }
1636
+ } else {
1637
+ loop$list = rest$1;
1638
+ loop$compare = compare;
1639
+ loop$growing = growing$1;
1640
+ loop$direction = direction;
1641
+ loop$prev = new$1;
1642
+ loop$acc = acc;
1643
+ }
1644
+ }
1645
+ }
1646
+ }
1647
+
1648
+ /**
1649
+ * Merges two lists sorted in ascending order into a single list sorted in
1650
+ * descending order according to the given comparator function.
1651
+ *
1652
+ * This reversing of the sort order is not avoidable if we want to implement
1653
+ * merge as a tail recursive function. We could reverse the accumulator before
1654
+ * returning it but that would end up being less efficient; so the merging
1655
+ * algorithm has to play around this.
1656
+ *
1657
+ * @ignore
1658
+ */
1659
+ function merge_ascendings(loop$list1, loop$list2, loop$compare, loop$acc) {
1660
+ while (true) {
1661
+ let list1 = loop$list1;
1662
+ let list2 = loop$list2;
1663
+ let compare = loop$compare;
1664
+ let acc = loop$acc;
1665
+ if (list1 instanceof $Empty) {
1666
+ let list = list2;
1667
+ return reverse_and_prepend(list, acc);
1668
+ } else if (list2 instanceof $Empty) {
1669
+ let list = list1;
1670
+ return reverse_and_prepend(list, acc);
1671
+ } else {
1672
+ let first1 = list1.head;
1673
+ let rest1 = list1.tail;
1674
+ let first2 = list2.head;
1675
+ let rest2 = list2.tail;
1676
+ let $ = compare(first1, first2);
1677
+ if ($ instanceof $order.Lt) {
1678
+ loop$list1 = rest1;
1679
+ loop$list2 = list2;
1680
+ loop$compare = compare;
1681
+ loop$acc = listPrepend(first1, acc);
1682
+ } else if ($ instanceof $order.Eq) {
1683
+ loop$list1 = list1;
1684
+ loop$list2 = rest2;
1685
+ loop$compare = compare;
1686
+ loop$acc = listPrepend(first2, acc);
1687
+ } else {
1688
+ loop$list1 = list1;
1689
+ loop$list2 = rest2;
1690
+ loop$compare = compare;
1691
+ loop$acc = listPrepend(first2, acc);
1692
+ }
1693
+ }
1694
+ }
1695
+ }
1696
+
1697
+ /**
1698
+ * Given a list of ascending lists, it merges adjacent pairs into a single
1699
+ * descending list, halving their number.
1700
+ * It returns a list of the remaining descending lists.
1701
+ *
1702
+ * @ignore
1703
+ */
1704
+ function merge_ascending_pairs(loop$sequences, loop$compare, loop$acc) {
1705
+ while (true) {
1706
+ let sequences = loop$sequences;
1707
+ let compare = loop$compare;
1708
+ let acc = loop$acc;
1709
+ if (sequences instanceof $Empty) {
1710
+ return reverse(acc);
1711
+ } else {
1712
+ let $ = sequences.tail;
1713
+ if ($ instanceof $Empty) {
1714
+ let sequence = sequences.head;
1715
+ return reverse(listPrepend(reverse(sequence), acc));
1716
+ } else {
1717
+ let ascending1 = sequences.head;
1718
+ let ascending2 = $.head;
1719
+ let rest$1 = $.tail;
1720
+ let descending = merge_ascendings(
1721
+ ascending1,
1722
+ ascending2,
1723
+ compare,
1724
+ toList([]),
1725
+ );
1726
+ loop$sequences = rest$1;
1727
+ loop$compare = compare;
1728
+ loop$acc = listPrepend(descending, acc);
1729
+ }
1730
+ }
1731
+ }
1732
+ }
1733
+
1734
+ /**
1735
+ * This is exactly the same as merge_ascendings but mirrored: it merges two
1736
+ * lists sorted in descending order into a single list sorted in ascending
1737
+ * order according to the given comparator function.
1738
+ *
1739
+ * This reversing of the sort order is not avoidable if we want to implement
1740
+ * merge as a tail recursive function. We could reverse the accumulator before
1741
+ * returning it but that would end up being less efficient; so the merging
1742
+ * algorithm has to play around this.
1743
+ *
1744
+ * @ignore
1745
+ */
1746
+ function merge_descendings(loop$list1, loop$list2, loop$compare, loop$acc) {
1747
+ while (true) {
1748
+ let list1 = loop$list1;
1749
+ let list2 = loop$list2;
1750
+ let compare = loop$compare;
1751
+ let acc = loop$acc;
1752
+ if (list1 instanceof $Empty) {
1753
+ let list = list2;
1754
+ return reverse_and_prepend(list, acc);
1755
+ } else if (list2 instanceof $Empty) {
1756
+ let list = list1;
1757
+ return reverse_and_prepend(list, acc);
1758
+ } else {
1759
+ let first1 = list1.head;
1760
+ let rest1 = list1.tail;
1761
+ let first2 = list2.head;
1762
+ let rest2 = list2.tail;
1763
+ let $ = compare(first1, first2);
1764
+ if ($ instanceof $order.Lt) {
1765
+ loop$list1 = list1;
1766
+ loop$list2 = rest2;
1767
+ loop$compare = compare;
1768
+ loop$acc = listPrepend(first2, acc);
1769
+ } else if ($ instanceof $order.Eq) {
1770
+ loop$list1 = rest1;
1771
+ loop$list2 = list2;
1772
+ loop$compare = compare;
1773
+ loop$acc = listPrepend(first1, acc);
1774
+ } else {
1775
+ loop$list1 = rest1;
1776
+ loop$list2 = list2;
1777
+ loop$compare = compare;
1778
+ loop$acc = listPrepend(first1, acc);
1779
+ }
1780
+ }
1781
+ }
1782
+ }
1783
+
1784
+ /**
1785
+ * This is the same as merge_ascending_pairs but flipped for descending lists.
1786
+ *
1787
+ * @ignore
1788
+ */
1789
+ function merge_descending_pairs(loop$sequences, loop$compare, loop$acc) {
1790
+ while (true) {
1791
+ let sequences = loop$sequences;
1792
+ let compare = loop$compare;
1793
+ let acc = loop$acc;
1794
+ if (sequences instanceof $Empty) {
1795
+ return reverse(acc);
1796
+ } else {
1797
+ let $ = sequences.tail;
1798
+ if ($ instanceof $Empty) {
1799
+ let sequence = sequences.head;
1800
+ return reverse(listPrepend(reverse(sequence), acc));
1801
+ } else {
1802
+ let descending1 = sequences.head;
1803
+ let descending2 = $.head;
1804
+ let rest$1 = $.tail;
1805
+ let ascending = merge_descendings(
1806
+ descending1,
1807
+ descending2,
1808
+ compare,
1809
+ toList([]),
1810
+ );
1811
+ loop$sequences = rest$1;
1812
+ loop$compare = compare;
1813
+ loop$acc = listPrepend(ascending, acc);
1814
+ }
1815
+ }
1816
+ }
1817
+ }
1818
+
1819
+ /**
1820
+ * Given some some sorted sequences (assumed to be sorted in `direction`) it
1821
+ * merges them all together until we're left with just a list sorted in
1822
+ * ascending order.
1823
+ *
1824
+ * @ignore
1825
+ */
1826
+ function merge_all(loop$sequences, loop$direction, loop$compare) {
1827
+ while (true) {
1828
+ let sequences = loop$sequences;
1829
+ let direction = loop$direction;
1830
+ let compare = loop$compare;
1831
+ if (sequences instanceof $Empty) {
1832
+ return sequences;
1833
+ } else if (direction instanceof Ascending) {
1834
+ let $ = sequences.tail;
1835
+ if ($ instanceof $Empty) {
1836
+ let sequence = sequences.head;
1837
+ return sequence;
1838
+ } else {
1839
+ let sequences$1 = merge_ascending_pairs(sequences, compare, toList([]));
1840
+ loop$sequences = sequences$1;
1841
+ loop$direction = new Descending();
1842
+ loop$compare = compare;
1843
+ }
1844
+ } else {
1845
+ let $ = sequences.tail;
1846
+ if ($ instanceof $Empty) {
1847
+ let sequence = sequences.head;
1848
+ return reverse(sequence);
1849
+ } else {
1850
+ let sequences$1 = merge_descending_pairs(sequences, compare, toList([]));
1851
+ loop$sequences = sequences$1;
1852
+ loop$direction = new Ascending();
1853
+ loop$compare = compare;
1854
+ }
1855
+ }
1856
+ }
1857
+ }
1858
+
1859
+ /**
1860
+ * Sorts from smallest to largest based upon the ordering specified by a given
1861
+ * function.
1862
+ *
1863
+ * ## Examples
1864
+ *
1865
+ * ```gleam
1866
+ * import gleam/int
1867
+ *
1868
+ * sort([4, 3, 6, 5, 4, 1, 2], by: int.compare)
1869
+ * // -> [1, 2, 3, 4, 4, 5, 6]
1870
+ * ```
1871
+ */
1872
+ export function sort(list, compare) {
1873
+ if (list instanceof $Empty) {
1874
+ return list;
1875
+ } else {
1876
+ let $ = list.tail;
1877
+ if ($ instanceof $Empty) {
1878
+ return list;
1879
+ } else {
1880
+ let x = list.head;
1881
+ let y = $.head;
1882
+ let rest$1 = $.tail;
1883
+ let _block;
1884
+ let $1 = compare(x, y);
1885
+ if ($1 instanceof $order.Lt) {
1886
+ _block = new Ascending();
1887
+ } else if ($1 instanceof $order.Eq) {
1888
+ _block = new Ascending();
1889
+ } else {
1890
+ _block = new Descending();
1891
+ }
1892
+ let direction = _block;
1893
+ let sequences$1 = sequences(
1894
+ rest$1,
1895
+ compare,
1896
+ toList([x]),
1897
+ direction,
1898
+ y,
1899
+ toList([]),
1900
+ );
1901
+ return merge_all(sequences$1, new Ascending(), compare);
1902
+ }
1903
+ }
1904
+ }
1905
+
1906
+ function range_loop(loop$start, loop$stop, loop$acc) {
1907
+ while (true) {
1908
+ let start = loop$start;
1909
+ let stop = loop$stop;
1910
+ let acc = loop$acc;
1911
+ let $ = $int.compare(start, stop);
1912
+ if ($ instanceof $order.Lt) {
1913
+ loop$start = start;
1914
+ loop$stop = stop - 1;
1915
+ loop$acc = listPrepend(stop, acc);
1916
+ } else if ($ instanceof $order.Eq) {
1917
+ return listPrepend(stop, acc);
1918
+ } else {
1919
+ loop$start = start;
1920
+ loop$stop = stop + 1;
1921
+ loop$acc = listPrepend(stop, acc);
1922
+ }
1923
+ }
1924
+ }
1925
+
1926
+ export function range(start, stop) {
1927
+ return range_loop(start, stop, toList([]));
1928
+ }
1929
+
1930
+ function repeat_loop(loop$item, loop$times, loop$acc) {
1931
+ while (true) {
1932
+ let item = loop$item;
1933
+ let times = loop$times;
1934
+ let acc = loop$acc;
1935
+ let $ = times <= 0;
1936
+ if ($) {
1937
+ return acc;
1938
+ } else {
1939
+ loop$item = item;
1940
+ loop$times = times - 1;
1941
+ loop$acc = listPrepend(item, acc);
1942
+ }
1943
+ }
1944
+ }
1945
+
1946
+ /**
1947
+ * Builds a list of a given value a given number of times.
1948
+ *
1949
+ * ## Examples
1950
+ *
1951
+ * ```gleam
1952
+ * repeat("a", times: 0)
1953
+ * // -> []
1954
+ * ```
1955
+ *
1956
+ * ```gleam
1957
+ * repeat("a", times: 5)
1958
+ * // -> ["a", "a", "a", "a", "a"]
1959
+ * ```
1960
+ */
1961
+ export function repeat(a, times) {
1962
+ return repeat_loop(a, times, toList([]));
1963
+ }
1964
+
1965
+ function split_loop(loop$list, loop$n, loop$taken) {
1966
+ while (true) {
1967
+ let list = loop$list;
1968
+ let n = loop$n;
1969
+ let taken = loop$taken;
1970
+ let $ = n <= 0;
1971
+ if ($) {
1972
+ return [reverse(taken), list];
1973
+ } else {
1974
+ if (list instanceof $Empty) {
1975
+ return [reverse(taken), toList([])];
1976
+ } else {
1977
+ let first$1 = list.head;
1978
+ let rest$1 = list.tail;
1979
+ loop$list = rest$1;
1980
+ loop$n = n - 1;
1981
+ loop$taken = listPrepend(first$1, taken);
1982
+ }
1983
+ }
1984
+ }
1985
+ }
1986
+
1987
+ /**
1988
+ * Splits a list in two before the given index.
1989
+ *
1990
+ * If the list is not long enough to have the given index the before list will
1991
+ * be the input list, and the after list will be empty.
1992
+ *
1993
+ * ## Examples
1994
+ *
1995
+ * ```gleam
1996
+ * split([6, 7, 8, 9], 0)
1997
+ * // -> #([], [6, 7, 8, 9])
1998
+ * ```
1999
+ *
2000
+ * ```gleam
2001
+ * split([6, 7, 8, 9], 2)
2002
+ * // -> #([6, 7], [8, 9])
2003
+ * ```
2004
+ *
2005
+ * ```gleam
2006
+ * split([6, 7, 8, 9], 4)
2007
+ * // -> #([6, 7, 8, 9], [])
2008
+ * ```
2009
+ */
2010
+ export function split(list, index) {
2011
+ return split_loop(list, index, toList([]));
2012
+ }
2013
+
2014
+ function split_while_loop(loop$list, loop$f, loop$acc) {
2015
+ while (true) {
2016
+ let list = loop$list;
2017
+ let f = loop$f;
2018
+ let acc = loop$acc;
2019
+ if (list instanceof $Empty) {
2020
+ return [reverse(acc), toList([])];
2021
+ } else {
2022
+ let first$1 = list.head;
2023
+ let rest$1 = list.tail;
2024
+ let $ = f(first$1);
2025
+ if ($) {
2026
+ loop$list = rest$1;
2027
+ loop$f = f;
2028
+ loop$acc = listPrepend(first$1, acc);
2029
+ } else {
2030
+ return [reverse(acc), list];
2031
+ }
2032
+ }
2033
+ }
2034
+ }
2035
+
2036
+ /**
2037
+ * Splits a list in two before the first element that a given function returns
2038
+ * `False` for.
2039
+ *
2040
+ * If the function returns `True` for all elements the first list will be the
2041
+ * input list, and the second list will be empty.
2042
+ *
2043
+ * ## Examples
2044
+ *
2045
+ * ```gleam
2046
+ * split_while([1, 2, 3, 4, 5], fn(x) { x <= 3 })
2047
+ * // -> #([1, 2, 3], [4, 5])
2048
+ * ```
2049
+ *
2050
+ * ```gleam
2051
+ * split_while([1, 2, 3, 4, 5], fn(x) { x <= 5 })
2052
+ * // -> #([1, 2, 3, 4, 5], [])
2053
+ * ```
2054
+ */
2055
+ export function split_while(list, predicate) {
2056
+ return split_while_loop(list, predicate, toList([]));
2057
+ }
2058
+
2059
+ /**
2060
+ * Given a list of 2-element tuples, finds the first tuple that has a given
2061
+ * key as the first element and returns the second element.
2062
+ *
2063
+ * If no tuple is found with the given key then `Error(Nil)` is returned.
2064
+ *
2065
+ * This function may be useful for interacting with Erlang code where lists of
2066
+ * tuples are common.
2067
+ *
2068
+ * ## Examples
2069
+ *
2070
+ * ```gleam
2071
+ * key_find([#("a", 0), #("b", 1)], "a")
2072
+ * // -> Ok(0)
2073
+ * ```
2074
+ *
2075
+ * ```gleam
2076
+ * key_find([#("a", 0), #("b", 1)], "b")
2077
+ * // -> Ok(1)
2078
+ * ```
2079
+ *
2080
+ * ```gleam
2081
+ * key_find([#("a", 0), #("b", 1)], "c")
2082
+ * // -> Error(Nil)
2083
+ * ```
2084
+ */
2085
+ export function key_find(keyword_list, desired_key) {
2086
+ return find_map(
2087
+ keyword_list,
2088
+ (keyword) => {
2089
+ let key;
2090
+ let value;
2091
+ key = keyword[0];
2092
+ value = keyword[1];
2093
+ let $ = isEqual(key, desired_key);
2094
+ if ($) {
2095
+ return new Ok(value);
2096
+ } else {
2097
+ return new Error(undefined);
2098
+ }
2099
+ },
2100
+ );
2101
+ }
2102
+
2103
+ /**
2104
+ * Given a list of 2-element tuples, finds all tuples that have a given
2105
+ * key as the first element and returns the second element.
2106
+ *
2107
+ * This function may be useful for interacting with Erlang code where lists of
2108
+ * tuples are common.
2109
+ *
2110
+ * ## Examples
2111
+ *
2112
+ * ```gleam
2113
+ * key_filter([#("a", 0), #("b", 1), #("a", 2)], "a")
2114
+ * // -> [0, 2]
2115
+ * ```
2116
+ *
2117
+ * ```gleam
2118
+ * key_filter([#("a", 0), #("b", 1)], "c")
2119
+ * // -> []
2120
+ * ```
2121
+ */
2122
+ export function key_filter(keyword_list, desired_key) {
2123
+ return filter_map(
2124
+ keyword_list,
2125
+ (keyword) => {
2126
+ let key;
2127
+ let value;
2128
+ key = keyword[0];
2129
+ value = keyword[1];
2130
+ let $ = isEqual(key, desired_key);
2131
+ if ($) {
2132
+ return new Ok(value);
2133
+ } else {
2134
+ return new Error(undefined);
2135
+ }
2136
+ },
2137
+ );
2138
+ }
2139
+
2140
+ function key_pop_loop(loop$list, loop$key, loop$checked) {
2141
+ while (true) {
2142
+ let list = loop$list;
2143
+ let key = loop$key;
2144
+ let checked = loop$checked;
2145
+ if (list instanceof $Empty) {
2146
+ return new Error(undefined);
2147
+ } else {
2148
+ let k = list.head[0];
2149
+ if (isEqual(k, key)) {
2150
+ let rest$1 = list.tail;
2151
+ let v = list.head[1];
2152
+ return new Ok([v, reverse_and_prepend(checked, rest$1)]);
2153
+ } else {
2154
+ let first$1 = list.head;
2155
+ let rest$1 = list.tail;
2156
+ loop$list = rest$1;
2157
+ loop$key = key;
2158
+ loop$checked = listPrepend(first$1, checked);
2159
+ }
2160
+ }
2161
+ }
2162
+ }
2163
+
2164
+ /**
2165
+ * Given a list of 2-element tuples, finds the first tuple that has a given
2166
+ * key as the first element. This function will return the second element
2167
+ * of the found tuple and list with tuple removed.
2168
+ *
2169
+ * If no tuple is found with the given key then `Error(Nil)` is returned.
2170
+ *
2171
+ * ## Examples
2172
+ *
2173
+ * ```gleam
2174
+ * key_pop([#("a", 0), #("b", 1)], "a")
2175
+ * // -> Ok(#(0, [#("b", 1)]))
2176
+ * ```
2177
+ *
2178
+ * ```gleam
2179
+ * key_pop([#("a", 0), #("b", 1)], "b")
2180
+ * // -> Ok(#(1, [#("a", 0)]))
2181
+ * ```
2182
+ *
2183
+ * ```gleam
2184
+ * key_pop([#("a", 0), #("b", 1)], "c")
2185
+ * // -> Error(Nil)
2186
+ * ```
2187
+ */
2188
+ export function key_pop(list, key) {
2189
+ return key_pop_loop(list, key, toList([]));
2190
+ }
2191
+
2192
+ function key_set_loop(loop$list, loop$key, loop$value, loop$inspected) {
2193
+ while (true) {
2194
+ let list = loop$list;
2195
+ let key = loop$key;
2196
+ let value = loop$value;
2197
+ let inspected = loop$inspected;
2198
+ if (list instanceof $Empty) {
2199
+ return reverse(listPrepend([key, value], inspected));
2200
+ } else {
2201
+ let k = list.head[0];
2202
+ if (isEqual(k, key)) {
2203
+ let rest$1 = list.tail;
2204
+ return reverse_and_prepend(inspected, listPrepend([k, value], rest$1));
2205
+ } else {
2206
+ let first$1 = list.head;
2207
+ let rest$1 = list.tail;
2208
+ loop$list = rest$1;
2209
+ loop$key = key;
2210
+ loop$value = value;
2211
+ loop$inspected = listPrepend(first$1, inspected);
2212
+ }
2213
+ }
2214
+ }
2215
+ }
2216
+
2217
+ /**
2218
+ * Given a list of 2-element tuples, inserts a key and value into the list.
2219
+ *
2220
+ * If there was already a tuple with the key then it is replaced, otherwise it
2221
+ * is added to the end of the list.
2222
+ *
2223
+ * ## Examples
2224
+ *
2225
+ * ```gleam
2226
+ * key_set([#(5, 0), #(4, 1)], 4, 100)
2227
+ * // -> [#(5, 0), #(4, 100)]
2228
+ * ```
2229
+ *
2230
+ * ```gleam
2231
+ * key_set([#(5, 0), #(4, 1)], 1, 100)
2232
+ * // -> [#(5, 0), #(4, 1), #(1, 100)]
2233
+ * ```
2234
+ */
2235
+ export function key_set(list, key, value) {
2236
+ return key_set_loop(list, key, value, toList([]));
2237
+ }
2238
+
2239
+ /**
2240
+ * Calls a function for each element in a list, discarding the return value.
2241
+ *
2242
+ * Useful for calling a side effect for every item of a list.
2243
+ *
2244
+ * ```gleam
2245
+ * import gleam/io
2246
+ *
2247
+ * each(["1", "2", "3"], io.println)
2248
+ * // -> Nil
2249
+ * // 1
2250
+ * // 2
2251
+ * // 3
2252
+ * ```
2253
+ */
2254
+ export function each(loop$list, loop$f) {
2255
+ while (true) {
2256
+ let list = loop$list;
2257
+ let f = loop$f;
2258
+ if (list instanceof $Empty) {
2259
+ return undefined;
2260
+ } else {
2261
+ let first$1 = list.head;
2262
+ let rest$1 = list.tail;
2263
+ f(first$1);
2264
+ loop$list = rest$1;
2265
+ loop$f = f;
2266
+ }
2267
+ }
2268
+ }
2269
+
2270
+ /**
2271
+ * Calls a `Result` returning function for each element in a list, discarding
2272
+ * the return value. If the function returns `Error` then the iteration is
2273
+ * stopped and the error is returned.
2274
+ *
2275
+ * Useful for calling a side effect for every item of a list.
2276
+ *
2277
+ * ## Examples
2278
+ *
2279
+ * ```gleam
2280
+ * try_each(
2281
+ * over: [1, 2, 3],
2282
+ * with: function_that_might_fail,
2283
+ * )
2284
+ * // -> Ok(Nil)
2285
+ * ```
2286
+ */
2287
+ export function try_each(loop$list, loop$fun) {
2288
+ while (true) {
2289
+ let list = loop$list;
2290
+ let fun = loop$fun;
2291
+ if (list instanceof $Empty) {
2292
+ return new Ok(undefined);
2293
+ } else {
2294
+ let first$1 = list.head;
2295
+ let rest$1 = list.tail;
2296
+ let $ = fun(first$1);
2297
+ if ($ instanceof Ok) {
2298
+ loop$list = rest$1;
2299
+ loop$fun = fun;
2300
+ } else {
2301
+ return $;
2302
+ }
2303
+ }
2304
+ }
2305
+ }
2306
+
2307
+ function partition_loop(loop$list, loop$categorise, loop$trues, loop$falses) {
2308
+ while (true) {
2309
+ let list = loop$list;
2310
+ let categorise = loop$categorise;
2311
+ let trues = loop$trues;
2312
+ let falses = loop$falses;
2313
+ if (list instanceof $Empty) {
2314
+ return [reverse(trues), reverse(falses)];
2315
+ } else {
2316
+ let first$1 = list.head;
2317
+ let rest$1 = list.tail;
2318
+ let $ = categorise(first$1);
2319
+ if ($) {
2320
+ loop$list = rest$1;
2321
+ loop$categorise = categorise;
2322
+ loop$trues = listPrepend(first$1, trues);
2323
+ loop$falses = falses;
2324
+ } else {
2325
+ loop$list = rest$1;
2326
+ loop$categorise = categorise;
2327
+ loop$trues = trues;
2328
+ loop$falses = listPrepend(first$1, falses);
2329
+ }
2330
+ }
2331
+ }
2332
+ }
2333
+
2334
+ /**
2335
+ * Partitions a list into a tuple/pair of lists
2336
+ * by a given categorisation function.
2337
+ *
2338
+ * ## Examples
2339
+ *
2340
+ * ```gleam
2341
+ * import gleam/int
2342
+ *
2343
+ * [1, 2, 3, 4, 5] |> partition(int.is_odd)
2344
+ * // -> #([1, 3, 5], [2, 4])
2345
+ * ```
2346
+ */
2347
+ export function partition(list, categorise) {
2348
+ return partition_loop(list, categorise, toList([]), toList([]));
2349
+ }
2350
+
2351
+ function window_loop(loop$acc, loop$list, loop$n) {
2352
+ while (true) {
2353
+ let acc = loop$acc;
2354
+ let list = loop$list;
2355
+ let n = loop$n;
2356
+ let window$1 = take(list, n);
2357
+ let $ = length(window$1) === n;
2358
+ if ($) {
2359
+ loop$acc = listPrepend(window$1, acc);
2360
+ loop$list = drop(list, 1);
2361
+ loop$n = n;
2362
+ } else {
2363
+ return reverse(acc);
2364
+ }
2365
+ }
2366
+ }
2367
+
2368
+ /**
2369
+ * Returns a list of sliding windows.
2370
+ *
2371
+ * ## Examples
2372
+ *
2373
+ * ```gleam
2374
+ * window([1,2,3,4,5], 3)
2375
+ * // -> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
2376
+ * ```
2377
+ *
2378
+ * ```gleam
2379
+ * window([1, 2], 4)
2380
+ * // -> []
2381
+ * ```
2382
+ */
2383
+ export function window(list, n) {
2384
+ let $ = n <= 0;
2385
+ if ($) {
2386
+ return toList([]);
2387
+ } else {
2388
+ return window_loop(toList([]), list, n);
2389
+ }
2390
+ }
2391
+
2392
+ /**
2393
+ * Returns a list of tuples containing two contiguous elements.
2394
+ *
2395
+ * ## Examples
2396
+ *
2397
+ * ```gleam
2398
+ * window_by_2([1,2,3,4])
2399
+ * // -> [#(1, 2), #(2, 3), #(3, 4)]
2400
+ * ```
2401
+ *
2402
+ * ```gleam
2403
+ * window_by_2([1])
2404
+ * // -> []
2405
+ * ```
2406
+ */
2407
+ export function window_by_2(list) {
2408
+ return zip(list, drop(list, 1));
2409
+ }
2410
+
2411
+ /**
2412
+ * Drops the first elements in a given list for which the predicate function returns `True`.
2413
+ *
2414
+ * ## Examples
2415
+ *
2416
+ * ```gleam
2417
+ * drop_while([1, 2, 3, 4], fn (x) { x < 3 })
2418
+ * // -> [3, 4]
2419
+ * ```
2420
+ */
2421
+ export function drop_while(loop$list, loop$predicate) {
2422
+ while (true) {
2423
+ let list = loop$list;
2424
+ let predicate = loop$predicate;
2425
+ if (list instanceof $Empty) {
2426
+ return list;
2427
+ } else {
2428
+ let first$1 = list.head;
2429
+ let rest$1 = list.tail;
2430
+ let $ = predicate(first$1);
2431
+ if ($) {
2432
+ loop$list = rest$1;
2433
+ loop$predicate = predicate;
2434
+ } else {
2435
+ return listPrepend(first$1, rest$1);
2436
+ }
2437
+ }
2438
+ }
2439
+ }
2440
+
2441
+ function take_while_loop(loop$list, loop$predicate, loop$acc) {
2442
+ while (true) {
2443
+ let list = loop$list;
2444
+ let predicate = loop$predicate;
2445
+ let acc = loop$acc;
2446
+ if (list instanceof $Empty) {
2447
+ return reverse(acc);
2448
+ } else {
2449
+ let first$1 = list.head;
2450
+ let rest$1 = list.tail;
2451
+ let $ = predicate(first$1);
2452
+ if ($) {
2453
+ loop$list = rest$1;
2454
+ loop$predicate = predicate;
2455
+ loop$acc = listPrepend(first$1, acc);
2456
+ } else {
2457
+ return reverse(acc);
2458
+ }
2459
+ }
2460
+ }
2461
+ }
2462
+
2463
+ /**
2464
+ * Takes the first elements in a given list for which the predicate function returns `True`.
2465
+ *
2466
+ * ## Examples
2467
+ *
2468
+ * ```gleam
2469
+ * take_while([1, 2, 3, 2, 4], fn (x) { x < 3 })
2470
+ * // -> [1, 2]
2471
+ * ```
2472
+ */
2473
+ export function take_while(list, predicate) {
2474
+ return take_while_loop(list, predicate, toList([]));
2475
+ }
2476
+
2477
+ function chunk_loop(
2478
+ loop$list,
2479
+ loop$f,
2480
+ loop$previous_key,
2481
+ loop$current_chunk,
2482
+ loop$acc
2483
+ ) {
2484
+ while (true) {
2485
+ let list = loop$list;
2486
+ let f = loop$f;
2487
+ let previous_key = loop$previous_key;
2488
+ let current_chunk = loop$current_chunk;
2489
+ let acc = loop$acc;
2490
+ if (list instanceof $Empty) {
2491
+ return reverse(listPrepend(reverse(current_chunk), acc));
2492
+ } else {
2493
+ let first$1 = list.head;
2494
+ let rest$1 = list.tail;
2495
+ let key = f(first$1);
2496
+ let $ = isEqual(key, previous_key);
2497
+ if ($) {
2498
+ loop$list = rest$1;
2499
+ loop$f = f;
2500
+ loop$previous_key = key;
2501
+ loop$current_chunk = listPrepend(first$1, current_chunk);
2502
+ loop$acc = acc;
2503
+ } else {
2504
+ let new_acc = listPrepend(reverse(current_chunk), acc);
2505
+ loop$list = rest$1;
2506
+ loop$f = f;
2507
+ loop$previous_key = key;
2508
+ loop$current_chunk = toList([first$1]);
2509
+ loop$acc = new_acc;
2510
+ }
2511
+ }
2512
+ }
2513
+ }
2514
+
2515
+ /**
2516
+ * Returns a list of chunks in which
2517
+ * the return value of calling `f` on each element is the same.
2518
+ *
2519
+ * ## Examples
2520
+ *
2521
+ * ```gleam
2522
+ * [1, 2, 2, 3, 4, 4, 6, 7, 7] |> chunk(by: fn(n) { n % 2 })
2523
+ * // -> [[1], [2, 2], [3], [4, 4, 6], [7, 7]]
2524
+ * ```
2525
+ */
2526
+ export function chunk(list, f) {
2527
+ if (list instanceof $Empty) {
2528
+ return list;
2529
+ } else {
2530
+ let first$1 = list.head;
2531
+ let rest$1 = list.tail;
2532
+ return chunk_loop(rest$1, f, f(first$1), toList([first$1]), toList([]));
2533
+ }
2534
+ }
2535
+
2536
+ function sized_chunk_loop(
2537
+ loop$list,
2538
+ loop$count,
2539
+ loop$left,
2540
+ loop$current_chunk,
2541
+ loop$acc
2542
+ ) {
2543
+ while (true) {
2544
+ let list = loop$list;
2545
+ let count = loop$count;
2546
+ let left = loop$left;
2547
+ let current_chunk = loop$current_chunk;
2548
+ let acc = loop$acc;
2549
+ if (list instanceof $Empty) {
2550
+ if (current_chunk instanceof $Empty) {
2551
+ return reverse(acc);
2552
+ } else {
2553
+ let remaining = current_chunk;
2554
+ return reverse(listPrepend(reverse(remaining), acc));
2555
+ }
2556
+ } else {
2557
+ let first$1 = list.head;
2558
+ let rest$1 = list.tail;
2559
+ let chunk$1 = listPrepend(first$1, current_chunk);
2560
+ let $ = left > 1;
2561
+ if ($) {
2562
+ loop$list = rest$1;
2563
+ loop$count = count;
2564
+ loop$left = left - 1;
2565
+ loop$current_chunk = chunk$1;
2566
+ loop$acc = acc;
2567
+ } else {
2568
+ loop$list = rest$1;
2569
+ loop$count = count;
2570
+ loop$left = count;
2571
+ loop$current_chunk = toList([]);
2572
+ loop$acc = listPrepend(reverse(chunk$1), acc);
2573
+ }
2574
+ }
2575
+ }
2576
+ }
2577
+
2578
+ /**
2579
+ * Returns a list of chunks containing `count` elements each.
2580
+ *
2581
+ * If the last chunk does not have `count` elements, it is instead
2582
+ * a partial chunk, with less than `count` elements.
2583
+ *
2584
+ * For any `count` less than 1 this function behaves as if it was set to 1.
2585
+ *
2586
+ * ## Examples
2587
+ *
2588
+ * ```gleam
2589
+ * [1, 2, 3, 4, 5, 6] |> sized_chunk(into: 2)
2590
+ * // -> [[1, 2], [3, 4], [5, 6]]
2591
+ * ```
2592
+ *
2593
+ * ```gleam
2594
+ * [1, 2, 3, 4, 5, 6, 7, 8] |> sized_chunk(into: 3)
2595
+ * // -> [[1, 2, 3], [4, 5, 6], [7, 8]]
2596
+ * ```
2597
+ */
2598
+ export function sized_chunk(list, count) {
2599
+ return sized_chunk_loop(list, count, count, toList([]), toList([]));
2600
+ }
2601
+
2602
+ /**
2603
+ * This function acts similar to fold, but does not take an initial state.
2604
+ * Instead, it starts from the first element in the list
2605
+ * and combines it with each subsequent element in turn using the given
2606
+ * function. The function is called as `fun(accumulator, current_element)`.
2607
+ *
2608
+ * Returns `Ok` to indicate a successful run, and `Error` if called on an
2609
+ * empty list.
2610
+ *
2611
+ * ## Examples
2612
+ *
2613
+ * ```gleam
2614
+ * [] |> reduce(fn(acc, x) { acc + x })
2615
+ * // -> Error(Nil)
2616
+ * ```
2617
+ *
2618
+ * ```gleam
2619
+ * [1, 2, 3, 4, 5] |> reduce(fn(acc, x) { acc + x })
2620
+ * // -> Ok(15)
2621
+ * ```
2622
+ */
2623
+ export function reduce(list, fun) {
2624
+ if (list instanceof $Empty) {
2625
+ return new Error(undefined);
2626
+ } else {
2627
+ let first$1 = list.head;
2628
+ let rest$1 = list.tail;
2629
+ return new Ok(fold(rest$1, first$1, fun));
2630
+ }
2631
+ }
2632
+
2633
+ function scan_loop(loop$list, loop$accumulator, loop$accumulated, loop$fun) {
2634
+ while (true) {
2635
+ let list = loop$list;
2636
+ let accumulator = loop$accumulator;
2637
+ let accumulated = loop$accumulated;
2638
+ let fun = loop$fun;
2639
+ if (list instanceof $Empty) {
2640
+ return reverse(accumulated);
2641
+ } else {
2642
+ let first$1 = list.head;
2643
+ let rest$1 = list.tail;
2644
+ let next = fun(accumulator, first$1);
2645
+ loop$list = rest$1;
2646
+ loop$accumulator = next;
2647
+ loop$accumulated = listPrepend(next, accumulated);
2648
+ loop$fun = fun;
2649
+ }
2650
+ }
2651
+ }
2652
+
2653
+ /**
2654
+ * Similar to `fold`, but yields the state of the accumulator at each stage.
2655
+ *
2656
+ * ## Examples
2657
+ *
2658
+ * ```gleam
2659
+ * scan(over: [1, 2, 3], from: 100, with: fn(acc, i) { acc + i })
2660
+ * // -> [101, 103, 106]
2661
+ * ```
2662
+ */
2663
+ export function scan(list, initial, fun) {
2664
+ return scan_loop(list, initial, toList([]), fun);
2665
+ }
2666
+
2667
+ /**
2668
+ * Returns the last element in the given list.
2669
+ *
2670
+ * Returns `Error(Nil)` if the list is empty.
2671
+ *
2672
+ * This function runs in linear time.
2673
+ *
2674
+ * ## Examples
2675
+ *
2676
+ * ```gleam
2677
+ * last([])
2678
+ * // -> Error(Nil)
2679
+ * ```
2680
+ *
2681
+ * ```gleam
2682
+ * last([1, 2, 3, 4, 5])
2683
+ * // -> Ok(5)
2684
+ * ```
2685
+ */
2686
+ export function last(loop$list) {
2687
+ while (true) {
2688
+ let list = loop$list;
2689
+ if (list instanceof $Empty) {
2690
+ return new Error(undefined);
2691
+ } else {
2692
+ let $ = list.tail;
2693
+ if ($ instanceof $Empty) {
2694
+ let last$1 = list.head;
2695
+ return new Ok(last$1);
2696
+ } else {
2697
+ let rest$1 = $;
2698
+ loop$list = rest$1;
2699
+ }
2700
+ }
2701
+ }
2702
+ }
2703
+
2704
+ /**
2705
+ * Return unique combinations of elements in the list.
2706
+ *
2707
+ * ## Examples
2708
+ *
2709
+ * ```gleam
2710
+ * combinations([1, 2, 3], 2)
2711
+ * // -> [[1, 2], [1, 3], [2, 3]]
2712
+ * ```
2713
+ *
2714
+ * ```gleam
2715
+ * combinations([1, 2, 3, 4], 3)
2716
+ * // -> [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
2717
+ * ```
2718
+ */
2719
+ export function combinations(items, n) {
2720
+ if (n === 0) {
2721
+ return toList([toList([])]);
2722
+ } else if (items instanceof $Empty) {
2723
+ return items;
2724
+ } else {
2725
+ let first$1 = items.head;
2726
+ let rest$1 = items.tail;
2727
+ let _pipe = rest$1;
2728
+ let _pipe$1 = combinations(_pipe, n - 1);
2729
+ let _pipe$2 = map(
2730
+ _pipe$1,
2731
+ (combination) => { return listPrepend(first$1, combination); },
2732
+ );
2733
+ let _pipe$3 = reverse(_pipe$2);
2734
+ return fold(
2735
+ _pipe$3,
2736
+ combinations(rest$1, n),
2737
+ (acc, c) => { return listPrepend(c, acc); },
2738
+ );
2739
+ }
2740
+ }
2741
+
2742
+ function combination_pairs_loop(loop$items, loop$acc) {
2743
+ while (true) {
2744
+ let items = loop$items;
2745
+ let acc = loop$acc;
2746
+ if (items instanceof $Empty) {
2747
+ return reverse(acc);
2748
+ } else {
2749
+ let first$1 = items.head;
2750
+ let rest$1 = items.tail;
2751
+ let first_combinations = map(
2752
+ rest$1,
2753
+ (other) => { return [first$1, other]; },
2754
+ );
2755
+ let acc$1 = reverse_and_prepend(first_combinations, acc);
2756
+ loop$items = rest$1;
2757
+ loop$acc = acc$1;
2758
+ }
2759
+ }
2760
+ }
2761
+
2762
+ /**
2763
+ * Return unique pair combinations of elements in the list.
2764
+ *
2765
+ * ## Examples
2766
+ *
2767
+ * ```gleam
2768
+ * combination_pairs([1, 2, 3])
2769
+ * // -> [#(1, 2), #(1, 3), #(2, 3)]
2770
+ * ```
2771
+ */
2772
+ export function combination_pairs(items) {
2773
+ return combination_pairs_loop(items, toList([]));
2774
+ }
2775
+
2776
+ function take_firsts(loop$rows, loop$column, loop$remaining_rows) {
2777
+ while (true) {
2778
+ let rows = loop$rows;
2779
+ let column = loop$column;
2780
+ let remaining_rows = loop$remaining_rows;
2781
+ if (rows instanceof $Empty) {
2782
+ return [reverse(column), reverse(remaining_rows)];
2783
+ } else {
2784
+ let $ = rows.head;
2785
+ if ($ instanceof $Empty) {
2786
+ let rest$1 = rows.tail;
2787
+ loop$rows = rest$1;
2788
+ loop$column = column;
2789
+ loop$remaining_rows = remaining_rows;
2790
+ } else {
2791
+ let rest_rows = rows.tail;
2792
+ let first$1 = $.head;
2793
+ let remaining_row = $.tail;
2794
+ let remaining_rows$1 = listPrepend(remaining_row, remaining_rows);
2795
+ loop$rows = rest_rows;
2796
+ loop$column = listPrepend(first$1, column);
2797
+ loop$remaining_rows = remaining_rows$1;
2798
+ }
2799
+ }
2800
+ }
2801
+ }
2802
+
2803
+ function transpose_loop(loop$rows, loop$columns) {
2804
+ while (true) {
2805
+ let rows = loop$rows;
2806
+ let columns = loop$columns;
2807
+ if (rows instanceof $Empty) {
2808
+ return reverse(columns);
2809
+ } else {
2810
+ let $ = take_firsts(rows, toList([]), toList([]));
2811
+ let column;
2812
+ let rest$1;
2813
+ column = $[0];
2814
+ rest$1 = $[1];
2815
+ if (column instanceof $Empty) {
2816
+ loop$rows = rest$1;
2817
+ loop$columns = columns;
2818
+ } else {
2819
+ loop$rows = rest$1;
2820
+ loop$columns = listPrepend(column, columns);
2821
+ }
2822
+ }
2823
+ }
2824
+ }
2825
+
2826
+ /**
2827
+ * Transpose rows and columns of the list of lists.
2828
+ *
2829
+ * Notice: This function is not tail recursive,
2830
+ * and thus may exceed stack size if called,
2831
+ * with large lists (on the JavaScript target).
2832
+ *
2833
+ * ## Examples
2834
+ *
2835
+ * ```gleam
2836
+ * transpose([[1, 2, 3], [101, 102, 103]])
2837
+ * // -> [[1, 101], [2, 102], [3, 103]]
2838
+ * ```
2839
+ */
2840
+ export function transpose(list_of_lists) {
2841
+ return transpose_loop(list_of_lists, toList([]));
2842
+ }
2843
+
2844
+ /**
2845
+ * Make a list alternating the elements from the given lists
2846
+ *
2847
+ * ## Examples
2848
+ *
2849
+ * ```gleam
2850
+ * interleave([[1, 2], [101, 102], [201, 202]])
2851
+ * // -> [1, 101, 201, 2, 102, 202]
2852
+ * ```
2853
+ */
2854
+ export function interleave(list) {
2855
+ let _pipe = list;
2856
+ let _pipe$1 = transpose(_pipe);
2857
+ return flatten(_pipe$1);
2858
+ }
2859
+
2860
+ function shuffle_pair_unwrap_loop(loop$list, loop$acc) {
2861
+ while (true) {
2862
+ let list = loop$list;
2863
+ let acc = loop$acc;
2864
+ if (list instanceof $Empty) {
2865
+ return acc;
2866
+ } else {
2867
+ let elem_pair = list.head;
2868
+ let enumerable = list.tail;
2869
+ loop$list = enumerable;
2870
+ loop$acc = listPrepend(elem_pair[1], acc);
2871
+ }
2872
+ }
2873
+ }
2874
+
2875
+ function do_shuffle_by_pair_indexes(list_of_pairs) {
2876
+ return sort(
2877
+ list_of_pairs,
2878
+ (a_pair, b_pair) => { return $float.compare(a_pair[0], b_pair[0]); },
2879
+ );
2880
+ }
2881
+
2882
+ /**
2883
+ * Takes a list, randomly sorts all items and returns the shuffled list.
2884
+ *
2885
+ * This function uses `float.random` to decide the order of the elements.
2886
+ *
2887
+ * ## Example
2888
+ *
2889
+ * ```gleam
2890
+ * range(1, 10) |> shuffle()
2891
+ * // -> [1, 6, 9, 10, 3, 8, 4, 2, 7, 5]
2892
+ * ```
2893
+ */
2894
+ export function shuffle(list) {
2895
+ let _pipe = list;
2896
+ let _pipe$1 = fold(
2897
+ _pipe,
2898
+ toList([]),
2899
+ (acc, a) => { return listPrepend([$float.random(), a], acc); },
2900
+ );
2901
+ let _pipe$2 = do_shuffle_by_pair_indexes(_pipe$1);
2902
+ return shuffle_pair_unwrap_loop(_pipe$2, toList([]));
2903
+ }
2904
+
2905
+ function max_loop(loop$list, loop$compare, loop$max) {
2906
+ while (true) {
2907
+ let list = loop$list;
2908
+ let compare = loop$compare;
2909
+ let max = loop$max;
2910
+ if (list instanceof $Empty) {
2911
+ return max;
2912
+ } else {
2913
+ let first$1 = list.head;
2914
+ let rest$1 = list.tail;
2915
+ let $ = compare(first$1, max);
2916
+ if ($ instanceof $order.Lt) {
2917
+ loop$list = rest$1;
2918
+ loop$compare = compare;
2919
+ loop$max = max;
2920
+ } else if ($ instanceof $order.Eq) {
2921
+ loop$list = rest$1;
2922
+ loop$compare = compare;
2923
+ loop$max = max;
2924
+ } else {
2925
+ loop$list = rest$1;
2926
+ loop$compare = compare;
2927
+ loop$max = first$1;
2928
+ }
2929
+ }
2930
+ }
2931
+ }
2932
+
2933
+ /**
2934
+ * Takes a list and a comparator, and returns the maximum element in the list
2935
+ *
2936
+ * ## Examples
2937
+ *
2938
+ * ```gleam
2939
+ * range(1, 10) |> list.max(int.compare)
2940
+ * // -> Ok(10)
2941
+ * ```
2942
+ *
2943
+ * ```gleam
2944
+ * ["a", "c", "b"] |> list.max(string.compare)
2945
+ * // -> Ok("c")
2946
+ * ```
2947
+ */
2948
+ export function max(list, compare) {
2949
+ if (list instanceof $Empty) {
2950
+ return new Error(undefined);
2951
+ } else {
2952
+ let first$1 = list.head;
2953
+ let rest$1 = list.tail;
2954
+ return new Ok(max_loop(rest$1, compare, first$1));
2955
+ }
2956
+ }
2957
+
2958
+ function build_reservoir_loop(loop$list, loop$size, loop$reservoir) {
2959
+ while (true) {
2960
+ let list = loop$list;
2961
+ let size = loop$size;
2962
+ let reservoir = loop$reservoir;
2963
+ let reservoir_size = $dict.size(reservoir);
2964
+ let $ = reservoir_size >= size;
2965
+ if ($) {
2966
+ return [reservoir, list];
2967
+ } else {
2968
+ if (list instanceof $Empty) {
2969
+ return [reservoir, toList([])];
2970
+ } else {
2971
+ let first$1 = list.head;
2972
+ let rest$1 = list.tail;
2973
+ let reservoir$1 = $dict.insert(reservoir, reservoir_size, first$1);
2974
+ loop$list = rest$1;
2975
+ loop$size = size;
2976
+ loop$reservoir = reservoir$1;
2977
+ }
2978
+ }
2979
+ }
2980
+ }
2981
+
2982
+ /**
2983
+ * Builds the initial reservoir used by Algorithm L.
2984
+ * This is a dictionary with keys ranging from `0` up to `n - 1` where each
2985
+ * value is the corresponding element at that position in `list`.
2986
+ *
2987
+ * This also returns the remaining elements of `list` that didn't end up in
2988
+ * the reservoir.
2989
+ *
2990
+ * @ignore
2991
+ */
2992
+ function build_reservoir(list, n) {
2993
+ return build_reservoir_loop(list, n, $dict.new$());
2994
+ }
2995
+
2996
+ function log_random() {
2997
+ let $ = $float.logarithm($float.random() + min_positive);
2998
+ let random;
2999
+ if ($ instanceof Ok) {
3000
+ random = $[0];
3001
+ } else {
3002
+ throw makeError(
3003
+ "let_assert",
3004
+ FILEPATH,
3005
+ "gleam/list",
3006
+ 2354,
3007
+ "log_random",
3008
+ "Pattern match failed, no pattern matched the value.",
3009
+ {
3010
+ value: $,
3011
+ start: 55356,
3012
+ end: 55427,
3013
+ pattern_start: 55367,
3014
+ pattern_end: 55377
3015
+ }
3016
+ )
3017
+ }
3018
+ return random;
3019
+ }
3020
+
3021
+ function sample_loop(loop$list, loop$reservoir, loop$n, loop$w) {
3022
+ while (true) {
3023
+ let list = loop$list;
3024
+ let reservoir = loop$reservoir;
3025
+ let n = loop$n;
3026
+ let w = loop$w;
3027
+ let _block;
3028
+ {
3029
+ let $ = $float.logarithm(1.0 - w);
3030
+ let log;
3031
+ if ($ instanceof Ok) {
3032
+ log = $[0];
3033
+ } else {
3034
+ throw makeError(
3035
+ "let_assert",
3036
+ FILEPATH,
3037
+ "gleam/list",
3038
+ 2337,
3039
+ "sample_loop",
3040
+ "Pattern match failed, no pattern matched the value.",
3041
+ {
3042
+ value: $,
3043
+ start: 54917,
3044
+ end: 54963,
3045
+ pattern_start: 54928,
3046
+ pattern_end: 54935
3047
+ }
3048
+ )
3049
+ }
3050
+ _block = $float.round($float.floor(divideFloat(log_random(), log)));
3051
+ }
3052
+ let skip = _block;
3053
+ let $ = drop(list, skip);
3054
+ if ($ instanceof $Empty) {
3055
+ return reservoir;
3056
+ } else {
3057
+ let first$1 = $.head;
3058
+ let rest$1 = $.tail;
3059
+ let reservoir$1 = $dict.insert(reservoir, $int.random(n), first$1);
3060
+ let w$1 = w * $float.exponential(
3061
+ divideFloat(log_random(), $int.to_float(n)),
3062
+ );
3063
+ loop$list = rest$1;
3064
+ loop$reservoir = reservoir$1;
3065
+ loop$n = n;
3066
+ loop$w = w$1;
3067
+ }
3068
+ }
3069
+ }
3070
+
3071
+ /**
3072
+ * Returns a random sample of up to n elements from a list using reservoir
3073
+ * sampling via [Algorithm L](https://en.wikipedia.org/wiki/Reservoir_sampling#Optimal:_Algorithm_L).
3074
+ * Returns an empty list if the sample size is less than or equal to 0.
3075
+ *
3076
+ * Order is not random, only selection is.
3077
+ *
3078
+ * ## Examples
3079
+ *
3080
+ * ```gleam
3081
+ * reservoir_sample([1, 2, 3, 4, 5], 3)
3082
+ * // -> [2, 4, 5] // A random sample of 3 items
3083
+ * ```
3084
+ */
3085
+ export function sample(list, n) {
3086
+ let $ = build_reservoir(list, n);
3087
+ let reservoir;
3088
+ let rest$1;
3089
+ reservoir = $[0];
3090
+ rest$1 = $[1];
3091
+ let $1 = $dict.is_empty(reservoir);
3092
+ if ($1) {
3093
+ return toList([]);
3094
+ } else {
3095
+ let w = $float.exponential(divideFloat(log_random(), $int.to_float(n)));
3096
+ return $dict.values(sample_loop(rest$1, reservoir, n, w));
3097
+ }
3098
+ }
3099
+
3100
+ function permutation_zip(list, rest, acc) {
3101
+ if (list instanceof $Empty) {
3102
+ return reverse(acc);
3103
+ } else {
3104
+ let head = list.head;
3105
+ let tail = list.tail;
3106
+ return permutation_prepend(
3107
+ head,
3108
+ permutations(reverse_and_prepend(rest, tail)),
3109
+ tail,
3110
+ listPrepend(head, rest),
3111
+ acc,
3112
+ );
3113
+ }
3114
+ }
3115
+
3116
+ /**
3117
+ * Returns all the permutations of a list.
3118
+ *
3119
+ * ## Examples
3120
+ *
3121
+ * ```gleam
3122
+ * permutations([1, 2])
3123
+ * // -> [[1, 2], [2, 1]]
3124
+ * ```
3125
+ */
3126
+ export function permutations(list) {
3127
+ if (list instanceof $Empty) {
3128
+ return toList([toList([])]);
3129
+ } else {
3130
+ let l = list;
3131
+ return permutation_zip(l, toList([]), toList([]));
3132
+ }
3133
+ }
3134
+
3135
+ function permutation_prepend(
3136
+ loop$el,
3137
+ loop$permutations,
3138
+ loop$list_1,
3139
+ loop$list_2,
3140
+ loop$acc
3141
+ ) {
3142
+ while (true) {
3143
+ let el = loop$el;
3144
+ let permutations = loop$permutations;
3145
+ let list_1 = loop$list_1;
3146
+ let list_2 = loop$list_2;
3147
+ let acc = loop$acc;
3148
+ if (permutations instanceof $Empty) {
3149
+ return permutation_zip(list_1, list_2, acc);
3150
+ } else {
3151
+ let head = permutations.head;
3152
+ let tail = permutations.tail;
3153
+ loop$el = el;
3154
+ loop$permutations = tail;
3155
+ loop$list_1 = list_1;
3156
+ loop$list_2 = list_2;
3157
+ loop$acc = listPrepend(listPrepend(el, head), acc);
3158
+ }
3159
+ }
3160
+ }