create-mendix-widget-gleam 2.0.13 → 2.0.15
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.
- package/README.md +3 -2
- package/package.json +5 -1
- package/src/index.mjs +22 -7
- package/src/templates/readme_md.mjs +6 -6
- package/template/docs/glendix_guide.md +204 -1
- package/template/gleam.toml +1 -1
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@command.cache +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@command.cache_inline +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@command.cache_meta +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@cursor.cache +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@cursor.cache_inline +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@cursor.cache_meta +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@event.cache +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@event.cache_inline +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@event.cache_meta +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@internal@consts.cache +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@internal@consts.cache_inline +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@internal@consts.cache_meta +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@stdout.cache +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@stdout.cache_inline +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@stdout.cache_meta +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@style.cache +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@style.cache_inline +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@style.cache_meta +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@terminal.cache +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@terminal.cache_inline +0 -0
- package/tui/build/dev/javascript/etch/_gleam_artefacts/etch@terminal.cache_meta +0 -0
- package/tui/build/dev/javascript/etch/etch/command.mjs +479 -0
- package/tui/build/dev/javascript/etch/etch/cursor.mjs +164 -0
- package/tui/build/dev/javascript/etch/etch/event.mjs +2399 -0
- package/tui/build/dev/javascript/etch/etch/internal/consts.mjs +3 -0
- package/tui/build/dev/javascript/etch/etch/stdout.mjs +375 -0
- package/tui/build/dev/javascript/etch/etch/style.mjs +741 -0
- package/tui/build/dev/javascript/etch/etch/terminal.mjs +137 -0
- package/tui/build/dev/javascript/etch/gleam.mjs +1 -0
- package/tui/build/dev/javascript/etch/input/event_ffi.erl +73 -0
- package/tui/build/dev/javascript/etch/input/input_ffi.mjs +192 -0
- package/tui/build/dev/javascript/etch/input/signal_handler.erl +33 -0
- package/tui/build/dev/javascript/etch/terminal/terminal_ffi.erl +22 -0
- package/tui/build/dev/javascript/etch/terminal/terminal_ffi.mjs +37 -0
- package/tui/build/dev/javascript/etch/terminal/tty_state.erl +29 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@application.cache +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@application.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@application.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@atom.cache +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@atom.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@atom.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@charlist.cache +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@charlist.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@charlist.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@node.cache +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@node.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@node.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@port.cache +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@port.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@port.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@process.cache +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@process.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@process.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@reference.cache +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@reference.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_erlang/_gleam_artefacts/gleam@erlang@reference.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam/erlang/application.mjs +38 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam/erlang/atom.mjs +2 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam/erlang/charlist.mjs +1 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam/erlang/node.mjs +12 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam/erlang/port.mjs +1 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam/erlang/process.mjs +161 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam/erlang/reference.mjs +1 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam.mjs +1 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam@erlang@application.erl +43 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam@erlang@atom.erl +94 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam@erlang@charlist.erl +42 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam@erlang@node.erl +80 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam@erlang@port.erl +8 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam@erlang@process.erl +868 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam@erlang@reference.erl +21 -0
- package/tui/build/dev/javascript/gleam_erlang/gleam_erlang_ffi.erl +164 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@array.cache +0 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@array.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@array.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@promise.cache +0 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@promise.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@promise.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@symbol.cache +0 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@symbol.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_javascript/_gleam_artefacts/gleam@javascript@symbol.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_javascript/gleam/javascript/array.mjs +24 -0
- package/tui/build/dev/javascript/gleam_javascript/gleam/javascript/promise.mjs +105 -0
- package/tui/build/dev/javascript/gleam_javascript/gleam/javascript/symbol.mjs +7 -0
- package/tui/build/dev/javascript/gleam_javascript/gleam.mjs +1 -0
- package/tui/build/dev/javascript/gleam_javascript/gleam_javascript_ffi.mjs +133 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bit_array.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bit_array.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bit_array.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bool.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bool.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bool.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bytes_tree.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bytes_tree.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@bytes_tree.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dict.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dict.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dict.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic@decode.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic@decode.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@dynamic@decode.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@float.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@float.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@float.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@function.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@function.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@function.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@int.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@int.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@int.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@io.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@io.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@io.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@list.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@list.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@list.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@option.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@option.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@option.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@order.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@order.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@order.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@pair.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@pair.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@pair.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@result.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@result.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@result.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@set.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@set.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@set.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string_tree.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string_tree.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@string_tree.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@uri.cache +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@uri.cache_inline +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/_gleam_artefacts/gleam@uri.cache_meta +0 -0
- package/tui/build/dev/javascript/gleam_stdlib/dict.mjs +710 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/bit_array.mjs +286 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/bool.mjs +295 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/bytes_tree.mjs +225 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/dict.mjs +455 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/dynamic/decode.mjs +993 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/dynamic.mjs +35 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/float.mjs +528 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/function.mjs +6 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/int.mjs +764 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/io.mjs +8 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/list.mjs +3063 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/option.mjs +386 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/order.mjs +166 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/pair.mjs +96 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/result.mjs +448 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/set.mjs +413 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/string.mjs +695 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/string_tree.mjs +128 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam/uri.mjs +1151 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam.mjs +1 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@bit_array.erl +347 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@bool.erl +334 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@bytes_tree.erl +211 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@dict.erl +513 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@dynamic.erl +105 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@dynamic@decode.erl +1114 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@float.erl +711 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@function.erl +18 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@int.erl +972 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@io.erl +76 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@list.erl +2735 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@option.erl +381 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@order.erl +188 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@pair.erl +104 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@result.erl +500 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@set.erl +430 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@string.erl +964 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@string_tree.erl +202 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam@uri.erl +1042 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam_stdlib.erl +534 -0
- package/tui/build/dev/javascript/gleam_stdlib/gleam_stdlib.mjs +1133 -0
- package/tui/build/dev/javascript/gleam_version +1 -0
- package/tui/build/dev/javascript/prelude.mjs +1575 -0
- package/tui/build/dev/javascript/tui/_gleam_artefacts/tui.cache +0 -0
- package/tui/build/dev/javascript/tui/_gleam_artefacts/tui.cache_inline +0 -0
- package/tui/build/dev/javascript/tui/_gleam_artefacts/tui.cache_meta +0 -0
- package/tui/build/dev/javascript/tui/_gleam_artefacts/tui.cache_warnings +0 -0
- package/tui/build/dev/javascript/tui/_gleam_artefacts/tui@prompt.cache +0 -0
- package/tui/build/dev/javascript/tui/_gleam_artefacts/tui@prompt.cache_inline +0 -0
- package/tui/build/dev/javascript/tui/_gleam_artefacts/tui@prompt.cache_meta +0 -0
- package/tui/build/dev/javascript/tui/_gleam_artefacts/tui@prompt.cache_warnings +0 -0
- package/tui/build/dev/javascript/tui/gleam.mjs +1 -0
- package/tui/build/dev/javascript/tui/tui/prompt.mjs +521 -0
- package/tui/build/dev/javascript/tui/tui.mjs +334 -0
- package/tui/build/dev/javascript/tui/tui_ffi.mjs +32 -0
|
@@ -0,0 +1,2735 @@
|
|
|
1
|
+
-module(gleam@list).
|
|
2
|
+
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
|
|
3
|
+
-define(FILEPATH, "src/gleam/list.gleam").
|
|
4
|
+
-export([length/1, count/2, reverse/1, is_empty/1, contains/2, first/1, rest/1, group/2, filter/2, filter_map/2, map/2, map2/3, map_fold/3, index_map/2, try_map/2, drop/2, take/2, new/0, wrap/1, append/2, prepend/2, flatten/1, flat_map/2, fold/3, fold_right/3, index_fold/3, try_fold/3, fold_until/3, find/2, find_map/2, all/2, any/2, zip/2, strict_zip/2, unzip/1, intersperse/2, unique/1, sort/2, range/2, repeat/2, split/2, split_while/2, key_find/2, key_filter/2, key_pop/2, key_set/3, each/2, try_each/2, partition/2, window/2, window_by_2/1, drop_while/2, take_while/2, chunk/2, sized_chunk/2, reduce/2, scan/3, last/1, combinations/2, combination_pairs/1, transpose/1, interleave/1, shuffle/1, max/2, sample/2, permutations/1]).
|
|
5
|
+
-export_type([continue_or_stop/1, sorting/0]).
|
|
6
|
+
|
|
7
|
+
-if(?OTP_RELEASE >= 27).
|
|
8
|
+
-define(MODULEDOC(Str), -moduledoc(Str)).
|
|
9
|
+
-define(DOC(Str), -doc(Str)).
|
|
10
|
+
-else.
|
|
11
|
+
-define(MODULEDOC(Str), -compile([])).
|
|
12
|
+
-define(DOC(Str), -compile([])).
|
|
13
|
+
-endif.
|
|
14
|
+
|
|
15
|
+
?MODULEDOC(
|
|
16
|
+
" Lists are an ordered sequence of elements and are one of the most common\n"
|
|
17
|
+
" data types in Gleam.\n"
|
|
18
|
+
"\n"
|
|
19
|
+
" New elements can be added and removed from the front of a list in\n"
|
|
20
|
+
" constant time, while adding and removing from the end requires traversing\n"
|
|
21
|
+
" and copying the whole list, so keep this in mind when designing your\n"
|
|
22
|
+
" programs.\n"
|
|
23
|
+
"\n"
|
|
24
|
+
" There is a dedicated syntax for prefixing to a list:\n"
|
|
25
|
+
"\n"
|
|
26
|
+
" ```gleam\n"
|
|
27
|
+
" let new_list = [1, 2, ..existing_list]\n"
|
|
28
|
+
" ```\n"
|
|
29
|
+
"\n"
|
|
30
|
+
" And a matching syntax for getting the first elements of a list:\n"
|
|
31
|
+
"\n"
|
|
32
|
+
" ```gleam\n"
|
|
33
|
+
" case list {\n"
|
|
34
|
+
" [first_element, ..rest] -> first_element\n"
|
|
35
|
+
" _ -> \"this pattern matches when the list is empty\"\n"
|
|
36
|
+
" }\n"
|
|
37
|
+
" ```\n"
|
|
38
|
+
"\n"
|
|
39
|
+
).
|
|
40
|
+
|
|
41
|
+
-type continue_or_stop(AAE) :: {continue, AAE} | {stop, AAE}.
|
|
42
|
+
|
|
43
|
+
-type sorting() :: ascending | descending.
|
|
44
|
+
|
|
45
|
+
-file("src/gleam/list.gleam", 57).
|
|
46
|
+
-spec length_loop(list(any()), integer()) -> integer().
|
|
47
|
+
length_loop(List, Count) ->
|
|
48
|
+
case List of
|
|
49
|
+
[_ | List@1] ->
|
|
50
|
+
length_loop(List@1, Count + 1);
|
|
51
|
+
|
|
52
|
+
[] ->
|
|
53
|
+
Count
|
|
54
|
+
end.
|
|
55
|
+
|
|
56
|
+
-file("src/gleam/list.gleam", 53).
|
|
57
|
+
?DOC(
|
|
58
|
+
" Counts the number of elements in a given list.\n"
|
|
59
|
+
"\n"
|
|
60
|
+
" This function has to traverse the list to determine the number of elements,\n"
|
|
61
|
+
" so it runs in linear time.\n"
|
|
62
|
+
"\n"
|
|
63
|
+
" This function is natively implemented by the virtual machine and is highly\n"
|
|
64
|
+
" optimised.\n"
|
|
65
|
+
"\n"
|
|
66
|
+
" ## Examples\n"
|
|
67
|
+
"\n"
|
|
68
|
+
" ```gleam\n"
|
|
69
|
+
" assert length([]) == 0\n"
|
|
70
|
+
" ```\n"
|
|
71
|
+
"\n"
|
|
72
|
+
" ```gleam\n"
|
|
73
|
+
" assert length([1]) == 1\n"
|
|
74
|
+
" ```\n"
|
|
75
|
+
"\n"
|
|
76
|
+
" ```gleam\n"
|
|
77
|
+
" assert length([1, 2]) == 2\n"
|
|
78
|
+
" ```\n"
|
|
79
|
+
).
|
|
80
|
+
-spec length(list(any())) -> integer().
|
|
81
|
+
length(List) ->
|
|
82
|
+
erlang:length(List).
|
|
83
|
+
|
|
84
|
+
-file("src/gleam/list.gleam", 87).
|
|
85
|
+
-spec count_loop(list(AAL), fun((AAL) -> boolean()), integer()) -> integer().
|
|
86
|
+
count_loop(List, Predicate, Acc) ->
|
|
87
|
+
case List of
|
|
88
|
+
[] ->
|
|
89
|
+
Acc;
|
|
90
|
+
|
|
91
|
+
[First | Rest] ->
|
|
92
|
+
case Predicate(First) of
|
|
93
|
+
true ->
|
|
94
|
+
count_loop(Rest, Predicate, Acc + 1);
|
|
95
|
+
|
|
96
|
+
false ->
|
|
97
|
+
count_loop(Rest, Predicate, Acc)
|
|
98
|
+
end
|
|
99
|
+
end.
|
|
100
|
+
|
|
101
|
+
-file("src/gleam/list.gleam", 83).
|
|
102
|
+
?DOC(
|
|
103
|
+
" Counts the number of elements in a given list satisfying a given predicate.\n"
|
|
104
|
+
"\n"
|
|
105
|
+
" This function has to traverse the list to determine the number of elements,\n"
|
|
106
|
+
" so it runs in linear time.\n"
|
|
107
|
+
"\n"
|
|
108
|
+
" ## Examples\n"
|
|
109
|
+
"\n"
|
|
110
|
+
" ```gleam\n"
|
|
111
|
+
" assert count([], fn(a) { a > 0 }) == 0\n"
|
|
112
|
+
" ```\n"
|
|
113
|
+
"\n"
|
|
114
|
+
" ```gleam\n"
|
|
115
|
+
" assert count([1], fn(a) { a > 0 }) == 1\n"
|
|
116
|
+
" ```\n"
|
|
117
|
+
"\n"
|
|
118
|
+
" ```gleam\n"
|
|
119
|
+
" assert count([1, 2, 3], int.is_odd) == 2\n"
|
|
120
|
+
" ```\n"
|
|
121
|
+
).
|
|
122
|
+
-spec count(list(AAJ), fun((AAJ) -> boolean())) -> integer().
|
|
123
|
+
count(List, Predicate) ->
|
|
124
|
+
count_loop(List, Predicate, 0).
|
|
125
|
+
|
|
126
|
+
-file("src/gleam/list.gleam", 122).
|
|
127
|
+
?DOC(
|
|
128
|
+
" Creates a new list from a given list containing the same elements but in the\n"
|
|
129
|
+
" opposite order.\n"
|
|
130
|
+
"\n"
|
|
131
|
+
" This function has to traverse the list to create the new reversed list, so\n"
|
|
132
|
+
" it runs in linear time.\n"
|
|
133
|
+
"\n"
|
|
134
|
+
" This function is natively implemented by the virtual machine and is highly\n"
|
|
135
|
+
" optimised.\n"
|
|
136
|
+
"\n"
|
|
137
|
+
" ## Examples\n"
|
|
138
|
+
"\n"
|
|
139
|
+
" ```gleam\n"
|
|
140
|
+
" assert reverse([]) == []\n"
|
|
141
|
+
" ```\n"
|
|
142
|
+
"\n"
|
|
143
|
+
" ```gleam\n"
|
|
144
|
+
" assert reverse([1]) == [1]\n"
|
|
145
|
+
" ```\n"
|
|
146
|
+
"\n"
|
|
147
|
+
" ```gleam\n"
|
|
148
|
+
" assert reverse([1, 2]) == [2, 1]\n"
|
|
149
|
+
" ```\n"
|
|
150
|
+
).
|
|
151
|
+
-spec reverse(list(AAN)) -> list(AAN).
|
|
152
|
+
reverse(List) ->
|
|
153
|
+
lists:reverse(List).
|
|
154
|
+
|
|
155
|
+
-file("src/gleam/list.gleam", 156).
|
|
156
|
+
?DOC(
|
|
157
|
+
" Determines whether or not the list is empty.\n"
|
|
158
|
+
"\n"
|
|
159
|
+
" This function runs in constant time.\n"
|
|
160
|
+
"\n"
|
|
161
|
+
" ## Examples\n"
|
|
162
|
+
"\n"
|
|
163
|
+
" ```gleam\n"
|
|
164
|
+
" assert is_empty([])\n"
|
|
165
|
+
" ```\n"
|
|
166
|
+
"\n"
|
|
167
|
+
" ```gleam\n"
|
|
168
|
+
" assert !is_empty([1])\n"
|
|
169
|
+
" ```\n"
|
|
170
|
+
"\n"
|
|
171
|
+
" ```gleam\n"
|
|
172
|
+
" assert !is_empty([1, 1])\n"
|
|
173
|
+
" ```\n"
|
|
174
|
+
).
|
|
175
|
+
-spec is_empty(list(any())) -> boolean().
|
|
176
|
+
is_empty(List) ->
|
|
177
|
+
List =:= [].
|
|
178
|
+
|
|
179
|
+
-file("src/gleam/list.gleam", 187).
|
|
180
|
+
?DOC(
|
|
181
|
+
" Determines whether or not a given element exists within a given list.\n"
|
|
182
|
+
"\n"
|
|
183
|
+
" This function traverses the list to find the element, so it runs in linear\n"
|
|
184
|
+
" time.\n"
|
|
185
|
+
"\n"
|
|
186
|
+
" ## Examples\n"
|
|
187
|
+
"\n"
|
|
188
|
+
" ```gleam\n"
|
|
189
|
+
" assert !contains([], any: 0)\n"
|
|
190
|
+
" ```\n"
|
|
191
|
+
"\n"
|
|
192
|
+
" ```gleam\n"
|
|
193
|
+
" assert [0] |> contains(any: 0)\n"
|
|
194
|
+
" ```\n"
|
|
195
|
+
"\n"
|
|
196
|
+
" ```gleam\n"
|
|
197
|
+
" assert !contains([1], any: 0)\n"
|
|
198
|
+
" ```\n"
|
|
199
|
+
"\n"
|
|
200
|
+
" ```gleam\n"
|
|
201
|
+
" assert !contains([1, 1], any: 0)\n"
|
|
202
|
+
" ```\n"
|
|
203
|
+
"\n"
|
|
204
|
+
" ```gleam\n"
|
|
205
|
+
" assert [1, 0] |> contains(any: 0)\n"
|
|
206
|
+
" ```\n"
|
|
207
|
+
).
|
|
208
|
+
-spec contains(list(AAW), AAW) -> boolean().
|
|
209
|
+
contains(List, Elem) ->
|
|
210
|
+
case List of
|
|
211
|
+
[] ->
|
|
212
|
+
false;
|
|
213
|
+
|
|
214
|
+
[First | _] when First =:= Elem ->
|
|
215
|
+
true;
|
|
216
|
+
|
|
217
|
+
[_ | Rest] ->
|
|
218
|
+
contains(Rest, Elem)
|
|
219
|
+
end.
|
|
220
|
+
|
|
221
|
+
-file("src/gleam/list.gleam", 211).
|
|
222
|
+
?DOC(
|
|
223
|
+
" Gets the first element from the start of the list, if there is one.\n"
|
|
224
|
+
"\n"
|
|
225
|
+
" ## Examples\n"
|
|
226
|
+
"\n"
|
|
227
|
+
" ```gleam\n"
|
|
228
|
+
" assert first([]) == Error(Nil)\n"
|
|
229
|
+
" ```\n"
|
|
230
|
+
"\n"
|
|
231
|
+
" ```gleam\n"
|
|
232
|
+
" assert first([0]) == Ok(0)\n"
|
|
233
|
+
" ```\n"
|
|
234
|
+
"\n"
|
|
235
|
+
" ```gleam\n"
|
|
236
|
+
" assert first([1, 2]) == Ok(1)\n"
|
|
237
|
+
" ```\n"
|
|
238
|
+
).
|
|
239
|
+
-spec first(list(AAY)) -> {ok, AAY} | {error, nil}.
|
|
240
|
+
first(List) ->
|
|
241
|
+
case List of
|
|
242
|
+
[] ->
|
|
243
|
+
{error, nil};
|
|
244
|
+
|
|
245
|
+
[First | _] ->
|
|
246
|
+
{ok, First}
|
|
247
|
+
end.
|
|
248
|
+
|
|
249
|
+
-file("src/gleam/list.gleam", 237).
|
|
250
|
+
?DOC(
|
|
251
|
+
" Returns the list minus the first element. If the list is empty, `Error(Nil)` is\n"
|
|
252
|
+
" returned.\n"
|
|
253
|
+
"\n"
|
|
254
|
+
" This function runs in constant time and does not make a copy of the list.\n"
|
|
255
|
+
"\n"
|
|
256
|
+
" ## Examples\n"
|
|
257
|
+
"\n"
|
|
258
|
+
" ```gleam\n"
|
|
259
|
+
" assert rest([]) == Error(Nil)\n"
|
|
260
|
+
" ```\n"
|
|
261
|
+
"\n"
|
|
262
|
+
" ```gleam\n"
|
|
263
|
+
" assert rest([0]) == Ok([])\n"
|
|
264
|
+
" ```\n"
|
|
265
|
+
"\n"
|
|
266
|
+
" ```gleam\n"
|
|
267
|
+
" assert rest([1, 2]) == Ok([2])\n"
|
|
268
|
+
" ```\n"
|
|
269
|
+
).
|
|
270
|
+
-spec rest(list(ABC)) -> {ok, list(ABC)} | {error, nil}.
|
|
271
|
+
rest(List) ->
|
|
272
|
+
case List of
|
|
273
|
+
[] ->
|
|
274
|
+
{error, nil};
|
|
275
|
+
|
|
276
|
+
[_ | Rest] ->
|
|
277
|
+
{ok, Rest}
|
|
278
|
+
end.
|
|
279
|
+
|
|
280
|
+
-file("src/gleam/list.gleam", 276).
|
|
281
|
+
?DOC(
|
|
282
|
+
" Groups the elements from the given list by the given key function.\n"
|
|
283
|
+
"\n"
|
|
284
|
+
" Does not preserve the initial value order.\n"
|
|
285
|
+
"\n"
|
|
286
|
+
" ## Examples\n"
|
|
287
|
+
"\n"
|
|
288
|
+
" ```gleam\n"
|
|
289
|
+
" import gleam/dict\n"
|
|
290
|
+
"\n"
|
|
291
|
+
" assert\n"
|
|
292
|
+
" [Ok(3), Error(\"Wrong\"), Ok(200), Ok(73)]\n"
|
|
293
|
+
" |> group(by: fn(i) {\n"
|
|
294
|
+
" case i {\n"
|
|
295
|
+
" Ok(_) -> \"Successful\"\n"
|
|
296
|
+
" Error(_) -> \"Failed\"\n"
|
|
297
|
+
" }\n"
|
|
298
|
+
" })\n"
|
|
299
|
+
" |> dict.to_list\n"
|
|
300
|
+
" == [\n"
|
|
301
|
+
" #(\"Failed\", [Error(\"Wrong\")]),\n"
|
|
302
|
+
" #(\"Successful\", [Ok(73), Ok(200), Ok(3)])\n"
|
|
303
|
+
" ]\n"
|
|
304
|
+
" ```\n"
|
|
305
|
+
"\n"
|
|
306
|
+
" ```gleam\n"
|
|
307
|
+
" import gleam/dict\n"
|
|
308
|
+
"\n"
|
|
309
|
+
" assert group([1,2,3,4,5], by: fn(i) { i - i / 3 * 3 })\n"
|
|
310
|
+
" |> dict.to_list\n"
|
|
311
|
+
" == [#(0, [3]), #(1, [4, 1]), #(2, [5, 2])]\n"
|
|
312
|
+
" ```\n"
|
|
313
|
+
).
|
|
314
|
+
-spec group(list(ABH), fun((ABH) -> ABJ)) -> gleam@dict:dict(ABJ, list(ABH)).
|
|
315
|
+
group(List, Key) ->
|
|
316
|
+
gleam@dict:group(Key, List).
|
|
317
|
+
|
|
318
|
+
-file("src/gleam/list.gleam", 297).
|
|
319
|
+
-spec filter_loop(list(ABQ), fun((ABQ) -> boolean()), list(ABQ)) -> list(ABQ).
|
|
320
|
+
filter_loop(List, Fun, Acc) ->
|
|
321
|
+
case List of
|
|
322
|
+
[] ->
|
|
323
|
+
lists:reverse(Acc);
|
|
324
|
+
|
|
325
|
+
[First | Rest] ->
|
|
326
|
+
New_acc = case Fun(First) of
|
|
327
|
+
true ->
|
|
328
|
+
[First | Acc];
|
|
329
|
+
|
|
330
|
+
false ->
|
|
331
|
+
Acc
|
|
332
|
+
end,
|
|
333
|
+
filter_loop(Rest, Fun, New_acc)
|
|
334
|
+
end.
|
|
335
|
+
|
|
336
|
+
-file("src/gleam/list.gleam", 293).
|
|
337
|
+
?DOC(
|
|
338
|
+
" Returns a new list containing only the elements from the first list for\n"
|
|
339
|
+
" which the given functions returns `True`.\n"
|
|
340
|
+
"\n"
|
|
341
|
+
" ## Examples\n"
|
|
342
|
+
"\n"
|
|
343
|
+
" ```gleam\n"
|
|
344
|
+
" assert filter([2, 4, 6, 1], fn(x) { x > 2 }) == [4, 6]\n"
|
|
345
|
+
" ```\n"
|
|
346
|
+
"\n"
|
|
347
|
+
" ```gleam\n"
|
|
348
|
+
" assert filter([2, 4, 6, 1], fn(x) { x > 6 }) == []\n"
|
|
349
|
+
" ```\n"
|
|
350
|
+
).
|
|
351
|
+
-spec filter(list(ABN), fun((ABN) -> boolean())) -> list(ABN).
|
|
352
|
+
filter(List, Predicate) ->
|
|
353
|
+
filter_loop(List, Predicate, []).
|
|
354
|
+
|
|
355
|
+
-file("src/gleam/list.gleam", 327).
|
|
356
|
+
-spec filter_map_loop(
|
|
357
|
+
list(ACB),
|
|
358
|
+
fun((ACB) -> {ok, ACD} | {error, any()}),
|
|
359
|
+
list(ACD)
|
|
360
|
+
) -> list(ACD).
|
|
361
|
+
filter_map_loop(List, Fun, Acc) ->
|
|
362
|
+
case List of
|
|
363
|
+
[] ->
|
|
364
|
+
lists:reverse(Acc);
|
|
365
|
+
|
|
366
|
+
[First | Rest] ->
|
|
367
|
+
New_acc = case Fun(First) of
|
|
368
|
+
{ok, First@1} ->
|
|
369
|
+
[First@1 | Acc];
|
|
370
|
+
|
|
371
|
+
{error, _} ->
|
|
372
|
+
Acc
|
|
373
|
+
end,
|
|
374
|
+
filter_map_loop(Rest, Fun, New_acc)
|
|
375
|
+
end.
|
|
376
|
+
|
|
377
|
+
-file("src/gleam/list.gleam", 323).
|
|
378
|
+
?DOC(
|
|
379
|
+
" Returns a new list containing only the elements from the first list for\n"
|
|
380
|
+
" which the given functions returns `Ok(_)`.\n"
|
|
381
|
+
"\n"
|
|
382
|
+
" ## Examples\n"
|
|
383
|
+
"\n"
|
|
384
|
+
" ```gleam\n"
|
|
385
|
+
" assert filter_map([2, 4, 6, 1], Error) == []\n"
|
|
386
|
+
" ```\n"
|
|
387
|
+
"\n"
|
|
388
|
+
" ```gleam\n"
|
|
389
|
+
" assert filter_map([2, 4, 6, 1], fn(x) { Ok(x + 1) }) == [3, 5, 7, 2]\n"
|
|
390
|
+
" ```\n"
|
|
391
|
+
).
|
|
392
|
+
-spec filter_map(list(ABU), fun((ABU) -> {ok, ABW} | {error, any()})) -> list(ABW).
|
|
393
|
+
filter_map(List, Fun) ->
|
|
394
|
+
filter_map_loop(List, Fun, []).
|
|
395
|
+
|
|
396
|
+
-file("src/gleam/list.gleam", 356).
|
|
397
|
+
-spec map_loop(list(ACN), fun((ACN) -> ACP), list(ACP)) -> list(ACP).
|
|
398
|
+
map_loop(List, Fun, Acc) ->
|
|
399
|
+
case List of
|
|
400
|
+
[] ->
|
|
401
|
+
lists:reverse(Acc);
|
|
402
|
+
|
|
403
|
+
[First | Rest] ->
|
|
404
|
+
map_loop(Rest, Fun, [Fun(First) | Acc])
|
|
405
|
+
end.
|
|
406
|
+
|
|
407
|
+
-file("src/gleam/list.gleam", 352).
|
|
408
|
+
?DOC(
|
|
409
|
+
" Returns a new list containing the results of applying the supplied function to each element.\n"
|
|
410
|
+
"\n"
|
|
411
|
+
" ## Examples\n"
|
|
412
|
+
"\n"
|
|
413
|
+
" ```gleam\n"
|
|
414
|
+
" assert map([2, 4, 6], fn(x) { x * 2 }) == [4, 8, 12]\n"
|
|
415
|
+
" ```\n"
|
|
416
|
+
).
|
|
417
|
+
-spec map(list(ACJ), fun((ACJ) -> ACL)) -> list(ACL).
|
|
418
|
+
map(List, Fun) ->
|
|
419
|
+
map_loop(List, Fun, []).
|
|
420
|
+
|
|
421
|
+
-file("src/gleam/list.gleam", 382).
|
|
422
|
+
-spec map2_loop(list(ACY), list(ADA), fun((ACY, ADA) -> ADC), list(ADC)) -> list(ADC).
|
|
423
|
+
map2_loop(List1, List2, Fun, Acc) ->
|
|
424
|
+
case {List1, List2} of
|
|
425
|
+
{[], _} ->
|
|
426
|
+
lists:reverse(Acc);
|
|
427
|
+
|
|
428
|
+
{_, []} ->
|
|
429
|
+
lists:reverse(Acc);
|
|
430
|
+
|
|
431
|
+
{[A | As_], [B | Bs]} ->
|
|
432
|
+
map2_loop(As_, Bs, Fun, [Fun(A, B) | Acc])
|
|
433
|
+
end.
|
|
434
|
+
|
|
435
|
+
-file("src/gleam/list.gleam", 378).
|
|
436
|
+
?DOC(
|
|
437
|
+
" Combines two lists into a single list using the given function.\n"
|
|
438
|
+
"\n"
|
|
439
|
+
" If a list is longer than the other, the extra elements are dropped.\n"
|
|
440
|
+
"\n"
|
|
441
|
+
" ## Examples\n"
|
|
442
|
+
"\n"
|
|
443
|
+
" ```gleam\n"
|
|
444
|
+
" assert map2([1, 2, 3], [4, 5, 6], fn(x, y) { x + y }) == [5, 7, 9]\n"
|
|
445
|
+
" ```\n"
|
|
446
|
+
"\n"
|
|
447
|
+
" ```gleam\n"
|
|
448
|
+
" assert map2([1, 2], [\"a\", \"b\", \"c\"], fn(i, x) { #(i, x) })\n"
|
|
449
|
+
" == [#(1, \"a\"), #(2, \"b\")]\n"
|
|
450
|
+
" ```\n"
|
|
451
|
+
).
|
|
452
|
+
-spec map2(list(ACS), list(ACU), fun((ACS, ACU) -> ACW)) -> list(ACW).
|
|
453
|
+
map2(List1, List2, Fun) ->
|
|
454
|
+
map2_loop(List1, List2, Fun, []).
|
|
455
|
+
|
|
456
|
+
-file("src/gleam/list.gleam", 416).
|
|
457
|
+
-spec map_fold_loop(list(ADK), fun((ADM, ADK) -> {ADM, ADN}), ADM, list(ADN)) -> {ADM,
|
|
458
|
+
list(ADN)}.
|
|
459
|
+
map_fold_loop(List, Fun, Acc, List_acc) ->
|
|
460
|
+
case List of
|
|
461
|
+
[] ->
|
|
462
|
+
{Acc, lists:reverse(List_acc)};
|
|
463
|
+
|
|
464
|
+
[First | Rest] ->
|
|
465
|
+
{Acc@1, First@1} = Fun(Acc, First),
|
|
466
|
+
map_fold_loop(Rest, Fun, Acc@1, [First@1 | List_acc])
|
|
467
|
+
end.
|
|
468
|
+
|
|
469
|
+
-file("src/gleam/list.gleam", 408).
|
|
470
|
+
?DOC(
|
|
471
|
+
" Similar to `map` but also lets you pass around an accumulated value.\n"
|
|
472
|
+
"\n"
|
|
473
|
+
" ## Examples\n"
|
|
474
|
+
"\n"
|
|
475
|
+
" ```gleam\n"
|
|
476
|
+
" assert\n"
|
|
477
|
+
" map_fold(\n"
|
|
478
|
+
" over: [1, 2, 3],\n"
|
|
479
|
+
" from: 100,\n"
|
|
480
|
+
" with: fn(memo, i) { #(memo + i, i * 2) }\n"
|
|
481
|
+
" )\n"
|
|
482
|
+
" == #(106, [2, 4, 6])\n"
|
|
483
|
+
" ```\n"
|
|
484
|
+
).
|
|
485
|
+
-spec map_fold(list(ADF), ADH, fun((ADH, ADF) -> {ADH, ADI})) -> {ADH,
|
|
486
|
+
list(ADI)}.
|
|
487
|
+
map_fold(List, Initial, Fun) ->
|
|
488
|
+
map_fold_loop(List, Fun, Initial, []).
|
|
489
|
+
|
|
490
|
+
-file("src/gleam/list.gleam", 447).
|
|
491
|
+
-spec index_map_loop(
|
|
492
|
+
list(ADU),
|
|
493
|
+
fun((ADU, integer()) -> ADW),
|
|
494
|
+
integer(),
|
|
495
|
+
list(ADW)
|
|
496
|
+
) -> list(ADW).
|
|
497
|
+
index_map_loop(List, Fun, Index, Acc) ->
|
|
498
|
+
case List of
|
|
499
|
+
[] ->
|
|
500
|
+
lists:reverse(Acc);
|
|
501
|
+
|
|
502
|
+
[First | Rest] ->
|
|
503
|
+
Acc@1 = [Fun(First, Index) | Acc],
|
|
504
|
+
index_map_loop(Rest, Fun, Index + 1, Acc@1)
|
|
505
|
+
end.
|
|
506
|
+
|
|
507
|
+
-file("src/gleam/list.gleam", 443).
|
|
508
|
+
?DOC(
|
|
509
|
+
" Similar to `map`, but the supplied function will also be passed the index\n"
|
|
510
|
+
" of the element being mapped as an additional argument.\n"
|
|
511
|
+
"\n"
|
|
512
|
+
" The index starts at 0, so the first element is 0, the second is 1, and so\n"
|
|
513
|
+
" on.\n"
|
|
514
|
+
"\n"
|
|
515
|
+
" ## Examples\n"
|
|
516
|
+
"\n"
|
|
517
|
+
" ```gleam\n"
|
|
518
|
+
" assert index_map([\"a\", \"b\"], fn(x, i) { #(i, x) }) == [#(0, \"a\"), #(1, \"b\")]\n"
|
|
519
|
+
" ```\n"
|
|
520
|
+
).
|
|
521
|
+
-spec index_map(list(ADQ), fun((ADQ, integer()) -> ADS)) -> list(ADS).
|
|
522
|
+
index_map(List, Fun) ->
|
|
523
|
+
index_map_loop(List, Fun, 0, []).
|
|
524
|
+
|
|
525
|
+
-file("src/gleam/list.gleam", 497).
|
|
526
|
+
-spec try_map_loop(list(AEI), fun((AEI) -> {ok, AEK} | {error, AEL}), list(AEK)) -> {ok,
|
|
527
|
+
list(AEK)} |
|
|
528
|
+
{error, AEL}.
|
|
529
|
+
try_map_loop(List, Fun, Acc) ->
|
|
530
|
+
case List of
|
|
531
|
+
[] ->
|
|
532
|
+
{ok, lists:reverse(Acc)};
|
|
533
|
+
|
|
534
|
+
[First | Rest] ->
|
|
535
|
+
case Fun(First) of
|
|
536
|
+
{ok, First@1} ->
|
|
537
|
+
try_map_loop(Rest, Fun, [First@1 | Acc]);
|
|
538
|
+
|
|
539
|
+
{error, Error} ->
|
|
540
|
+
{error, Error}
|
|
541
|
+
end
|
|
542
|
+
end.
|
|
543
|
+
|
|
544
|
+
-file("src/gleam/list.gleam", 490).
|
|
545
|
+
?DOC(
|
|
546
|
+
" Takes a function that returns a `Result` and applies it to each element in a\n"
|
|
547
|
+
" given list in turn.\n"
|
|
548
|
+
"\n"
|
|
549
|
+
" If the function returns `Ok(new_value)` for all elements in the list then a\n"
|
|
550
|
+
" list of the new values is returned.\n"
|
|
551
|
+
"\n"
|
|
552
|
+
" If the function returns `Error(reason)` for any of the elements then it is\n"
|
|
553
|
+
" returned immediately. None of the elements in the list are processed after\n"
|
|
554
|
+
" one returns an `Error`.\n"
|
|
555
|
+
"\n"
|
|
556
|
+
" ## Examples\n"
|
|
557
|
+
"\n"
|
|
558
|
+
" ```gleam\n"
|
|
559
|
+
" assert try_map([1, 2, 3], fn(x) { Ok(x + 2) }) == Ok([3, 4, 5])\n"
|
|
560
|
+
" ```\n"
|
|
561
|
+
"\n"
|
|
562
|
+
" ```gleam\n"
|
|
563
|
+
" assert try_map([1, 2, 3], fn(_) { Error(0) }) == Error(0)\n"
|
|
564
|
+
" ```\n"
|
|
565
|
+
"\n"
|
|
566
|
+
" ```gleam\n"
|
|
567
|
+
" assert try_map([[1], [2, 3]], first) == Ok([1, 2])\n"
|
|
568
|
+
" ```\n"
|
|
569
|
+
"\n"
|
|
570
|
+
" ```gleam\n"
|
|
571
|
+
" assert try_map([[1], [], [2]], first) == Error(Nil)\n"
|
|
572
|
+
" ```\n"
|
|
573
|
+
).
|
|
574
|
+
-spec try_map(list(ADZ), fun((ADZ) -> {ok, AEB} | {error, AEC})) -> {ok,
|
|
575
|
+
list(AEB)} |
|
|
576
|
+
{error, AEC}.
|
|
577
|
+
try_map(List, Fun) ->
|
|
578
|
+
try_map_loop(List, Fun, []).
|
|
579
|
+
|
|
580
|
+
-file("src/gleam/list.gleam", 530).
|
|
581
|
+
?DOC(
|
|
582
|
+
" Returns a list that is the given list with up to the given number of\n"
|
|
583
|
+
" elements removed from the front of the list.\n"
|
|
584
|
+
"\n"
|
|
585
|
+
" If the list has less than the number of elements an empty list is\n"
|
|
586
|
+
" returned.\n"
|
|
587
|
+
"\n"
|
|
588
|
+
" This function runs in linear time but does not copy the list.\n"
|
|
589
|
+
"\n"
|
|
590
|
+
" ## Examples\n"
|
|
591
|
+
"\n"
|
|
592
|
+
" ```gleam\n"
|
|
593
|
+
" assert drop([1, 2, 3, 4], 2) == [3, 4]\n"
|
|
594
|
+
" ```\n"
|
|
595
|
+
"\n"
|
|
596
|
+
" ```gleam\n"
|
|
597
|
+
" assert drop([1, 2, 3, 4], 9) == []\n"
|
|
598
|
+
" ```\n"
|
|
599
|
+
).
|
|
600
|
+
-spec drop(list(AES), integer()) -> list(AES).
|
|
601
|
+
drop(List, N) ->
|
|
602
|
+
case N =< 0 of
|
|
603
|
+
true ->
|
|
604
|
+
List;
|
|
605
|
+
|
|
606
|
+
false ->
|
|
607
|
+
case List of
|
|
608
|
+
[] ->
|
|
609
|
+
[];
|
|
610
|
+
|
|
611
|
+
[_ | Rest] ->
|
|
612
|
+
drop(Rest, N - 1)
|
|
613
|
+
end
|
|
614
|
+
end.
|
|
615
|
+
|
|
616
|
+
-file("src/gleam/list.gleam", 563).
|
|
617
|
+
-spec take_loop(list(AEY), integer(), list(AEY)) -> list(AEY).
|
|
618
|
+
take_loop(List, N, Acc) ->
|
|
619
|
+
case N =< 0 of
|
|
620
|
+
true ->
|
|
621
|
+
lists:reverse(Acc);
|
|
622
|
+
|
|
623
|
+
false ->
|
|
624
|
+
case List of
|
|
625
|
+
[] ->
|
|
626
|
+
lists:reverse(Acc);
|
|
627
|
+
|
|
628
|
+
[First | Rest] ->
|
|
629
|
+
take_loop(Rest, N - 1, [First | Acc])
|
|
630
|
+
end
|
|
631
|
+
end.
|
|
632
|
+
|
|
633
|
+
-file("src/gleam/list.gleam", 559).
|
|
634
|
+
?DOC(
|
|
635
|
+
" Returns a list containing the first given number of elements from the given\n"
|
|
636
|
+
" list.\n"
|
|
637
|
+
"\n"
|
|
638
|
+
" If the list has less than the number of elements then the full list is\n"
|
|
639
|
+
" returned.\n"
|
|
640
|
+
"\n"
|
|
641
|
+
" This function runs in linear time.\n"
|
|
642
|
+
"\n"
|
|
643
|
+
" ## Examples\n"
|
|
644
|
+
"\n"
|
|
645
|
+
" ```gleam\n"
|
|
646
|
+
" assert take([1, 2, 3, 4], 2) == [1, 2]\n"
|
|
647
|
+
" ```\n"
|
|
648
|
+
"\n"
|
|
649
|
+
" ```gleam\n"
|
|
650
|
+
" assert take([1, 2, 3, 4], 9) == [1, 2, 3, 4]\n"
|
|
651
|
+
" ```\n"
|
|
652
|
+
).
|
|
653
|
+
-spec take(list(AEV), integer()) -> list(AEV).
|
|
654
|
+
take(List, N) ->
|
|
655
|
+
take_loop(List, N, []).
|
|
656
|
+
|
|
657
|
+
-file("src/gleam/list.gleam", 582).
|
|
658
|
+
?DOC(
|
|
659
|
+
" Returns a new empty list.\n"
|
|
660
|
+
"\n"
|
|
661
|
+
" ## Examples\n"
|
|
662
|
+
"\n"
|
|
663
|
+
" ```gleam\n"
|
|
664
|
+
" assert new() == []\n"
|
|
665
|
+
" ```\n"
|
|
666
|
+
).
|
|
667
|
+
-spec new() -> list(any()).
|
|
668
|
+
new() ->
|
|
669
|
+
[].
|
|
670
|
+
|
|
671
|
+
-file("src/gleam/list.gleam", 603).
|
|
672
|
+
?DOC(
|
|
673
|
+
" Returns the given item wrapped in a list.\n"
|
|
674
|
+
"\n"
|
|
675
|
+
" ## Examples\n"
|
|
676
|
+
"\n"
|
|
677
|
+
" ```gleam\n"
|
|
678
|
+
" assert wrap(1) == [1]\n"
|
|
679
|
+
" ```\n"
|
|
680
|
+
"\n"
|
|
681
|
+
" ```gleam\n"
|
|
682
|
+
" assert wrap([\"a\", \"b\", \"c\"]) == [[\"a\", \"b\", \"c\"]]\n"
|
|
683
|
+
" ```\n"
|
|
684
|
+
"\n"
|
|
685
|
+
" ```gleam\n"
|
|
686
|
+
" assert wrap([[]]) == [[[]]]\n"
|
|
687
|
+
" ```\n"
|
|
688
|
+
).
|
|
689
|
+
-spec wrap(AFE) -> list(AFE).
|
|
690
|
+
wrap(Item) ->
|
|
691
|
+
[Item].
|
|
692
|
+
|
|
693
|
+
-file("src/gleam/list.gleam", 623).
|
|
694
|
+
-spec append_loop(list(AFK), list(AFK)) -> list(AFK).
|
|
695
|
+
append_loop(First, Second) ->
|
|
696
|
+
case First of
|
|
697
|
+
[] ->
|
|
698
|
+
Second;
|
|
699
|
+
|
|
700
|
+
[First@1 | Rest] ->
|
|
701
|
+
append_loop(Rest, [First@1 | Second])
|
|
702
|
+
end.
|
|
703
|
+
|
|
704
|
+
-file("src/gleam/list.gleam", 619).
|
|
705
|
+
?DOC(
|
|
706
|
+
" Joins one list onto the end of another.\n"
|
|
707
|
+
"\n"
|
|
708
|
+
" This function runs in linear time, and it traverses and copies the first\n"
|
|
709
|
+
" list.\n"
|
|
710
|
+
"\n"
|
|
711
|
+
" ## Examples\n"
|
|
712
|
+
"\n"
|
|
713
|
+
" ```gleam\n"
|
|
714
|
+
" assert append([1, 2], [3]) == [1, 2, 3]\n"
|
|
715
|
+
" ```\n"
|
|
716
|
+
).
|
|
717
|
+
-spec append(list(AFG), list(AFG)) -> list(AFG).
|
|
718
|
+
append(First, Second) ->
|
|
719
|
+
lists:append(First, Second).
|
|
720
|
+
|
|
721
|
+
-file("src/gleam/list.gleam", 643).
|
|
722
|
+
?DOC(
|
|
723
|
+
" Prefixes an item to a list. This can also be done using the dedicated\n"
|
|
724
|
+
" syntax instead.\n"
|
|
725
|
+
"\n"
|
|
726
|
+
" ```gleam\n"
|
|
727
|
+
" let existing_list = [2, 3, 4]\n"
|
|
728
|
+
" assert [1, ..existing_list] == [1, 2, 3, 4]\n"
|
|
729
|
+
" ```\n"
|
|
730
|
+
"\n"
|
|
731
|
+
" ```gleam\n"
|
|
732
|
+
" let existing_list = [2, 3, 4]\n"
|
|
733
|
+
" assert prepend(to: existing_list, this: 1) == [1, 2, 3, 4]\n"
|
|
734
|
+
" ```\n"
|
|
735
|
+
).
|
|
736
|
+
-spec prepend(list(AFO), AFO) -> list(AFO).
|
|
737
|
+
prepend(List, Item) ->
|
|
738
|
+
[Item | List].
|
|
739
|
+
|
|
740
|
+
-file("src/gleam/list.gleam", 663).
|
|
741
|
+
-spec flatten_loop(list(list(AFV)), list(AFV)) -> list(AFV).
|
|
742
|
+
flatten_loop(Lists, Acc) ->
|
|
743
|
+
case Lists of
|
|
744
|
+
[] ->
|
|
745
|
+
lists:reverse(Acc);
|
|
746
|
+
|
|
747
|
+
[List | Further_lists] ->
|
|
748
|
+
flatten_loop(Further_lists, lists:reverse(List, Acc))
|
|
749
|
+
end.
|
|
750
|
+
|
|
751
|
+
-file("src/gleam/list.gleam", 659).
|
|
752
|
+
?DOC(
|
|
753
|
+
" Joins a list of lists into a single list.\n"
|
|
754
|
+
"\n"
|
|
755
|
+
" This function traverses all elements twice on the JavaScript target.\n"
|
|
756
|
+
" This function traverses all elements once on the Erlang target.\n"
|
|
757
|
+
"\n"
|
|
758
|
+
" ## Examples\n"
|
|
759
|
+
"\n"
|
|
760
|
+
" ```gleam\n"
|
|
761
|
+
" assert flatten([[1], [2, 3], []]) == [1, 2, 3]\n"
|
|
762
|
+
" ```\n"
|
|
763
|
+
).
|
|
764
|
+
-spec flatten(list(list(AFR))) -> list(AFR).
|
|
765
|
+
flatten(Lists) ->
|
|
766
|
+
lists:append(Lists).
|
|
767
|
+
|
|
768
|
+
-file("src/gleam/list.gleam", 679).
|
|
769
|
+
?DOC(
|
|
770
|
+
" Maps the list with the given function into a list of lists, and then flattens it.\n"
|
|
771
|
+
"\n"
|
|
772
|
+
" ## Examples\n"
|
|
773
|
+
"\n"
|
|
774
|
+
" ```gleam\n"
|
|
775
|
+
" assert flat_map([2, 4, 6], fn(x) { [x, x + 1] }) == [2, 3, 4, 5, 6, 7]\n"
|
|
776
|
+
" ```\n"
|
|
777
|
+
).
|
|
778
|
+
-spec flat_map(list(AGA), fun((AGA) -> list(AGC))) -> list(AGC).
|
|
779
|
+
flat_map(List, Fun) ->
|
|
780
|
+
lists:append(map(List, Fun)).
|
|
781
|
+
|
|
782
|
+
-file("src/gleam/list.gleam", 691).
|
|
783
|
+
?DOC(
|
|
784
|
+
" Reduces a list of elements into a single value by calling a given function\n"
|
|
785
|
+
" on each element, going from left to right.\n"
|
|
786
|
+
"\n"
|
|
787
|
+
" `fold([1, 2, 3], 0, add)` is the equivalent of\n"
|
|
788
|
+
" `add(add(add(0, 1), 2), 3)`.\n"
|
|
789
|
+
"\n"
|
|
790
|
+
" This function runs in linear time.\n"
|
|
791
|
+
).
|
|
792
|
+
-spec fold(list(AGF), AGH, fun((AGH, AGF) -> AGH)) -> AGH.
|
|
793
|
+
fold(List, Initial, Fun) ->
|
|
794
|
+
case List of
|
|
795
|
+
[] ->
|
|
796
|
+
Initial;
|
|
797
|
+
|
|
798
|
+
[First | Rest] ->
|
|
799
|
+
fold(Rest, Fun(Initial, First), Fun)
|
|
800
|
+
end.
|
|
801
|
+
|
|
802
|
+
-file("src/gleam/list.gleam", 713).
|
|
803
|
+
?DOC(
|
|
804
|
+
" Reduces a list of elements into a single value by calling a given function\n"
|
|
805
|
+
" on each element, going from right to left.\n"
|
|
806
|
+
"\n"
|
|
807
|
+
" `fold_right([1, 2, 3], 0, add)` is the equivalent of\n"
|
|
808
|
+
" `add(add(add(0, 3), 2), 1)`.\n"
|
|
809
|
+
"\n"
|
|
810
|
+
" This function runs in linear time.\n"
|
|
811
|
+
"\n"
|
|
812
|
+
" Unlike `fold` this function is not tail recursive. Where possible use\n"
|
|
813
|
+
" `fold` instead as it will use less memory.\n"
|
|
814
|
+
).
|
|
815
|
+
-spec fold_right(list(AGI), AGK, fun((AGK, AGI) -> AGK)) -> AGK.
|
|
816
|
+
fold_right(List, Initial, Fun) ->
|
|
817
|
+
case List of
|
|
818
|
+
[] ->
|
|
819
|
+
Initial;
|
|
820
|
+
|
|
821
|
+
[First | Rest] ->
|
|
822
|
+
Fun(fold_right(Rest, Initial, Fun), First)
|
|
823
|
+
end.
|
|
824
|
+
|
|
825
|
+
-file("src/gleam/list.gleam", 750).
|
|
826
|
+
-spec index_fold_loop(
|
|
827
|
+
list(AGO),
|
|
828
|
+
AGQ,
|
|
829
|
+
fun((AGQ, AGO, integer()) -> AGQ),
|
|
830
|
+
integer()
|
|
831
|
+
) -> AGQ.
|
|
832
|
+
index_fold_loop(Over, Acc, With, Index) ->
|
|
833
|
+
case Over of
|
|
834
|
+
[] ->
|
|
835
|
+
Acc;
|
|
836
|
+
|
|
837
|
+
[First | Rest] ->
|
|
838
|
+
index_fold_loop(Rest, With(Acc, First, Index), With, Index + 1)
|
|
839
|
+
end.
|
|
840
|
+
|
|
841
|
+
-file("src/gleam/list.gleam", 742).
|
|
842
|
+
?DOC(
|
|
843
|
+
" Like `fold` but the folding function also receives the index of the current element.\n"
|
|
844
|
+
"\n"
|
|
845
|
+
" ## Examples\n"
|
|
846
|
+
"\n"
|
|
847
|
+
" ```gleam\n"
|
|
848
|
+
" assert [\"a\", \"b\", \"c\"]\n"
|
|
849
|
+
" |> index_fold(\"\", fn(acc, item, index) {\n"
|
|
850
|
+
" acc <> int.to_string(index) <> \":\" <> item <> \" \"\n"
|
|
851
|
+
" })\n"
|
|
852
|
+
" == \"0:a 1:b 2:c\"\n"
|
|
853
|
+
" ```\n"
|
|
854
|
+
"\n"
|
|
855
|
+
" ```gleam\n"
|
|
856
|
+
" assert [10, 20, 30]\n"
|
|
857
|
+
" |> index_fold(0, fn(acc, item, index) { acc + item * index })\n"
|
|
858
|
+
" == 80\n"
|
|
859
|
+
" ```\n"
|
|
860
|
+
).
|
|
861
|
+
-spec index_fold(list(AGL), AGN, fun((AGN, AGL, integer()) -> AGN)) -> AGN.
|
|
862
|
+
index_fold(List, Initial, Fun) ->
|
|
863
|
+
index_fold_loop(List, Initial, Fun, 0).
|
|
864
|
+
|
|
865
|
+
-file("src/gleam/list.gleam", 782).
|
|
866
|
+
?DOC(
|
|
867
|
+
" A variant of fold that might fail.\n"
|
|
868
|
+
"\n"
|
|
869
|
+
" The folding function should return `Result(accumulator, error)`.\n"
|
|
870
|
+
" If the returned value is `Ok(accumulator)` try_fold will try the next value in the list.\n"
|
|
871
|
+
" If the returned value is `Error(error)` try_fold will stop and return that error.\n"
|
|
872
|
+
"\n"
|
|
873
|
+
" ## Examples\n"
|
|
874
|
+
"\n"
|
|
875
|
+
" ```gleam\n"
|
|
876
|
+
" assert [1, 2, 3, 4]\n"
|
|
877
|
+
" |> try_fold(0, fn(acc, i) {\n"
|
|
878
|
+
" case i < 3 {\n"
|
|
879
|
+
" True -> Ok(acc + i)\n"
|
|
880
|
+
" False -> Error(Nil)\n"
|
|
881
|
+
" }\n"
|
|
882
|
+
" })\n"
|
|
883
|
+
" == Error(Nil)\n"
|
|
884
|
+
" ```\n"
|
|
885
|
+
).
|
|
886
|
+
-spec try_fold(list(AGR), AGT, fun((AGT, AGR) -> {ok, AGT} | {error, AGU})) -> {ok,
|
|
887
|
+
AGT} |
|
|
888
|
+
{error, AGU}.
|
|
889
|
+
try_fold(List, Initial, Fun) ->
|
|
890
|
+
case List of
|
|
891
|
+
[] ->
|
|
892
|
+
{ok, Initial};
|
|
893
|
+
|
|
894
|
+
[First | Rest] ->
|
|
895
|
+
case Fun(Initial, First) of
|
|
896
|
+
{ok, Result} ->
|
|
897
|
+
try_fold(Rest, Result, Fun);
|
|
898
|
+
|
|
899
|
+
{error, _} = Error ->
|
|
900
|
+
Error
|
|
901
|
+
end
|
|
902
|
+
end.
|
|
903
|
+
|
|
904
|
+
-file("src/gleam/list.gleam", 821).
|
|
905
|
+
?DOC(
|
|
906
|
+
" A variant of fold that allows to stop folding earlier.\n"
|
|
907
|
+
"\n"
|
|
908
|
+
" The folding function should return `ContinueOrStop(accumulator)`.\n"
|
|
909
|
+
" If the returned value is `Continue(accumulator)` fold_until will try the next value in the list.\n"
|
|
910
|
+
" If the returned value is `Stop(accumulator)` fold_until will stop and return that accumulator.\n"
|
|
911
|
+
"\n"
|
|
912
|
+
" ## Examples\n"
|
|
913
|
+
"\n"
|
|
914
|
+
" ```gleam\n"
|
|
915
|
+
" assert [1, 2, 3, 4]\n"
|
|
916
|
+
" |> fold_until(0, fn(acc, i) {\n"
|
|
917
|
+
" case i < 3 {\n"
|
|
918
|
+
" True -> Continue(acc + i)\n"
|
|
919
|
+
" False -> Stop(acc)\n"
|
|
920
|
+
" }\n"
|
|
921
|
+
" })\n"
|
|
922
|
+
" == 3\n"
|
|
923
|
+
" ```\n"
|
|
924
|
+
).
|
|
925
|
+
-spec fold_until(list(AGZ), AHB, fun((AHB, AGZ) -> continue_or_stop(AHB))) -> AHB.
|
|
926
|
+
fold_until(List, Initial, Fun) ->
|
|
927
|
+
case List of
|
|
928
|
+
[] ->
|
|
929
|
+
Initial;
|
|
930
|
+
|
|
931
|
+
[First | Rest] ->
|
|
932
|
+
case Fun(Initial, First) of
|
|
933
|
+
{continue, Next_accumulator} ->
|
|
934
|
+
fold_until(Rest, Next_accumulator, Fun);
|
|
935
|
+
|
|
936
|
+
{stop, B} ->
|
|
937
|
+
B
|
|
938
|
+
end
|
|
939
|
+
end.
|
|
940
|
+
|
|
941
|
+
-file("src/gleam/list.gleam", 855).
|
|
942
|
+
?DOC(
|
|
943
|
+
" Finds the first element in a given list for which the given function returns\n"
|
|
944
|
+
" `True`.\n"
|
|
945
|
+
"\n"
|
|
946
|
+
" Returns `Error(Nil)` if no such element is found.\n"
|
|
947
|
+
"\n"
|
|
948
|
+
" ## Examples\n"
|
|
949
|
+
"\n"
|
|
950
|
+
" ```gleam\n"
|
|
951
|
+
" assert find([1, 2, 3], fn(x) { x > 2 }) == Ok(3)\n"
|
|
952
|
+
" ```\n"
|
|
953
|
+
"\n"
|
|
954
|
+
" ```gleam\n"
|
|
955
|
+
" assert find([1, 2, 3], fn(x) { x > 4 }) == Error(Nil)\n"
|
|
956
|
+
" ```\n"
|
|
957
|
+
"\n"
|
|
958
|
+
" ```gleam\n"
|
|
959
|
+
" assert find([], fn(_) { True }) == Error(Nil)\n"
|
|
960
|
+
" ```\n"
|
|
961
|
+
).
|
|
962
|
+
-spec find(list(AHD), fun((AHD) -> boolean())) -> {ok, AHD} | {error, nil}.
|
|
963
|
+
find(List, Is_desired) ->
|
|
964
|
+
case List of
|
|
965
|
+
[] ->
|
|
966
|
+
{error, nil};
|
|
967
|
+
|
|
968
|
+
[First | Rest] ->
|
|
969
|
+
case Is_desired(First) of
|
|
970
|
+
true ->
|
|
971
|
+
{ok, First};
|
|
972
|
+
|
|
973
|
+
false ->
|
|
974
|
+
find(Rest, Is_desired)
|
|
975
|
+
end
|
|
976
|
+
end.
|
|
977
|
+
|
|
978
|
+
-file("src/gleam/list.gleam", 888).
|
|
979
|
+
?DOC(
|
|
980
|
+
" Finds the first element in a given list for which the given function returns\n"
|
|
981
|
+
" `Ok(new_value)`, then returns the wrapped `new_value`.\n"
|
|
982
|
+
"\n"
|
|
983
|
+
" Returns `Error(Nil)` if no such element is found.\n"
|
|
984
|
+
"\n"
|
|
985
|
+
" ## Examples\n"
|
|
986
|
+
"\n"
|
|
987
|
+
" ```gleam\n"
|
|
988
|
+
" assert find_map([[], [2], [3]], first) == Ok(2)\n"
|
|
989
|
+
" ```\n"
|
|
990
|
+
"\n"
|
|
991
|
+
" ```gleam\n"
|
|
992
|
+
" assert find_map([[], []], first) == Error(Nil)\n"
|
|
993
|
+
" ```\n"
|
|
994
|
+
"\n"
|
|
995
|
+
" ```gleam\n"
|
|
996
|
+
" assert find_map([], first) == Error(Nil)\n"
|
|
997
|
+
" ```\n"
|
|
998
|
+
).
|
|
999
|
+
-spec find_map(list(AHH), fun((AHH) -> {ok, AHJ} | {error, any()})) -> {ok, AHJ} |
|
|
1000
|
+
{error, nil}.
|
|
1001
|
+
find_map(List, Fun) ->
|
|
1002
|
+
case List of
|
|
1003
|
+
[] ->
|
|
1004
|
+
{error, nil};
|
|
1005
|
+
|
|
1006
|
+
[First | Rest] ->
|
|
1007
|
+
case Fun(First) of
|
|
1008
|
+
{ok, First@1} ->
|
|
1009
|
+
{ok, First@1};
|
|
1010
|
+
|
|
1011
|
+
{error, _} ->
|
|
1012
|
+
find_map(Rest, Fun)
|
|
1013
|
+
end
|
|
1014
|
+
end.
|
|
1015
|
+
|
|
1016
|
+
-file("src/gleam/list.gleam", 920).
|
|
1017
|
+
?DOC(
|
|
1018
|
+
" Returns `True` if the given function returns `True` for all the elements in\n"
|
|
1019
|
+
" the given list. If the function returns `False` for any of the elements it\n"
|
|
1020
|
+
" immediately returns `False` without checking the rest of the list.\n"
|
|
1021
|
+
"\n"
|
|
1022
|
+
" ## Examples\n"
|
|
1023
|
+
"\n"
|
|
1024
|
+
" ```gleam\n"
|
|
1025
|
+
" assert all([], fn(x) { x > 3 })\n"
|
|
1026
|
+
" ```\n"
|
|
1027
|
+
"\n"
|
|
1028
|
+
" ```gleam\n"
|
|
1029
|
+
" assert all([4, 5], fn(x) { x > 3 })\n"
|
|
1030
|
+
" ```\n"
|
|
1031
|
+
"\n"
|
|
1032
|
+
" ```gleam\n"
|
|
1033
|
+
" assert !all([4, 3], fn(x) { x > 3 })\n"
|
|
1034
|
+
" ```\n"
|
|
1035
|
+
).
|
|
1036
|
+
-spec all(list(AHP), fun((AHP) -> boolean())) -> boolean().
|
|
1037
|
+
all(List, Predicate) ->
|
|
1038
|
+
case List of
|
|
1039
|
+
[] ->
|
|
1040
|
+
true;
|
|
1041
|
+
|
|
1042
|
+
[First | Rest] ->
|
|
1043
|
+
case Predicate(First) of
|
|
1044
|
+
true ->
|
|
1045
|
+
all(Rest, Predicate);
|
|
1046
|
+
|
|
1047
|
+
false ->
|
|
1048
|
+
false
|
|
1049
|
+
end
|
|
1050
|
+
end.
|
|
1051
|
+
|
|
1052
|
+
-file("src/gleam/list.gleam", 953).
|
|
1053
|
+
?DOC(
|
|
1054
|
+
" Returns `True` if the given function returns `True` for any the elements in\n"
|
|
1055
|
+
" the given list. If the function returns `True` for any of the elements it\n"
|
|
1056
|
+
" immediately returns `True` without checking the rest of the list.\n"
|
|
1057
|
+
"\n"
|
|
1058
|
+
" ## Examples\n"
|
|
1059
|
+
"\n"
|
|
1060
|
+
" ```gleam\n"
|
|
1061
|
+
" assert !any([], fn(x) { x > 3 })\n"
|
|
1062
|
+
" ```\n"
|
|
1063
|
+
"\n"
|
|
1064
|
+
" ```gleam\n"
|
|
1065
|
+
" assert any([4, 5], fn(x) { x > 3 })\n"
|
|
1066
|
+
" ```\n"
|
|
1067
|
+
"\n"
|
|
1068
|
+
" ```gleam\n"
|
|
1069
|
+
" assert any([4, 3], fn(x) { x > 4 })\n"
|
|
1070
|
+
" ```\n"
|
|
1071
|
+
"\n"
|
|
1072
|
+
" ```gleam\n"
|
|
1073
|
+
" assert any([3, 4], fn(x) { x > 3 })\n"
|
|
1074
|
+
" ```\n"
|
|
1075
|
+
).
|
|
1076
|
+
-spec any(list(AHR), fun((AHR) -> boolean())) -> boolean().
|
|
1077
|
+
any(List, Predicate) ->
|
|
1078
|
+
case List of
|
|
1079
|
+
[] ->
|
|
1080
|
+
false;
|
|
1081
|
+
|
|
1082
|
+
[First | Rest] ->
|
|
1083
|
+
case Predicate(First) of
|
|
1084
|
+
true ->
|
|
1085
|
+
true;
|
|
1086
|
+
|
|
1087
|
+
false ->
|
|
1088
|
+
any(Rest, Predicate)
|
|
1089
|
+
end
|
|
1090
|
+
end.
|
|
1091
|
+
|
|
1092
|
+
-file("src/gleam/list.gleam", 991).
|
|
1093
|
+
-spec zip_loop(list(AHY), list(AIA), list({AHY, AIA})) -> list({AHY, AIA}).
|
|
1094
|
+
zip_loop(One, Other, Acc) ->
|
|
1095
|
+
case {One, Other} of
|
|
1096
|
+
{[First_one | Rest_one], [First_other | Rest_other]} ->
|
|
1097
|
+
zip_loop(Rest_one, Rest_other, [{First_one, First_other} | Acc]);
|
|
1098
|
+
|
|
1099
|
+
{_, _} ->
|
|
1100
|
+
lists:reverse(Acc)
|
|
1101
|
+
end.
|
|
1102
|
+
|
|
1103
|
+
-file("src/gleam/list.gleam", 987).
|
|
1104
|
+
?DOC(
|
|
1105
|
+
" Takes two lists and returns a single list of 2-element tuples.\n"
|
|
1106
|
+
"\n"
|
|
1107
|
+
" If one of the lists is longer than the other, the remaining elements from\n"
|
|
1108
|
+
" the longer list are not used.\n"
|
|
1109
|
+
"\n"
|
|
1110
|
+
" ## Examples\n"
|
|
1111
|
+
"\n"
|
|
1112
|
+
" ```gleam\n"
|
|
1113
|
+
" assert zip([], []) == []\n"
|
|
1114
|
+
" ```\n"
|
|
1115
|
+
"\n"
|
|
1116
|
+
" ```gleam\n"
|
|
1117
|
+
" assert zip([1, 2], [3]) == [#(1, 3)]\n"
|
|
1118
|
+
" ```\n"
|
|
1119
|
+
"\n"
|
|
1120
|
+
" ```gleam\n"
|
|
1121
|
+
" assert zip([1], [3, 4]) == [#(1, 3)]\n"
|
|
1122
|
+
" ```\n"
|
|
1123
|
+
"\n"
|
|
1124
|
+
" ```gleam\n"
|
|
1125
|
+
" assert zip([1, 2], [3, 4]) == [#(1, 3), #(2, 4)]\n"
|
|
1126
|
+
" ```\n"
|
|
1127
|
+
).
|
|
1128
|
+
-spec zip(list(AHT), list(AHV)) -> list({AHT, AHV}).
|
|
1129
|
+
zip(List, Other) ->
|
|
1130
|
+
zip_loop(List, Other, []).
|
|
1131
|
+
|
|
1132
|
+
-file("src/gleam/list.gleam", 1028).
|
|
1133
|
+
-spec strict_zip_loop(list(AIL), list(AIN), list({AIL, AIN})) -> {ok,
|
|
1134
|
+
list({AIL, AIN})} |
|
|
1135
|
+
{error, nil}.
|
|
1136
|
+
strict_zip_loop(One, Other, Acc) ->
|
|
1137
|
+
case {One, Other} of
|
|
1138
|
+
{[], []} ->
|
|
1139
|
+
{ok, lists:reverse(Acc)};
|
|
1140
|
+
|
|
1141
|
+
{[], _} ->
|
|
1142
|
+
{error, nil};
|
|
1143
|
+
|
|
1144
|
+
{_, []} ->
|
|
1145
|
+
{error, nil};
|
|
1146
|
+
|
|
1147
|
+
{[First_one | Rest_one], [First_other | Rest_other]} ->
|
|
1148
|
+
strict_zip_loop(
|
|
1149
|
+
Rest_one,
|
|
1150
|
+
Rest_other,
|
|
1151
|
+
[{First_one, First_other} | Acc]
|
|
1152
|
+
)
|
|
1153
|
+
end.
|
|
1154
|
+
|
|
1155
|
+
-file("src/gleam/list.gleam", 1021).
|
|
1156
|
+
?DOC(
|
|
1157
|
+
" Takes two lists and returns a single list of 2-element tuples.\n"
|
|
1158
|
+
"\n"
|
|
1159
|
+
" If one of the lists is longer than the other, an `Error` is returned.\n"
|
|
1160
|
+
"\n"
|
|
1161
|
+
" ## Examples\n"
|
|
1162
|
+
"\n"
|
|
1163
|
+
" ```gleam\n"
|
|
1164
|
+
" assert strict_zip([], []) == Ok([])\n"
|
|
1165
|
+
" ```\n"
|
|
1166
|
+
"\n"
|
|
1167
|
+
" ```gleam\n"
|
|
1168
|
+
" assert strict_zip([1, 2], [3]) == Error(Nil)\n"
|
|
1169
|
+
" ```\n"
|
|
1170
|
+
"\n"
|
|
1171
|
+
" ```gleam\n"
|
|
1172
|
+
" assert strict_zip([1], [3, 4]) == Error(Nil)\n"
|
|
1173
|
+
" ```\n"
|
|
1174
|
+
"\n"
|
|
1175
|
+
" ```gleam\n"
|
|
1176
|
+
" assert strict_zip([1, 2], [3, 4]) == Ok([#(1, 3), #(2, 4)])\n"
|
|
1177
|
+
" ```\n"
|
|
1178
|
+
).
|
|
1179
|
+
-spec strict_zip(list(AIE), list(AIG)) -> {ok, list({AIE, AIG})} | {error, nil}.
|
|
1180
|
+
strict_zip(List, Other) ->
|
|
1181
|
+
strict_zip_loop(List, Other, []).
|
|
1182
|
+
|
|
1183
|
+
-file("src/gleam/list.gleam", 1057).
|
|
1184
|
+
-spec unzip_loop(list({AIY, AIZ}), list(AIY), list(AIZ)) -> {list(AIY),
|
|
1185
|
+
list(AIZ)}.
|
|
1186
|
+
unzip_loop(Input, One, Other) ->
|
|
1187
|
+
case Input of
|
|
1188
|
+
[] ->
|
|
1189
|
+
{lists:reverse(One), lists:reverse(Other)};
|
|
1190
|
+
|
|
1191
|
+
[{First_one, First_other} | Rest] ->
|
|
1192
|
+
unzip_loop(Rest, [First_one | One], [First_other | Other])
|
|
1193
|
+
end.
|
|
1194
|
+
|
|
1195
|
+
-file("src/gleam/list.gleam", 1053).
|
|
1196
|
+
?DOC(
|
|
1197
|
+
" Takes a single list of 2-element tuples and returns two lists.\n"
|
|
1198
|
+
"\n"
|
|
1199
|
+
" ## Examples\n"
|
|
1200
|
+
"\n"
|
|
1201
|
+
" ```gleam\n"
|
|
1202
|
+
" assert unzip([#(1, 2), #(3, 4)]) == #([1, 3], [2, 4])\n"
|
|
1203
|
+
" ```\n"
|
|
1204
|
+
"\n"
|
|
1205
|
+
" ```gleam\n"
|
|
1206
|
+
" assert unzip([]) == #([], [])\n"
|
|
1207
|
+
" ```\n"
|
|
1208
|
+
).
|
|
1209
|
+
-spec unzip(list({AIT, AIU})) -> {list(AIT), list(AIU)}.
|
|
1210
|
+
unzip(Input) ->
|
|
1211
|
+
unzip_loop(Input, [], []).
|
|
1212
|
+
|
|
1213
|
+
-file("src/gleam/list.gleam", 1090).
|
|
1214
|
+
-spec intersperse_loop(list(AJI), AJI, list(AJI)) -> list(AJI).
|
|
1215
|
+
intersperse_loop(List, Separator, Acc) ->
|
|
1216
|
+
case List of
|
|
1217
|
+
[] ->
|
|
1218
|
+
lists:reverse(Acc);
|
|
1219
|
+
|
|
1220
|
+
[First | Rest] ->
|
|
1221
|
+
intersperse_loop(Rest, Separator, [First, Separator | Acc])
|
|
1222
|
+
end.
|
|
1223
|
+
|
|
1224
|
+
-file("src/gleam/list.gleam", 1083).
|
|
1225
|
+
?DOC(
|
|
1226
|
+
" Inserts a given value between each existing element in a given list.\n"
|
|
1227
|
+
"\n"
|
|
1228
|
+
" This function runs in linear time and copies the list.\n"
|
|
1229
|
+
"\n"
|
|
1230
|
+
" ## Examples\n"
|
|
1231
|
+
"\n"
|
|
1232
|
+
" ```gleam\n"
|
|
1233
|
+
" assert intersperse([1, 1, 1], 2) == [1, 2, 1, 2, 1]\n"
|
|
1234
|
+
" ```\n"
|
|
1235
|
+
"\n"
|
|
1236
|
+
" ```gleam\n"
|
|
1237
|
+
" assert intersperse([], 2) == []\n"
|
|
1238
|
+
" ```\n"
|
|
1239
|
+
).
|
|
1240
|
+
-spec intersperse(list(AJF), AJF) -> list(AJF).
|
|
1241
|
+
intersperse(List, Elem) ->
|
|
1242
|
+
case List of
|
|
1243
|
+
[] ->
|
|
1244
|
+
List;
|
|
1245
|
+
|
|
1246
|
+
[_] ->
|
|
1247
|
+
List;
|
|
1248
|
+
|
|
1249
|
+
[First | Rest] ->
|
|
1250
|
+
intersperse_loop(Rest, Elem, [First])
|
|
1251
|
+
end.
|
|
1252
|
+
|
|
1253
|
+
-file("src/gleam/list.gleam", 1112).
|
|
1254
|
+
-spec unique_loop(list(AJP), gleam@dict:dict(AJP, nil), list(AJP)) -> list(AJP).
|
|
1255
|
+
unique_loop(List, Seen, Acc) ->
|
|
1256
|
+
case List of
|
|
1257
|
+
[] ->
|
|
1258
|
+
lists:reverse(Acc);
|
|
1259
|
+
|
|
1260
|
+
[First | Rest] ->
|
|
1261
|
+
case gleam@dict:has_key(Seen, First) of
|
|
1262
|
+
true ->
|
|
1263
|
+
unique_loop(Rest, Seen, Acc);
|
|
1264
|
+
|
|
1265
|
+
false ->
|
|
1266
|
+
unique_loop(
|
|
1267
|
+
Rest,
|
|
1268
|
+
gleam@dict:insert(Seen, First, nil),
|
|
1269
|
+
[First | Acc]
|
|
1270
|
+
)
|
|
1271
|
+
end
|
|
1272
|
+
end.
|
|
1273
|
+
|
|
1274
|
+
-file("src/gleam/list.gleam", 1108).
|
|
1275
|
+
?DOC(
|
|
1276
|
+
" Removes any duplicate elements from a given list.\n"
|
|
1277
|
+
"\n"
|
|
1278
|
+
" This function returns in loglinear time.\n"
|
|
1279
|
+
"\n"
|
|
1280
|
+
" ## Examples\n"
|
|
1281
|
+
"\n"
|
|
1282
|
+
" ```gleam\n"
|
|
1283
|
+
" assert unique([1, 1, 1, 4, 7, 3, 3, 4]) == [1, 4, 7, 3]\n"
|
|
1284
|
+
" ```\n"
|
|
1285
|
+
).
|
|
1286
|
+
-spec unique(list(AJM)) -> list(AJM).
|
|
1287
|
+
unique(List) ->
|
|
1288
|
+
unique_loop(List, maps:new(), []).
|
|
1289
|
+
|
|
1290
|
+
-file("src/gleam/list.gleam", 1197).
|
|
1291
|
+
?DOC(
|
|
1292
|
+
" Given a list it returns slices of it that are locally sorted in ascending\n"
|
|
1293
|
+
" order.\n"
|
|
1294
|
+
"\n"
|
|
1295
|
+
" Imagine you have this list:\n"
|
|
1296
|
+
"\n"
|
|
1297
|
+
" ```\n"
|
|
1298
|
+
" [1, 2, 3, 2, 1, 0]\n"
|
|
1299
|
+
" ^^^^^^^ ^^^^^^^ This is a slice in descending order\n"
|
|
1300
|
+
" |\n"
|
|
1301
|
+
" | This is a slice that is sorted in ascending order\n"
|
|
1302
|
+
" ```\n"
|
|
1303
|
+
"\n"
|
|
1304
|
+
" So the produced result will contain these two slices, each one sorted in\n"
|
|
1305
|
+
" ascending order: `[[1, 2, 3], [0, 1, 2]]`.\n"
|
|
1306
|
+
"\n"
|
|
1307
|
+
" - `growing` is an accumulator with the current slice being grown\n"
|
|
1308
|
+
" - `direction` is the growing direction of the slice being grown, it could\n"
|
|
1309
|
+
" either be ascending or strictly descending\n"
|
|
1310
|
+
" - `prev` is the previous element that needs to be added to the growing slice\n"
|
|
1311
|
+
" it is carried around to check whether we have to keep growing the current\n"
|
|
1312
|
+
" slice or not\n"
|
|
1313
|
+
" - `acc` is the accumulator containing the slices sorted in ascending order\n"
|
|
1314
|
+
).
|
|
1315
|
+
-spec sequences(
|
|
1316
|
+
list(AJY),
|
|
1317
|
+
fun((AJY, AJY) -> gleam@order:order()),
|
|
1318
|
+
list(AJY),
|
|
1319
|
+
sorting(),
|
|
1320
|
+
AJY,
|
|
1321
|
+
list(list(AJY))
|
|
1322
|
+
) -> list(list(AJY)).
|
|
1323
|
+
sequences(List, Compare, Growing, Direction, Prev, Acc) ->
|
|
1324
|
+
Growing@1 = [Prev | Growing],
|
|
1325
|
+
case List of
|
|
1326
|
+
[] ->
|
|
1327
|
+
case Direction of
|
|
1328
|
+
ascending ->
|
|
1329
|
+
[lists:reverse(Growing@1) | Acc];
|
|
1330
|
+
|
|
1331
|
+
descending ->
|
|
1332
|
+
[Growing@1 | Acc]
|
|
1333
|
+
end;
|
|
1334
|
+
|
|
1335
|
+
[New | Rest] ->
|
|
1336
|
+
case {Compare(Prev, New), Direction} of
|
|
1337
|
+
{gt, descending} ->
|
|
1338
|
+
sequences(Rest, Compare, Growing@1, Direction, New, Acc);
|
|
1339
|
+
|
|
1340
|
+
{lt, ascending} ->
|
|
1341
|
+
sequences(Rest, Compare, Growing@1, Direction, New, Acc);
|
|
1342
|
+
|
|
1343
|
+
{eq, ascending} ->
|
|
1344
|
+
sequences(Rest, Compare, Growing@1, Direction, New, Acc);
|
|
1345
|
+
|
|
1346
|
+
{gt, ascending} ->
|
|
1347
|
+
Acc@1 = case Direction of
|
|
1348
|
+
ascending ->
|
|
1349
|
+
[lists:reverse(Growing@1) | Acc];
|
|
1350
|
+
|
|
1351
|
+
descending ->
|
|
1352
|
+
[Growing@1 | Acc]
|
|
1353
|
+
end,
|
|
1354
|
+
case Rest of
|
|
1355
|
+
[] ->
|
|
1356
|
+
[[New] | Acc@1];
|
|
1357
|
+
|
|
1358
|
+
[Next | Rest@1] ->
|
|
1359
|
+
Direction@1 = case Compare(New, Next) of
|
|
1360
|
+
lt ->
|
|
1361
|
+
ascending;
|
|
1362
|
+
|
|
1363
|
+
eq ->
|
|
1364
|
+
ascending;
|
|
1365
|
+
|
|
1366
|
+
gt ->
|
|
1367
|
+
descending
|
|
1368
|
+
end,
|
|
1369
|
+
sequences(
|
|
1370
|
+
Rest@1,
|
|
1371
|
+
Compare,
|
|
1372
|
+
[New],
|
|
1373
|
+
Direction@1,
|
|
1374
|
+
Next,
|
|
1375
|
+
Acc@1
|
|
1376
|
+
)
|
|
1377
|
+
end;
|
|
1378
|
+
|
|
1379
|
+
{lt, descending} ->
|
|
1380
|
+
Acc@1 = case Direction of
|
|
1381
|
+
ascending ->
|
|
1382
|
+
[lists:reverse(Growing@1) | Acc];
|
|
1383
|
+
|
|
1384
|
+
descending ->
|
|
1385
|
+
[Growing@1 | Acc]
|
|
1386
|
+
end,
|
|
1387
|
+
case Rest of
|
|
1388
|
+
[] ->
|
|
1389
|
+
[[New] | Acc@1];
|
|
1390
|
+
|
|
1391
|
+
[Next | Rest@1] ->
|
|
1392
|
+
Direction@1 = case Compare(New, Next) of
|
|
1393
|
+
lt ->
|
|
1394
|
+
ascending;
|
|
1395
|
+
|
|
1396
|
+
eq ->
|
|
1397
|
+
ascending;
|
|
1398
|
+
|
|
1399
|
+
gt ->
|
|
1400
|
+
descending
|
|
1401
|
+
end,
|
|
1402
|
+
sequences(
|
|
1403
|
+
Rest@1,
|
|
1404
|
+
Compare,
|
|
1405
|
+
[New],
|
|
1406
|
+
Direction@1,
|
|
1407
|
+
Next,
|
|
1408
|
+
Acc@1
|
|
1409
|
+
)
|
|
1410
|
+
end;
|
|
1411
|
+
|
|
1412
|
+
{eq, descending} ->
|
|
1413
|
+
Acc@1 = case Direction of
|
|
1414
|
+
ascending ->
|
|
1415
|
+
[lists:reverse(Growing@1) | Acc];
|
|
1416
|
+
|
|
1417
|
+
descending ->
|
|
1418
|
+
[Growing@1 | Acc]
|
|
1419
|
+
end,
|
|
1420
|
+
case Rest of
|
|
1421
|
+
[] ->
|
|
1422
|
+
[[New] | Acc@1];
|
|
1423
|
+
|
|
1424
|
+
[Next | Rest@1] ->
|
|
1425
|
+
Direction@1 = case Compare(New, Next) of
|
|
1426
|
+
lt ->
|
|
1427
|
+
ascending;
|
|
1428
|
+
|
|
1429
|
+
eq ->
|
|
1430
|
+
ascending;
|
|
1431
|
+
|
|
1432
|
+
gt ->
|
|
1433
|
+
descending
|
|
1434
|
+
end,
|
|
1435
|
+
sequences(
|
|
1436
|
+
Rest@1,
|
|
1437
|
+
Compare,
|
|
1438
|
+
[New],
|
|
1439
|
+
Direction@1,
|
|
1440
|
+
Next,
|
|
1441
|
+
Acc@1
|
|
1442
|
+
)
|
|
1443
|
+
end
|
|
1444
|
+
end
|
|
1445
|
+
end.
|
|
1446
|
+
|
|
1447
|
+
-file("src/gleam/list.gleam", 1345).
|
|
1448
|
+
?DOC(
|
|
1449
|
+
" Merges two lists sorted in ascending order into a single list sorted in\n"
|
|
1450
|
+
" descending order according to the given comparator function.\n"
|
|
1451
|
+
"\n"
|
|
1452
|
+
" This reversing of the sort order is not avoidable if we want to implement\n"
|
|
1453
|
+
" merge as a tail recursive function. We could reverse the accumulator before\n"
|
|
1454
|
+
" returning it but that would end up being less efficient; so the merging\n"
|
|
1455
|
+
" algorithm has to play around this.\n"
|
|
1456
|
+
).
|
|
1457
|
+
-spec merge_ascendings(
|
|
1458
|
+
list(AKV),
|
|
1459
|
+
list(AKV),
|
|
1460
|
+
fun((AKV, AKV) -> gleam@order:order()),
|
|
1461
|
+
list(AKV)
|
|
1462
|
+
) -> list(AKV).
|
|
1463
|
+
merge_ascendings(List1, List2, Compare, Acc) ->
|
|
1464
|
+
case {List1, List2} of
|
|
1465
|
+
{[], List} ->
|
|
1466
|
+
lists:reverse(List, Acc);
|
|
1467
|
+
|
|
1468
|
+
{List, []} ->
|
|
1469
|
+
lists:reverse(List, Acc);
|
|
1470
|
+
|
|
1471
|
+
{[First1 | Rest1], [First2 | Rest2]} ->
|
|
1472
|
+
case Compare(First1, First2) of
|
|
1473
|
+
lt ->
|
|
1474
|
+
merge_ascendings(Rest1, List2, Compare, [First1 | Acc]);
|
|
1475
|
+
|
|
1476
|
+
gt ->
|
|
1477
|
+
merge_ascendings(List1, Rest2, Compare, [First2 | Acc]);
|
|
1478
|
+
|
|
1479
|
+
eq ->
|
|
1480
|
+
merge_ascendings(List1, Rest2, Compare, [First2 | Acc])
|
|
1481
|
+
end
|
|
1482
|
+
end.
|
|
1483
|
+
|
|
1484
|
+
-file("src/gleam/list.gleam", 1298).
|
|
1485
|
+
?DOC(
|
|
1486
|
+
" Given a list of ascending lists, it merges adjacent pairs into a single\n"
|
|
1487
|
+
" descending list, halving their number.\n"
|
|
1488
|
+
" It returns a list of the remaining descending lists.\n"
|
|
1489
|
+
).
|
|
1490
|
+
-spec merge_ascending_pairs(
|
|
1491
|
+
list(list(AKJ)),
|
|
1492
|
+
fun((AKJ, AKJ) -> gleam@order:order()),
|
|
1493
|
+
list(list(AKJ))
|
|
1494
|
+
) -> list(list(AKJ)).
|
|
1495
|
+
merge_ascending_pairs(Sequences, Compare, Acc) ->
|
|
1496
|
+
case Sequences of
|
|
1497
|
+
[] ->
|
|
1498
|
+
lists:reverse(Acc);
|
|
1499
|
+
|
|
1500
|
+
[Sequence] ->
|
|
1501
|
+
lists:reverse([lists:reverse(Sequence) | Acc]);
|
|
1502
|
+
|
|
1503
|
+
[Ascending1, Ascending2 | Rest] ->
|
|
1504
|
+
Descending = merge_ascendings(Ascending1, Ascending2, Compare, []),
|
|
1505
|
+
merge_ascending_pairs(Rest, Compare, [Descending | Acc])
|
|
1506
|
+
end.
|
|
1507
|
+
|
|
1508
|
+
-file("src/gleam/list.gleam", 1372).
|
|
1509
|
+
?DOC(
|
|
1510
|
+
" This is exactly the same as merge_ascendings but mirrored: it merges two\n"
|
|
1511
|
+
" lists sorted in descending order into a single list sorted in ascending\n"
|
|
1512
|
+
" order according to the given comparator function.\n"
|
|
1513
|
+
"\n"
|
|
1514
|
+
" This reversing of the sort order is not avoidable if we want to implement\n"
|
|
1515
|
+
" merge as a tail recursive function. We could reverse the accumulator before\n"
|
|
1516
|
+
" returning it but that would end up being less efficient; so the merging\n"
|
|
1517
|
+
" algorithm has to play around this.\n"
|
|
1518
|
+
).
|
|
1519
|
+
-spec merge_descendings(
|
|
1520
|
+
list(ALA),
|
|
1521
|
+
list(ALA),
|
|
1522
|
+
fun((ALA, ALA) -> gleam@order:order()),
|
|
1523
|
+
list(ALA)
|
|
1524
|
+
) -> list(ALA).
|
|
1525
|
+
merge_descendings(List1, List2, Compare, Acc) ->
|
|
1526
|
+
case {List1, List2} of
|
|
1527
|
+
{[], List} ->
|
|
1528
|
+
lists:reverse(List, Acc);
|
|
1529
|
+
|
|
1530
|
+
{List, []} ->
|
|
1531
|
+
lists:reverse(List, Acc);
|
|
1532
|
+
|
|
1533
|
+
{[First1 | Rest1], [First2 | Rest2]} ->
|
|
1534
|
+
case Compare(First1, First2) of
|
|
1535
|
+
lt ->
|
|
1536
|
+
merge_descendings(List1, Rest2, Compare, [First2 | Acc]);
|
|
1537
|
+
|
|
1538
|
+
gt ->
|
|
1539
|
+
merge_descendings(Rest1, List2, Compare, [First1 | Acc]);
|
|
1540
|
+
|
|
1541
|
+
eq ->
|
|
1542
|
+
merge_descendings(Rest1, List2, Compare, [First1 | Acc])
|
|
1543
|
+
end
|
|
1544
|
+
end.
|
|
1545
|
+
|
|
1546
|
+
-file("src/gleam/list.gleam", 1320).
|
|
1547
|
+
?DOC(" This is the same as merge_ascending_pairs but flipped for descending lists.\n").
|
|
1548
|
+
-spec merge_descending_pairs(
|
|
1549
|
+
list(list(AKP)),
|
|
1550
|
+
fun((AKP, AKP) -> gleam@order:order()),
|
|
1551
|
+
list(list(AKP))
|
|
1552
|
+
) -> list(list(AKP)).
|
|
1553
|
+
merge_descending_pairs(Sequences, Compare, Acc) ->
|
|
1554
|
+
case Sequences of
|
|
1555
|
+
[] ->
|
|
1556
|
+
lists:reverse(Acc);
|
|
1557
|
+
|
|
1558
|
+
[Sequence] ->
|
|
1559
|
+
lists:reverse([lists:reverse(Sequence) | Acc]);
|
|
1560
|
+
|
|
1561
|
+
[Descending1, Descending2 | Rest] ->
|
|
1562
|
+
Ascending = merge_descendings(Descending1, Descending2, Compare, []),
|
|
1563
|
+
merge_descending_pairs(Rest, Compare, [Ascending | Acc])
|
|
1564
|
+
end.
|
|
1565
|
+
|
|
1566
|
+
-file("src/gleam/list.gleam", 1264).
|
|
1567
|
+
?DOC(
|
|
1568
|
+
" Given some some sorted sequences (assumed to be sorted in `direction`) it\n"
|
|
1569
|
+
" merges them all together until we're left with just a list sorted in\n"
|
|
1570
|
+
" ascending order.\n"
|
|
1571
|
+
).
|
|
1572
|
+
-spec merge_all(
|
|
1573
|
+
list(list(AKF)),
|
|
1574
|
+
sorting(),
|
|
1575
|
+
fun((AKF, AKF) -> gleam@order:order())
|
|
1576
|
+
) -> list(AKF).
|
|
1577
|
+
merge_all(Sequences, Direction, Compare) ->
|
|
1578
|
+
case {Sequences, Direction} of
|
|
1579
|
+
{[], _} ->
|
|
1580
|
+
[];
|
|
1581
|
+
|
|
1582
|
+
{[Sequence], ascending} ->
|
|
1583
|
+
Sequence;
|
|
1584
|
+
|
|
1585
|
+
{[Sequence@1], descending} ->
|
|
1586
|
+
lists:reverse(Sequence@1);
|
|
1587
|
+
|
|
1588
|
+
{_, ascending} ->
|
|
1589
|
+
Sequences@1 = merge_ascending_pairs(Sequences, Compare, []),
|
|
1590
|
+
merge_all(Sequences@1, descending, Compare);
|
|
1591
|
+
|
|
1592
|
+
{_, descending} ->
|
|
1593
|
+
Sequences@2 = merge_descending_pairs(Sequences, Compare, []),
|
|
1594
|
+
merge_all(Sequences@2, ascending, Compare)
|
|
1595
|
+
end.
|
|
1596
|
+
|
|
1597
|
+
-file("src/gleam/list.gleam", 1135).
|
|
1598
|
+
?DOC(
|
|
1599
|
+
" Sorts from smallest to largest based upon the ordering specified by a given\n"
|
|
1600
|
+
" function.\n"
|
|
1601
|
+
"\n"
|
|
1602
|
+
" ## Examples\n"
|
|
1603
|
+
"\n"
|
|
1604
|
+
" ```gleam\n"
|
|
1605
|
+
" import gleam/int\n"
|
|
1606
|
+
"\n"
|
|
1607
|
+
" assert sort([4, 3, 6, 5, 4, 1, 2], by: int.compare) == [1, 2, 3, 4, 4, 5, 6]\n"
|
|
1608
|
+
" ```\n"
|
|
1609
|
+
).
|
|
1610
|
+
-spec sort(list(AJV), fun((AJV, AJV) -> gleam@order:order())) -> list(AJV).
|
|
1611
|
+
sort(List, Compare) ->
|
|
1612
|
+
case List of
|
|
1613
|
+
[] ->
|
|
1614
|
+
[];
|
|
1615
|
+
|
|
1616
|
+
[X] ->
|
|
1617
|
+
[X];
|
|
1618
|
+
|
|
1619
|
+
[X@1, Y | Rest] ->
|
|
1620
|
+
Direction = case Compare(X@1, Y) of
|
|
1621
|
+
lt ->
|
|
1622
|
+
ascending;
|
|
1623
|
+
|
|
1624
|
+
eq ->
|
|
1625
|
+
ascending;
|
|
1626
|
+
|
|
1627
|
+
gt ->
|
|
1628
|
+
descending
|
|
1629
|
+
end,
|
|
1630
|
+
Sequences = sequences(Rest, Compare, [X@1], Direction, Y, []),
|
|
1631
|
+
merge_all(Sequences, ascending, Compare)
|
|
1632
|
+
end.
|
|
1633
|
+
|
|
1634
|
+
-file("src/gleam/list.gleam", 1394).
|
|
1635
|
+
-spec range_loop(integer(), integer(), list(integer())) -> list(integer()).
|
|
1636
|
+
range_loop(Start, Stop, Acc) ->
|
|
1637
|
+
case gleam@int:compare(Start, Stop) of
|
|
1638
|
+
eq ->
|
|
1639
|
+
[Stop | Acc];
|
|
1640
|
+
|
|
1641
|
+
gt ->
|
|
1642
|
+
range_loop(Start, Stop + 1, [Stop | Acc]);
|
|
1643
|
+
|
|
1644
|
+
lt ->
|
|
1645
|
+
range_loop(Start, Stop - 1, [Stop | Acc])
|
|
1646
|
+
end.
|
|
1647
|
+
|
|
1648
|
+
-file("src/gleam/list.gleam", 1390).
|
|
1649
|
+
-spec range(integer(), integer()) -> list(integer()).
|
|
1650
|
+
range(Start, Stop) ->
|
|
1651
|
+
range_loop(Start, Stop, []).
|
|
1652
|
+
|
|
1653
|
+
-file("src/gleam/list.gleam", 1418).
|
|
1654
|
+
-spec repeat_loop(ALK, integer(), list(ALK)) -> list(ALK).
|
|
1655
|
+
repeat_loop(Item, Times, Acc) ->
|
|
1656
|
+
case Times =< 0 of
|
|
1657
|
+
true ->
|
|
1658
|
+
Acc;
|
|
1659
|
+
|
|
1660
|
+
false ->
|
|
1661
|
+
repeat_loop(Item, Times - 1, [Item | Acc])
|
|
1662
|
+
end.
|
|
1663
|
+
|
|
1664
|
+
-file("src/gleam/list.gleam", 1414).
|
|
1665
|
+
?DOC(
|
|
1666
|
+
" Builds a list of a given value a given number of times.\n"
|
|
1667
|
+
"\n"
|
|
1668
|
+
" ## Examples\n"
|
|
1669
|
+
"\n"
|
|
1670
|
+
" ```gleam\n"
|
|
1671
|
+
" assert repeat(\"a\", times: 0) == []\n"
|
|
1672
|
+
" ```\n"
|
|
1673
|
+
"\n"
|
|
1674
|
+
" ```gleam\n"
|
|
1675
|
+
" assert repeat(\"a\", times: 5) == [\"a\", \"a\", \"a\", \"a\", \"a\"]\n"
|
|
1676
|
+
" ```\n"
|
|
1677
|
+
).
|
|
1678
|
+
-spec repeat(ALI, integer()) -> list(ALI).
|
|
1679
|
+
repeat(A, Times) ->
|
|
1680
|
+
repeat_loop(A, Times, []).
|
|
1681
|
+
|
|
1682
|
+
-file("src/gleam/list.gleam", 1448).
|
|
1683
|
+
-spec split_loop(list(ALR), integer(), list(ALR)) -> {list(ALR), list(ALR)}.
|
|
1684
|
+
split_loop(List, N, Taken) ->
|
|
1685
|
+
case N =< 0 of
|
|
1686
|
+
true ->
|
|
1687
|
+
{lists:reverse(Taken), List};
|
|
1688
|
+
|
|
1689
|
+
false ->
|
|
1690
|
+
case List of
|
|
1691
|
+
[] ->
|
|
1692
|
+
{lists:reverse(Taken), []};
|
|
1693
|
+
|
|
1694
|
+
[First | Rest] ->
|
|
1695
|
+
split_loop(Rest, N - 1, [First | Taken])
|
|
1696
|
+
end
|
|
1697
|
+
end.
|
|
1698
|
+
|
|
1699
|
+
-file("src/gleam/list.gleam", 1444).
|
|
1700
|
+
?DOC(
|
|
1701
|
+
" Splits a list in two before the given index.\n"
|
|
1702
|
+
"\n"
|
|
1703
|
+
" If the list is not long enough to have the given index the before list will\n"
|
|
1704
|
+
" be the input list, and the after list will be empty.\n"
|
|
1705
|
+
"\n"
|
|
1706
|
+
" ## Examples\n"
|
|
1707
|
+
"\n"
|
|
1708
|
+
" ```gleam\n"
|
|
1709
|
+
" assert split([6, 7, 8, 9], 0) == #([], [6, 7, 8, 9])\n"
|
|
1710
|
+
" ```\n"
|
|
1711
|
+
"\n"
|
|
1712
|
+
" ```gleam\n"
|
|
1713
|
+
" assert split([6, 7, 8, 9], 2) == #([6, 7], [8, 9])\n"
|
|
1714
|
+
" ```\n"
|
|
1715
|
+
"\n"
|
|
1716
|
+
" ```gleam\n"
|
|
1717
|
+
" assert split([6, 7, 8, 9], 4) == #([6, 7, 8, 9], [])\n"
|
|
1718
|
+
" ```\n"
|
|
1719
|
+
).
|
|
1720
|
+
-spec split(list(ALN), integer()) -> {list(ALN), list(ALN)}.
|
|
1721
|
+
split(List, Index) ->
|
|
1722
|
+
split_loop(List, Index, []).
|
|
1723
|
+
|
|
1724
|
+
-file("src/gleam/list.gleam", 1484).
|
|
1725
|
+
-spec split_while_loop(list(AMA), fun((AMA) -> boolean()), list(AMA)) -> {list(AMA),
|
|
1726
|
+
list(AMA)}.
|
|
1727
|
+
split_while_loop(List, F, Acc) ->
|
|
1728
|
+
case List of
|
|
1729
|
+
[] ->
|
|
1730
|
+
{lists:reverse(Acc), []};
|
|
1731
|
+
|
|
1732
|
+
[First | Rest] ->
|
|
1733
|
+
case F(First) of
|
|
1734
|
+
true ->
|
|
1735
|
+
split_while_loop(Rest, F, [First | Acc]);
|
|
1736
|
+
|
|
1737
|
+
false ->
|
|
1738
|
+
{lists:reverse(Acc), List}
|
|
1739
|
+
end
|
|
1740
|
+
end.
|
|
1741
|
+
|
|
1742
|
+
-file("src/gleam/list.gleam", 1477).
|
|
1743
|
+
?DOC(
|
|
1744
|
+
" Splits a list in two before the first element that a given function returns\n"
|
|
1745
|
+
" `False` for.\n"
|
|
1746
|
+
"\n"
|
|
1747
|
+
" If the function returns `True` for all elements the first list will be the\n"
|
|
1748
|
+
" input list, and the second list will be empty.\n"
|
|
1749
|
+
"\n"
|
|
1750
|
+
" ## Examples\n"
|
|
1751
|
+
"\n"
|
|
1752
|
+
" ```gleam\n"
|
|
1753
|
+
" assert split_while([1, 2, 3, 4, 5], fn(x) { x <= 3 })\n"
|
|
1754
|
+
" == #([1, 2, 3], [4, 5])\n"
|
|
1755
|
+
" ```\n"
|
|
1756
|
+
"\n"
|
|
1757
|
+
" ```gleam\n"
|
|
1758
|
+
" assert split_while([1, 2, 3, 4, 5], fn(x) { x <= 5 })\n"
|
|
1759
|
+
" == #([1, 2, 3, 4, 5], [])\n"
|
|
1760
|
+
" ```\n"
|
|
1761
|
+
).
|
|
1762
|
+
-spec split_while(list(ALW), fun((ALW) -> boolean())) -> {list(ALW), list(ALW)}.
|
|
1763
|
+
split_while(List, Predicate) ->
|
|
1764
|
+
split_while_loop(List, Predicate, []).
|
|
1765
|
+
|
|
1766
|
+
-file("src/gleam/list.gleam", 1521).
|
|
1767
|
+
?DOC(
|
|
1768
|
+
" Given a list of 2-element tuples, finds the first tuple that has a given\n"
|
|
1769
|
+
" key as the first element and returns the second element.\n"
|
|
1770
|
+
"\n"
|
|
1771
|
+
" If no tuple is found with the given key then `Error(Nil)` is returned.\n"
|
|
1772
|
+
"\n"
|
|
1773
|
+
" This function may be useful for interacting with Erlang code where lists of\n"
|
|
1774
|
+
" tuples are common.\n"
|
|
1775
|
+
"\n"
|
|
1776
|
+
" ## Examples\n"
|
|
1777
|
+
"\n"
|
|
1778
|
+
" ```gleam\n"
|
|
1779
|
+
" assert key_find([#(\"a\", 0), #(\"b\", 1)], \"a\") == Ok(0)\n"
|
|
1780
|
+
" ```\n"
|
|
1781
|
+
"\n"
|
|
1782
|
+
" ```gleam\n"
|
|
1783
|
+
" assert key_find([#(\"a\", 0), #(\"b\", 1)], \"b\") == Ok(1)\n"
|
|
1784
|
+
" ```\n"
|
|
1785
|
+
"\n"
|
|
1786
|
+
" ```gleam\n"
|
|
1787
|
+
" assert key_find([#(\"a\", 0), #(\"b\", 1)], \"c\") == Error(Nil)\n"
|
|
1788
|
+
" ```\n"
|
|
1789
|
+
).
|
|
1790
|
+
-spec key_find(list({AMF, AMG}), AMF) -> {ok, AMG} | {error, nil}.
|
|
1791
|
+
key_find(Keyword_list, Desired_key) ->
|
|
1792
|
+
find_map(
|
|
1793
|
+
Keyword_list,
|
|
1794
|
+
fun(Keyword) ->
|
|
1795
|
+
{Key, Value} = Keyword,
|
|
1796
|
+
case Key =:= Desired_key of
|
|
1797
|
+
true ->
|
|
1798
|
+
{ok, Value};
|
|
1799
|
+
|
|
1800
|
+
false ->
|
|
1801
|
+
{error, nil}
|
|
1802
|
+
end
|
|
1803
|
+
end
|
|
1804
|
+
).
|
|
1805
|
+
|
|
1806
|
+
-file("src/gleam/list.gleam", 1550).
|
|
1807
|
+
?DOC(
|
|
1808
|
+
" Given a list of 2-element tuples, finds all tuples that have a given\n"
|
|
1809
|
+
" key as the first element and returns the second element.\n"
|
|
1810
|
+
"\n"
|
|
1811
|
+
" This function may be useful for interacting with Erlang code where lists of\n"
|
|
1812
|
+
" tuples are common.\n"
|
|
1813
|
+
"\n"
|
|
1814
|
+
" ## Examples\n"
|
|
1815
|
+
"\n"
|
|
1816
|
+
" ```gleam\n"
|
|
1817
|
+
" assert key_filter([#(\"a\", 0), #(\"b\", 1), #(\"a\", 2)], \"a\") == [0, 2]\n"
|
|
1818
|
+
" ```\n"
|
|
1819
|
+
"\n"
|
|
1820
|
+
" ```gleam\n"
|
|
1821
|
+
" assert key_filter([#(\"a\", 0), #(\"b\", 1)], \"c\") == []\n"
|
|
1822
|
+
" ```\n"
|
|
1823
|
+
).
|
|
1824
|
+
-spec key_filter(list({AMK, AML}), AMK) -> list(AML).
|
|
1825
|
+
key_filter(Keyword_list, Desired_key) ->
|
|
1826
|
+
filter_map(
|
|
1827
|
+
Keyword_list,
|
|
1828
|
+
fun(Keyword) ->
|
|
1829
|
+
{Key, Value} = Keyword,
|
|
1830
|
+
case Key =:= Desired_key of
|
|
1831
|
+
true ->
|
|
1832
|
+
{ok, Value};
|
|
1833
|
+
|
|
1834
|
+
false ->
|
|
1835
|
+
{error, nil}
|
|
1836
|
+
end
|
|
1837
|
+
end
|
|
1838
|
+
).
|
|
1839
|
+
|
|
1840
|
+
-file("src/gleam/list.gleam", 1587).
|
|
1841
|
+
-spec key_pop_loop(list({AMU, AMV}), AMU, list({AMU, AMV})) -> {ok,
|
|
1842
|
+
{AMV, list({AMU, AMV})}} |
|
|
1843
|
+
{error, nil}.
|
|
1844
|
+
key_pop_loop(List, Key, Checked) ->
|
|
1845
|
+
case List of
|
|
1846
|
+
[] ->
|
|
1847
|
+
{error, nil};
|
|
1848
|
+
|
|
1849
|
+
[{K, V} | Rest] when K =:= Key ->
|
|
1850
|
+
{ok, {V, lists:reverse(Checked, Rest)}};
|
|
1851
|
+
|
|
1852
|
+
[First | Rest@1] ->
|
|
1853
|
+
key_pop_loop(Rest@1, Key, [First | Checked])
|
|
1854
|
+
end.
|
|
1855
|
+
|
|
1856
|
+
-file("src/gleam/list.gleam", 1583).
|
|
1857
|
+
?DOC(
|
|
1858
|
+
" Given a list of 2-element tuples, finds the first tuple that has a given\n"
|
|
1859
|
+
" key as the first element. This function will return the second element\n"
|
|
1860
|
+
" of the found tuple and list with tuple removed.\n"
|
|
1861
|
+
"\n"
|
|
1862
|
+
" If no tuple is found with the given key then `Error(Nil)` is returned.\n"
|
|
1863
|
+
"\n"
|
|
1864
|
+
" ## Examples\n"
|
|
1865
|
+
"\n"
|
|
1866
|
+
" ```gleam\n"
|
|
1867
|
+
" assert key_pop([#(\"a\", 0), #(\"b\", 1)], \"a\") == Ok(#(0, [#(\"b\", 1)]))\n"
|
|
1868
|
+
" ```\n"
|
|
1869
|
+
"\n"
|
|
1870
|
+
" ```gleam\n"
|
|
1871
|
+
" assert key_pop([#(\"a\", 0), #(\"b\", 1)], \"b\") == Ok(#(1, [#(\"a\", 0)]))\n"
|
|
1872
|
+
" ```\n"
|
|
1873
|
+
"\n"
|
|
1874
|
+
" ```gleam\n"
|
|
1875
|
+
" assert key_pop([#(\"a\", 0), #(\"b\", 1)], \"c\") == Error(Nil)\n"
|
|
1876
|
+
" ```\n"
|
|
1877
|
+
).
|
|
1878
|
+
-spec key_pop(list({AMO, AMP}), AMO) -> {ok, {AMP, list({AMO, AMP})}} |
|
|
1879
|
+
{error, nil}.
|
|
1880
|
+
key_pop(List, Key) ->
|
|
1881
|
+
key_pop_loop(List, Key, []).
|
|
1882
|
+
|
|
1883
|
+
-file("src/gleam/list.gleam", 1619).
|
|
1884
|
+
-spec key_set_loop(list({ANF, ANG}), ANF, ANG, list({ANF, ANG})) -> list({ANF,
|
|
1885
|
+
ANG}).
|
|
1886
|
+
key_set_loop(List, Key, Value, Inspected) ->
|
|
1887
|
+
case List of
|
|
1888
|
+
[{K, _} | Rest] when K =:= Key ->
|
|
1889
|
+
lists:reverse(Inspected, [{K, Value} | Rest]);
|
|
1890
|
+
|
|
1891
|
+
[First | Rest@1] ->
|
|
1892
|
+
key_set_loop(Rest@1, Key, Value, [First | Inspected]);
|
|
1893
|
+
|
|
1894
|
+
[] ->
|
|
1895
|
+
lists:reverse([{Key, Value} | Inspected])
|
|
1896
|
+
end.
|
|
1897
|
+
|
|
1898
|
+
-file("src/gleam/list.gleam", 1615).
|
|
1899
|
+
?DOC(
|
|
1900
|
+
" Given a list of 2-element tuples, inserts a key and value into the list.\n"
|
|
1901
|
+
"\n"
|
|
1902
|
+
" If there was already a tuple with the key then it is replaced, otherwise it\n"
|
|
1903
|
+
" is added to the end of the list.\n"
|
|
1904
|
+
"\n"
|
|
1905
|
+
" ## Examples\n"
|
|
1906
|
+
"\n"
|
|
1907
|
+
" ```gleam\n"
|
|
1908
|
+
" assert key_set([#(5, 0), #(4, 1)], 4, 100) == [#(5, 0), #(4, 100)]\n"
|
|
1909
|
+
" ```\n"
|
|
1910
|
+
"\n"
|
|
1911
|
+
" ```gleam\n"
|
|
1912
|
+
" assert key_set([#(5, 0), #(4, 1)], 1, 100) == [#(5, 0), #(4, 1), #(1, 100)]\n"
|
|
1913
|
+
" ```\n"
|
|
1914
|
+
).
|
|
1915
|
+
-spec key_set(list({ANB, ANC}), ANB, ANC) -> list({ANB, ANC}).
|
|
1916
|
+
key_set(List, Key, Value) ->
|
|
1917
|
+
key_set_loop(List, Key, Value, []).
|
|
1918
|
+
|
|
1919
|
+
-file("src/gleam/list.gleam", 1646).
|
|
1920
|
+
?DOC(
|
|
1921
|
+
" Calls a function for each element in a list, discarding the return value.\n"
|
|
1922
|
+
"\n"
|
|
1923
|
+
" Useful for calling a side effect for every item of a list.\n"
|
|
1924
|
+
"\n"
|
|
1925
|
+
" ```gleam\n"
|
|
1926
|
+
" import gleam/io\n"
|
|
1927
|
+
"\n"
|
|
1928
|
+
" assert each([\"1\", \"2\", \"3\"], io.println) == Nil\n"
|
|
1929
|
+
" // 1\n"
|
|
1930
|
+
" // 2\n"
|
|
1931
|
+
" // 3\n"
|
|
1932
|
+
" ```\n"
|
|
1933
|
+
).
|
|
1934
|
+
-spec each(list(ANK), fun((ANK) -> any())) -> nil.
|
|
1935
|
+
each(List, F) ->
|
|
1936
|
+
case List of
|
|
1937
|
+
[] ->
|
|
1938
|
+
nil;
|
|
1939
|
+
|
|
1940
|
+
[First | Rest] ->
|
|
1941
|
+
F(First),
|
|
1942
|
+
each(Rest, F)
|
|
1943
|
+
end.
|
|
1944
|
+
|
|
1945
|
+
-file("src/gleam/list.gleam", 1673).
|
|
1946
|
+
?DOC(
|
|
1947
|
+
" Calls a `Result` returning function for each element in a list, discarding\n"
|
|
1948
|
+
" the return value. If the function returns `Error` then the iteration is\n"
|
|
1949
|
+
" stopped and the error is returned.\n"
|
|
1950
|
+
"\n"
|
|
1951
|
+
" Useful for calling a side effect for every item of a list.\n"
|
|
1952
|
+
"\n"
|
|
1953
|
+
" ## Examples\n"
|
|
1954
|
+
"\n"
|
|
1955
|
+
" ```gleam\n"
|
|
1956
|
+
" assert\n"
|
|
1957
|
+
" try_each(\n"
|
|
1958
|
+
" over: [1, 2, 3],\n"
|
|
1959
|
+
" with: function_that_might_fail,\n"
|
|
1960
|
+
" )\n"
|
|
1961
|
+
" == Ok(Nil)\n"
|
|
1962
|
+
" ```\n"
|
|
1963
|
+
).
|
|
1964
|
+
-spec try_each(list(ANN), fun((ANN) -> {ok, any()} | {error, ANQ})) -> {ok, nil} |
|
|
1965
|
+
{error, ANQ}.
|
|
1966
|
+
try_each(List, Fun) ->
|
|
1967
|
+
case List of
|
|
1968
|
+
[] ->
|
|
1969
|
+
{ok, nil};
|
|
1970
|
+
|
|
1971
|
+
[First | Rest] ->
|
|
1972
|
+
case Fun(First) of
|
|
1973
|
+
{ok, _} ->
|
|
1974
|
+
try_each(Rest, Fun);
|
|
1975
|
+
|
|
1976
|
+
{error, E} ->
|
|
1977
|
+
{error, E}
|
|
1978
|
+
end
|
|
1979
|
+
end.
|
|
1980
|
+
|
|
1981
|
+
-file("src/gleam/list.gleam", 1705).
|
|
1982
|
+
-spec partition_loop(list(BGQ), fun((BGQ) -> boolean()), list(BGQ), list(BGQ)) -> {list(BGQ),
|
|
1983
|
+
list(BGQ)}.
|
|
1984
|
+
partition_loop(List, Categorise, Trues, Falses) ->
|
|
1985
|
+
case List of
|
|
1986
|
+
[] ->
|
|
1987
|
+
{lists:reverse(Trues), lists:reverse(Falses)};
|
|
1988
|
+
|
|
1989
|
+
[First | Rest] ->
|
|
1990
|
+
case Categorise(First) of
|
|
1991
|
+
true ->
|
|
1992
|
+
partition_loop(Rest, Categorise, [First | Trues], Falses);
|
|
1993
|
+
|
|
1994
|
+
false ->
|
|
1995
|
+
partition_loop(Rest, Categorise, Trues, [First | Falses])
|
|
1996
|
+
end
|
|
1997
|
+
end.
|
|
1998
|
+
|
|
1999
|
+
-file("src/gleam/list.gleam", 1698).
|
|
2000
|
+
?DOC(
|
|
2001
|
+
" Partitions a list into a tuple/pair of lists\n"
|
|
2002
|
+
" by a given categorisation function.\n"
|
|
2003
|
+
"\n"
|
|
2004
|
+
" ## Examples\n"
|
|
2005
|
+
"\n"
|
|
2006
|
+
" ```gleam\n"
|
|
2007
|
+
" import gleam/int\n"
|
|
2008
|
+
"\n"
|
|
2009
|
+
" assert [1, 2, 3, 4, 5] |> partition(int.is_odd) == #([1, 3, 5], [2, 4])\n"
|
|
2010
|
+
" ```\n"
|
|
2011
|
+
).
|
|
2012
|
+
-spec partition(list(ANV), fun((ANV) -> boolean())) -> {list(ANV), list(ANV)}.
|
|
2013
|
+
partition(List, Categorise) ->
|
|
2014
|
+
partition_loop(List, Categorise, [], []).
|
|
2015
|
+
|
|
2016
|
+
-file("src/gleam/list.gleam", 1782).
|
|
2017
|
+
-spec window_loop(list(list(APC)), list(APC), integer()) -> list(list(APC)).
|
|
2018
|
+
window_loop(Acc, List, N) ->
|
|
2019
|
+
Window = take(List, N),
|
|
2020
|
+
case erlang:length(Window) =:= N of
|
|
2021
|
+
true ->
|
|
2022
|
+
window_loop([Window | Acc], drop(List, 1), N);
|
|
2023
|
+
|
|
2024
|
+
false ->
|
|
2025
|
+
lists:reverse(Acc)
|
|
2026
|
+
end.
|
|
2027
|
+
|
|
2028
|
+
-file("src/gleam/list.gleam", 1775).
|
|
2029
|
+
?DOC(
|
|
2030
|
+
" Returns a list of sliding windows.\n"
|
|
2031
|
+
"\n"
|
|
2032
|
+
" ## Examples\n"
|
|
2033
|
+
"\n"
|
|
2034
|
+
" ```gleam\n"
|
|
2035
|
+
" assert window([1,2,3,4,5], 3) == [[1, 2, 3], [2, 3, 4], [3, 4, 5]]\n"
|
|
2036
|
+
" ```\n"
|
|
2037
|
+
"\n"
|
|
2038
|
+
" ```gleam\n"
|
|
2039
|
+
" assert window([1, 2], 4) == []\n"
|
|
2040
|
+
" ```\n"
|
|
2041
|
+
).
|
|
2042
|
+
-spec window(list(AOY), integer()) -> list(list(AOY)).
|
|
2043
|
+
window(List, N) ->
|
|
2044
|
+
case N =< 0 of
|
|
2045
|
+
true ->
|
|
2046
|
+
[];
|
|
2047
|
+
|
|
2048
|
+
false ->
|
|
2049
|
+
window_loop([], List, N)
|
|
2050
|
+
end.
|
|
2051
|
+
|
|
2052
|
+
-file("src/gleam/list.gleam", 1803).
|
|
2053
|
+
?DOC(
|
|
2054
|
+
" Returns a list of tuples containing two contiguous elements.\n"
|
|
2055
|
+
"\n"
|
|
2056
|
+
" ## Examples\n"
|
|
2057
|
+
"\n"
|
|
2058
|
+
" ```gleam\n"
|
|
2059
|
+
" assert window_by_2([1,2,3,4]) == [#(1, 2), #(2, 3), #(3, 4)]\n"
|
|
2060
|
+
" ```\n"
|
|
2061
|
+
"\n"
|
|
2062
|
+
" ```gleam\n"
|
|
2063
|
+
" assert window_by_2([1]) == []\n"
|
|
2064
|
+
" ```\n"
|
|
2065
|
+
).
|
|
2066
|
+
-spec window_by_2(list(API)) -> list({API, API}).
|
|
2067
|
+
window_by_2(List) ->
|
|
2068
|
+
zip(List, drop(List, 1)).
|
|
2069
|
+
|
|
2070
|
+
-file("src/gleam/list.gleam", 1815).
|
|
2071
|
+
?DOC(
|
|
2072
|
+
" Drops the first elements in a given list for which the predicate function returns `True`.\n"
|
|
2073
|
+
"\n"
|
|
2074
|
+
" ## Examples\n"
|
|
2075
|
+
"\n"
|
|
2076
|
+
" ```gleam\n"
|
|
2077
|
+
" assert drop_while([1, 2, 3, 4], fn (x) { x < 3 }) == [3, 4]\n"
|
|
2078
|
+
" ```\n"
|
|
2079
|
+
).
|
|
2080
|
+
-spec drop_while(list(APL), fun((APL) -> boolean())) -> list(APL).
|
|
2081
|
+
drop_while(List, Predicate) ->
|
|
2082
|
+
case List of
|
|
2083
|
+
[] ->
|
|
2084
|
+
[];
|
|
2085
|
+
|
|
2086
|
+
[First | Rest] ->
|
|
2087
|
+
case Predicate(First) of
|
|
2088
|
+
true ->
|
|
2089
|
+
drop_while(Rest, Predicate);
|
|
2090
|
+
|
|
2091
|
+
false ->
|
|
2092
|
+
[First | Rest]
|
|
2093
|
+
end
|
|
2094
|
+
end.
|
|
2095
|
+
|
|
2096
|
+
-file("src/gleam/list.gleam", 1844).
|
|
2097
|
+
-spec take_while_loop(list(APR), fun((APR) -> boolean()), list(APR)) -> list(APR).
|
|
2098
|
+
take_while_loop(List, Predicate, Acc) ->
|
|
2099
|
+
case List of
|
|
2100
|
+
[] ->
|
|
2101
|
+
lists:reverse(Acc);
|
|
2102
|
+
|
|
2103
|
+
[First | Rest] ->
|
|
2104
|
+
case Predicate(First) of
|
|
2105
|
+
true ->
|
|
2106
|
+
take_while_loop(Rest, Predicate, [First | Acc]);
|
|
2107
|
+
|
|
2108
|
+
false ->
|
|
2109
|
+
lists:reverse(Acc)
|
|
2110
|
+
end
|
|
2111
|
+
end.
|
|
2112
|
+
|
|
2113
|
+
-file("src/gleam/list.gleam", 1837).
|
|
2114
|
+
?DOC(
|
|
2115
|
+
" Takes the first elements in a given list for which the predicate function returns `True`.\n"
|
|
2116
|
+
"\n"
|
|
2117
|
+
" ## Examples\n"
|
|
2118
|
+
"\n"
|
|
2119
|
+
" ```gleam\n"
|
|
2120
|
+
" assert take_while([1, 2, 3, 2, 4], fn (x) { x < 3 }) == [1, 2]\n"
|
|
2121
|
+
" ```\n"
|
|
2122
|
+
).
|
|
2123
|
+
-spec take_while(list(APO), fun((APO) -> boolean())) -> list(APO).
|
|
2124
|
+
take_while(List, Predicate) ->
|
|
2125
|
+
take_while_loop(List, Predicate, []).
|
|
2126
|
+
|
|
2127
|
+
-file("src/gleam/list.gleam", 1876).
|
|
2128
|
+
-spec chunk_loop(list(AQA), fun((AQA) -> AQC), AQC, list(AQA), list(list(AQA))) -> list(list(AQA)).
|
|
2129
|
+
chunk_loop(List, F, Previous_key, Current_chunk, Acc) ->
|
|
2130
|
+
case List of
|
|
2131
|
+
[First | Rest] ->
|
|
2132
|
+
Key = F(First),
|
|
2133
|
+
case Key =:= Previous_key of
|
|
2134
|
+
true ->
|
|
2135
|
+
chunk_loop(Rest, F, Key, [First | Current_chunk], Acc);
|
|
2136
|
+
|
|
2137
|
+
false ->
|
|
2138
|
+
New_acc = [lists:reverse(Current_chunk) | Acc],
|
|
2139
|
+
chunk_loop(Rest, F, Key, [First], New_acc)
|
|
2140
|
+
end;
|
|
2141
|
+
|
|
2142
|
+
[] ->
|
|
2143
|
+
lists:reverse([lists:reverse(Current_chunk) | Acc])
|
|
2144
|
+
end.
|
|
2145
|
+
|
|
2146
|
+
-file("src/gleam/list.gleam", 1869).
|
|
2147
|
+
?DOC(
|
|
2148
|
+
" Returns a list of chunks in which\n"
|
|
2149
|
+
" the return value of calling `f` on each element is the same.\n"
|
|
2150
|
+
"\n"
|
|
2151
|
+
" ## Examples\n"
|
|
2152
|
+
"\n"
|
|
2153
|
+
" ```gleam\n"
|
|
2154
|
+
" assert [1, 2, 2, 3, 4, 4, 6, 7, 7] |> chunk(by: fn(n) { n % 2 })\n"
|
|
2155
|
+
" == [[1], [2, 2], [3], [4, 4, 6], [7, 7]]\n"
|
|
2156
|
+
" ```\n"
|
|
2157
|
+
).
|
|
2158
|
+
-spec chunk(list(APV), fun((APV) -> any())) -> list(list(APV)).
|
|
2159
|
+
chunk(List, F) ->
|
|
2160
|
+
case List of
|
|
2161
|
+
[] ->
|
|
2162
|
+
[];
|
|
2163
|
+
|
|
2164
|
+
[First | Rest] ->
|
|
2165
|
+
chunk_loop(Rest, F, F(First), [First], [])
|
|
2166
|
+
end.
|
|
2167
|
+
|
|
2168
|
+
-file("src/gleam/list.gleam", 1921).
|
|
2169
|
+
-spec sized_chunk_loop(
|
|
2170
|
+
list(AQM),
|
|
2171
|
+
integer(),
|
|
2172
|
+
integer(),
|
|
2173
|
+
list(AQM),
|
|
2174
|
+
list(list(AQM))
|
|
2175
|
+
) -> list(list(AQM)).
|
|
2176
|
+
sized_chunk_loop(List, Count, Left, Current_chunk, Acc) ->
|
|
2177
|
+
case List of
|
|
2178
|
+
[] ->
|
|
2179
|
+
case Current_chunk of
|
|
2180
|
+
[] ->
|
|
2181
|
+
lists:reverse(Acc);
|
|
2182
|
+
|
|
2183
|
+
Remaining ->
|
|
2184
|
+
lists:reverse([lists:reverse(Remaining) | Acc])
|
|
2185
|
+
end;
|
|
2186
|
+
|
|
2187
|
+
[First | Rest] ->
|
|
2188
|
+
Chunk = [First | Current_chunk],
|
|
2189
|
+
case Left > 1 of
|
|
2190
|
+
true ->
|
|
2191
|
+
sized_chunk_loop(Rest, Count, Left - 1, Chunk, Acc);
|
|
2192
|
+
|
|
2193
|
+
false ->
|
|
2194
|
+
sized_chunk_loop(
|
|
2195
|
+
Rest,
|
|
2196
|
+
Count,
|
|
2197
|
+
Count,
|
|
2198
|
+
[],
|
|
2199
|
+
[lists:reverse(Chunk) | Acc]
|
|
2200
|
+
)
|
|
2201
|
+
end
|
|
2202
|
+
end.
|
|
2203
|
+
|
|
2204
|
+
-file("src/gleam/list.gleam", 1917).
|
|
2205
|
+
?DOC(
|
|
2206
|
+
" Returns a list of chunks containing `count` elements each.\n"
|
|
2207
|
+
"\n"
|
|
2208
|
+
" If the last chunk does not have `count` elements, it is instead\n"
|
|
2209
|
+
" a partial chunk, with less than `count` elements.\n"
|
|
2210
|
+
"\n"
|
|
2211
|
+
" For any `count` less than 1 this function behaves as if it was set to 1.\n"
|
|
2212
|
+
"\n"
|
|
2213
|
+
" ## Examples\n"
|
|
2214
|
+
"\n"
|
|
2215
|
+
" ```gleam\n"
|
|
2216
|
+
" assert [1, 2, 3, 4, 5, 6] |> sized_chunk(into: 2)\n"
|
|
2217
|
+
" == [[1, 2], [3, 4], [5, 6]]\n"
|
|
2218
|
+
" ```\n"
|
|
2219
|
+
"\n"
|
|
2220
|
+
" ```gleam\n"
|
|
2221
|
+
" assert [1, 2, 3, 4, 5, 6, 7, 8] |> sized_chunk(into: 3)\n"
|
|
2222
|
+
" == [[1, 2, 3], [4, 5, 6], [7, 8]]\n"
|
|
2223
|
+
" ```\n"
|
|
2224
|
+
).
|
|
2225
|
+
-spec sized_chunk(list(AQI), integer()) -> list(list(AQI)).
|
|
2226
|
+
sized_chunk(List, Count) ->
|
|
2227
|
+
sized_chunk_loop(List, Count, Count, [], []).
|
|
2228
|
+
|
|
2229
|
+
-file("src/gleam/list.gleam", 1963).
|
|
2230
|
+
?DOC(
|
|
2231
|
+
" This function acts similar to fold, but does not take an initial state.\n"
|
|
2232
|
+
" Instead, it starts from the first element in the list\n"
|
|
2233
|
+
" and combines it with each subsequent element in turn using the given\n"
|
|
2234
|
+
" function. The function is called as `fun(accumulator, current_element)`.\n"
|
|
2235
|
+
"\n"
|
|
2236
|
+
" Returns `Ok` to indicate a successful run, and `Error` if called on an\n"
|
|
2237
|
+
" empty list.\n"
|
|
2238
|
+
"\n"
|
|
2239
|
+
" ## Examples\n"
|
|
2240
|
+
"\n"
|
|
2241
|
+
" ```gleam\n"
|
|
2242
|
+
" assert [] |> reduce(fn(acc, x) { acc + x }) == Error(Nil)\n"
|
|
2243
|
+
" ```\n"
|
|
2244
|
+
"\n"
|
|
2245
|
+
" ```gleam\n"
|
|
2246
|
+
" assert [1, 2, 3, 4, 5] |> reduce(fn(acc, x) { acc + x }) == Ok(15)\n"
|
|
2247
|
+
" ```\n"
|
|
2248
|
+
).
|
|
2249
|
+
-spec reduce(list(AQT), fun((AQT, AQT) -> AQT)) -> {ok, AQT} | {error, nil}.
|
|
2250
|
+
reduce(List, Fun) ->
|
|
2251
|
+
case List of
|
|
2252
|
+
[] ->
|
|
2253
|
+
{error, nil};
|
|
2254
|
+
|
|
2255
|
+
[First | Rest] ->
|
|
2256
|
+
{ok, fold(Rest, First, Fun)}
|
|
2257
|
+
end.
|
|
2258
|
+
|
|
2259
|
+
-file("src/gleam/list.gleam", 1987).
|
|
2260
|
+
-spec scan_loop(list(ARB), ARD, list(ARD), fun((ARD, ARB) -> ARD)) -> list(ARD).
|
|
2261
|
+
scan_loop(List, Accumulator, Accumulated, Fun) ->
|
|
2262
|
+
case List of
|
|
2263
|
+
[] ->
|
|
2264
|
+
lists:reverse(Accumulated);
|
|
2265
|
+
|
|
2266
|
+
[First | Rest] ->
|
|
2267
|
+
Next = Fun(Accumulator, First),
|
|
2268
|
+
scan_loop(Rest, Next, [Next | Accumulated], Fun)
|
|
2269
|
+
end.
|
|
2270
|
+
|
|
2271
|
+
-file("src/gleam/list.gleam", 1979).
|
|
2272
|
+
?DOC(
|
|
2273
|
+
" Similar to `fold`, but yields the state of the accumulator at each stage.\n"
|
|
2274
|
+
"\n"
|
|
2275
|
+
" ## Examples\n"
|
|
2276
|
+
"\n"
|
|
2277
|
+
" ```gleam\n"
|
|
2278
|
+
" assert scan(over: [1, 2, 3], from: 100, with: fn(acc, i) { acc + i })\n"
|
|
2279
|
+
" == [101, 103, 106]\n"
|
|
2280
|
+
" ```\n"
|
|
2281
|
+
).
|
|
2282
|
+
-spec scan(list(AQX), AQZ, fun((AQZ, AQX) -> AQZ)) -> list(AQZ).
|
|
2283
|
+
scan(List, Initial, Fun) ->
|
|
2284
|
+
scan_loop(List, Initial, [], Fun).
|
|
2285
|
+
|
|
2286
|
+
-file("src/gleam/list.gleam", 2018).
|
|
2287
|
+
?DOC(
|
|
2288
|
+
" Returns the last element in the given list.\n"
|
|
2289
|
+
"\n"
|
|
2290
|
+
" Returns `Error(Nil)` if the list is empty.\n"
|
|
2291
|
+
"\n"
|
|
2292
|
+
" This function runs in linear time.\n"
|
|
2293
|
+
"\n"
|
|
2294
|
+
" ## Examples\n"
|
|
2295
|
+
"\n"
|
|
2296
|
+
" ```gleam\n"
|
|
2297
|
+
" assert last([]) == Error(Nil)\n"
|
|
2298
|
+
" ```\n"
|
|
2299
|
+
"\n"
|
|
2300
|
+
" ```gleam\n"
|
|
2301
|
+
" assert last([1, 2, 3, 4, 5]) == Ok(5)\n"
|
|
2302
|
+
" ```\n"
|
|
2303
|
+
).
|
|
2304
|
+
-spec last(list(ARG)) -> {ok, ARG} | {error, nil}.
|
|
2305
|
+
last(List) ->
|
|
2306
|
+
case List of
|
|
2307
|
+
[] ->
|
|
2308
|
+
{error, nil};
|
|
2309
|
+
|
|
2310
|
+
[Last] ->
|
|
2311
|
+
{ok, Last};
|
|
2312
|
+
|
|
2313
|
+
[_ | Rest] ->
|
|
2314
|
+
last(Rest)
|
|
2315
|
+
end.
|
|
2316
|
+
|
|
2317
|
+
-file("src/gleam/list.gleam", 2039).
|
|
2318
|
+
?DOC(
|
|
2319
|
+
" Return unique combinations of elements in the list.\n"
|
|
2320
|
+
"\n"
|
|
2321
|
+
" ## Examples\n"
|
|
2322
|
+
"\n"
|
|
2323
|
+
" ```gleam\n"
|
|
2324
|
+
" assert combinations([1, 2, 3], 2) == [[1, 2], [1, 3], [2, 3]]\n"
|
|
2325
|
+
" ```\n"
|
|
2326
|
+
"\n"
|
|
2327
|
+
" ```gleam\n"
|
|
2328
|
+
" assert combinations([1, 2, 3, 4], 3)\n"
|
|
2329
|
+
" == [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]\n"
|
|
2330
|
+
" ```\n"
|
|
2331
|
+
).
|
|
2332
|
+
-spec combinations(list(ARK), integer()) -> list(list(ARK)).
|
|
2333
|
+
combinations(Items, N) ->
|
|
2334
|
+
case {N, Items} of
|
|
2335
|
+
{0, _} ->
|
|
2336
|
+
[[]];
|
|
2337
|
+
|
|
2338
|
+
{_, []} ->
|
|
2339
|
+
[];
|
|
2340
|
+
|
|
2341
|
+
{_, [First | Rest]} ->
|
|
2342
|
+
_pipe = Rest,
|
|
2343
|
+
_pipe@1 = combinations(_pipe, N - 1),
|
|
2344
|
+
_pipe@2 = map(
|
|
2345
|
+
_pipe@1,
|
|
2346
|
+
fun(Combination) -> [First | Combination] end
|
|
2347
|
+
),
|
|
2348
|
+
_pipe@3 = lists:reverse(_pipe@2),
|
|
2349
|
+
fold(_pipe@3, combinations(Rest, N), fun(Acc, C) -> [C | Acc] end)
|
|
2350
|
+
end.
|
|
2351
|
+
|
|
2352
|
+
-file("src/gleam/list.gleam", 2064).
|
|
2353
|
+
-spec combination_pairs_loop(list(ARR), list({ARR, ARR})) -> list({ARR, ARR}).
|
|
2354
|
+
combination_pairs_loop(Items, Acc) ->
|
|
2355
|
+
case Items of
|
|
2356
|
+
[] ->
|
|
2357
|
+
lists:reverse(Acc);
|
|
2358
|
+
|
|
2359
|
+
[First | Rest] ->
|
|
2360
|
+
First_combinations = map(Rest, fun(Other) -> {First, Other} end),
|
|
2361
|
+
Acc@1 = lists:reverse(First_combinations, Acc),
|
|
2362
|
+
combination_pairs_loop(Rest, Acc@1)
|
|
2363
|
+
end.
|
|
2364
|
+
|
|
2365
|
+
-file("src/gleam/list.gleam", 2060).
|
|
2366
|
+
?DOC(
|
|
2367
|
+
" Return unique pair combinations of elements in the list.\n"
|
|
2368
|
+
"\n"
|
|
2369
|
+
" ## Examples\n"
|
|
2370
|
+
"\n"
|
|
2371
|
+
" ```gleam\n"
|
|
2372
|
+
" assert combination_pairs([1, 2, 3]) == [#(1, 2), #(1, 3), #(2, 3)]\n"
|
|
2373
|
+
" ```\n"
|
|
2374
|
+
).
|
|
2375
|
+
-spec combination_pairs(list(ARO)) -> list({ARO, ARO}).
|
|
2376
|
+
combination_pairs(Items) ->
|
|
2377
|
+
combination_pairs_loop(Items, []).
|
|
2378
|
+
|
|
2379
|
+
-file("src/gleam/list.gleam", 2120).
|
|
2380
|
+
-spec take_firsts(list(list(ASL)), list(ASL), list(list(ASL))) -> {list(ASL),
|
|
2381
|
+
list(list(ASL))}.
|
|
2382
|
+
take_firsts(Rows, Column, Remaining_rows) ->
|
|
2383
|
+
case Rows of
|
|
2384
|
+
[] ->
|
|
2385
|
+
{lists:reverse(Column), lists:reverse(Remaining_rows)};
|
|
2386
|
+
|
|
2387
|
+
[[] | Rest] ->
|
|
2388
|
+
take_firsts(Rest, Column, Remaining_rows);
|
|
2389
|
+
|
|
2390
|
+
[[First | Remaining_row] | Rest_rows] ->
|
|
2391
|
+
Remaining_rows@1 = [Remaining_row | Remaining_rows],
|
|
2392
|
+
take_firsts(Rest_rows, [First | Column], Remaining_rows@1)
|
|
2393
|
+
end.
|
|
2394
|
+
|
|
2395
|
+
-file("src/gleam/list.gleam", 2107).
|
|
2396
|
+
-spec transpose_loop(list(list(ASE)), list(list(ASE))) -> list(list(ASE)).
|
|
2397
|
+
transpose_loop(Rows, Columns) ->
|
|
2398
|
+
case Rows of
|
|
2399
|
+
[] ->
|
|
2400
|
+
lists:reverse(Columns);
|
|
2401
|
+
|
|
2402
|
+
_ ->
|
|
2403
|
+
{Column, Rest} = take_firsts(Rows, [], []),
|
|
2404
|
+
case Column of
|
|
2405
|
+
[_ | _] ->
|
|
2406
|
+
transpose_loop(Rest, [Column | Columns]);
|
|
2407
|
+
|
|
2408
|
+
[] ->
|
|
2409
|
+
transpose_loop(Rest, Columns)
|
|
2410
|
+
end
|
|
2411
|
+
end.
|
|
2412
|
+
|
|
2413
|
+
-file("src/gleam/list.gleam", 2103).
|
|
2414
|
+
?DOC(
|
|
2415
|
+
" Transpose rows and columns of the list of lists.\n"
|
|
2416
|
+
"\n"
|
|
2417
|
+
" Notice: This function is not tail recursive,\n"
|
|
2418
|
+
" and thus may exceed stack size if called,\n"
|
|
2419
|
+
" with large lists (on the JavaScript target).\n"
|
|
2420
|
+
"\n"
|
|
2421
|
+
" ## Examples\n"
|
|
2422
|
+
"\n"
|
|
2423
|
+
" ```gleam\n"
|
|
2424
|
+
" assert transpose([[1, 2, 3], [101, 102, 103]])\n"
|
|
2425
|
+
" == [[1, 101], [2, 102], [3, 103]]\n"
|
|
2426
|
+
" ```\n"
|
|
2427
|
+
).
|
|
2428
|
+
-spec transpose(list(list(ARZ))) -> list(list(ARZ)).
|
|
2429
|
+
transpose(List_of_lists) ->
|
|
2430
|
+
transpose_loop(List_of_lists, []).
|
|
2431
|
+
|
|
2432
|
+
-file("src/gleam/list.gleam", 2084).
|
|
2433
|
+
?DOC(
|
|
2434
|
+
" Make a list alternating the elements from the given lists\n"
|
|
2435
|
+
"\n"
|
|
2436
|
+
" ## Examples\n"
|
|
2437
|
+
"\n"
|
|
2438
|
+
" ```gleam\n"
|
|
2439
|
+
" assert interleave([[1, 2], [101, 102], [201, 202]])\n"
|
|
2440
|
+
" == [1, 101, 201, 2, 102, 202]\n"
|
|
2441
|
+
" ```\n"
|
|
2442
|
+
).
|
|
2443
|
+
-spec interleave(list(list(ARV))) -> list(ARV).
|
|
2444
|
+
interleave(List) ->
|
|
2445
|
+
_pipe = List,
|
|
2446
|
+
_pipe@1 = transpose(_pipe),
|
|
2447
|
+
lists:append(_pipe@1).
|
|
2448
|
+
|
|
2449
|
+
-file("src/gleam/list.gleam", 2153).
|
|
2450
|
+
-spec shuffle_pair_unwrap_loop(list({float(), ASX}), list(ASX)) -> list(ASX).
|
|
2451
|
+
shuffle_pair_unwrap_loop(List, Acc) ->
|
|
2452
|
+
case List of
|
|
2453
|
+
[] ->
|
|
2454
|
+
Acc;
|
|
2455
|
+
|
|
2456
|
+
[Elem_pair | Enumerable] ->
|
|
2457
|
+
shuffle_pair_unwrap_loop(
|
|
2458
|
+
Enumerable,
|
|
2459
|
+
[erlang:element(2, Elem_pair) | Acc]
|
|
2460
|
+
)
|
|
2461
|
+
end.
|
|
2462
|
+
|
|
2463
|
+
-file("src/gleam/list.gleam", 2161).
|
|
2464
|
+
-spec do_shuffle_by_pair_indexes(list({float(), ATB})) -> list({float(), ATB}).
|
|
2465
|
+
do_shuffle_by_pair_indexes(List_of_pairs) ->
|
|
2466
|
+
sort(
|
|
2467
|
+
List_of_pairs,
|
|
2468
|
+
fun(A_pair, B_pair) ->
|
|
2469
|
+
gleam@float:compare(
|
|
2470
|
+
erlang:element(1, A_pair),
|
|
2471
|
+
erlang:element(1, B_pair)
|
|
2472
|
+
)
|
|
2473
|
+
end
|
|
2474
|
+
).
|
|
2475
|
+
|
|
2476
|
+
-file("src/gleam/list.gleam", 2146).
|
|
2477
|
+
?DOC(
|
|
2478
|
+
" Takes a list, randomly sorts all items and returns the shuffled list.\n"
|
|
2479
|
+
"\n"
|
|
2480
|
+
" This function uses `float.random` to decide the order of the elements.\n"
|
|
2481
|
+
"\n"
|
|
2482
|
+
" ## Example\n"
|
|
2483
|
+
"\n"
|
|
2484
|
+
" ```gleam\n"
|
|
2485
|
+
" [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |> shuffle\n"
|
|
2486
|
+
" // -> [1, 6, 9, 10, 3, 8, 4, 2, 7, 5]\n"
|
|
2487
|
+
" ```\n"
|
|
2488
|
+
).
|
|
2489
|
+
-spec shuffle(list(ASU)) -> list(ASU).
|
|
2490
|
+
shuffle(List) ->
|
|
2491
|
+
_pipe = List,
|
|
2492
|
+
_pipe@1 = fold(_pipe, [], fun(Acc, A) -> [{rand:uniform(), A} | Acc] end),
|
|
2493
|
+
_pipe@2 = do_shuffle_by_pair_indexes(_pipe@1),
|
|
2494
|
+
shuffle_pair_unwrap_loop(_pipe@2, []).
|
|
2495
|
+
|
|
2496
|
+
-file("src/gleam/list.gleam", 2191).
|
|
2497
|
+
-spec max_loop(list(ATL), fun((ATL, ATL) -> gleam@order:order()), ATL) -> ATL.
|
|
2498
|
+
max_loop(List, Compare, Max) ->
|
|
2499
|
+
case List of
|
|
2500
|
+
[] ->
|
|
2501
|
+
Max;
|
|
2502
|
+
|
|
2503
|
+
[First | Rest] ->
|
|
2504
|
+
case Compare(First, Max) of
|
|
2505
|
+
gt ->
|
|
2506
|
+
max_loop(Rest, Compare, First);
|
|
2507
|
+
|
|
2508
|
+
lt ->
|
|
2509
|
+
max_loop(Rest, Compare, Max);
|
|
2510
|
+
|
|
2511
|
+
eq ->
|
|
2512
|
+
max_loop(Rest, Compare, Max)
|
|
2513
|
+
end
|
|
2514
|
+
end.
|
|
2515
|
+
|
|
2516
|
+
-file("src/gleam/list.gleam", 2181).
|
|
2517
|
+
?DOC(
|
|
2518
|
+
" Takes a list and a comparator, and returns the maximum element in the list\n"
|
|
2519
|
+
"\n"
|
|
2520
|
+
" ## Examples\n"
|
|
2521
|
+
"\n"
|
|
2522
|
+
" ```gleam\n"
|
|
2523
|
+
" assert [1, 2, 3, 4, 5] |> list.max(int.compare) == Ok(5)\n"
|
|
2524
|
+
" ```\n"
|
|
2525
|
+
"\n"
|
|
2526
|
+
" ```gleam\n"
|
|
2527
|
+
" assert [\"a\", \"c\", \"b\"] |> list.max(string.compare) == Ok(\"c\")\n"
|
|
2528
|
+
" ```\n"
|
|
2529
|
+
).
|
|
2530
|
+
-spec max(list(ATE), fun((ATE, ATE) -> gleam@order:order())) -> {ok, ATE} |
|
|
2531
|
+
{error, nil}.
|
|
2532
|
+
max(List, Compare) ->
|
|
2533
|
+
case List of
|
|
2534
|
+
[] ->
|
|
2535
|
+
{error, nil};
|
|
2536
|
+
|
|
2537
|
+
[First | Rest] ->
|
|
2538
|
+
{ok, max_loop(Rest, Compare, First)}
|
|
2539
|
+
end.
|
|
2540
|
+
|
|
2541
|
+
-file("src/gleam/list.gleam", 2272).
|
|
2542
|
+
-spec build_reservoir_loop(
|
|
2543
|
+
list(AUA),
|
|
2544
|
+
integer(),
|
|
2545
|
+
gleam@dict:dict(integer(), AUA)
|
|
2546
|
+
) -> {gleam@dict:dict(integer(), AUA), list(AUA)}.
|
|
2547
|
+
build_reservoir_loop(List, Size, Reservoir) ->
|
|
2548
|
+
Reservoir_size = maps:size(Reservoir),
|
|
2549
|
+
case Reservoir_size >= Size of
|
|
2550
|
+
true ->
|
|
2551
|
+
{Reservoir, List};
|
|
2552
|
+
|
|
2553
|
+
false ->
|
|
2554
|
+
case List of
|
|
2555
|
+
[] ->
|
|
2556
|
+
{Reservoir, []};
|
|
2557
|
+
|
|
2558
|
+
[First | Rest] ->
|
|
2559
|
+
Reservoir@1 = gleam@dict:insert(
|
|
2560
|
+
Reservoir,
|
|
2561
|
+
Reservoir_size,
|
|
2562
|
+
First
|
|
2563
|
+
),
|
|
2564
|
+
build_reservoir_loop(Rest, Size, Reservoir@1)
|
|
2565
|
+
end
|
|
2566
|
+
end.
|
|
2567
|
+
|
|
2568
|
+
-file("src/gleam/list.gleam", 2268).
|
|
2569
|
+
?DOC(
|
|
2570
|
+
" Builds the initial reservoir used by Algorithm L.\n"
|
|
2571
|
+
" This is a dictionary with keys ranging from `0` up to `n - 1` where each\n"
|
|
2572
|
+
" value is the corresponding element at that position in `list`.\n"
|
|
2573
|
+
"\n"
|
|
2574
|
+
" This also returns the remaining elements of `list` that didn't end up in\n"
|
|
2575
|
+
" the reservoir.\n"
|
|
2576
|
+
).
|
|
2577
|
+
-spec build_reservoir(list(ATV), integer()) -> {gleam@dict:dict(integer(), ATV),
|
|
2578
|
+
list(ATV)}.
|
|
2579
|
+
build_reservoir(List, N) ->
|
|
2580
|
+
build_reservoir_loop(List, N, maps:new()).
|
|
2581
|
+
|
|
2582
|
+
-file("src/gleam/list.gleam", 2256).
|
|
2583
|
+
-spec log_random() -> float().
|
|
2584
|
+
log_random() ->
|
|
2585
|
+
Random@1 = case gleam@float:logarithm(
|
|
2586
|
+
rand:uniform() + 2.2250738585072014e-308
|
|
2587
|
+
) of
|
|
2588
|
+
{ok, Random} -> Random;
|
|
2589
|
+
_assert_fail ->
|
|
2590
|
+
erlang:error(#{gleam_error => let_assert,
|
|
2591
|
+
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
|
|
2592
|
+
file => <<?FILEPATH/utf8>>,
|
|
2593
|
+
module => <<"gleam/list"/utf8>>,
|
|
2594
|
+
function => <<"log_random"/utf8>>,
|
|
2595
|
+
line => 2257,
|
|
2596
|
+
value => _assert_fail,
|
|
2597
|
+
start => 55515,
|
|
2598
|
+
'end' => 55586,
|
|
2599
|
+
pattern_start => 55526,
|
|
2600
|
+
pattern_end => 55536})
|
|
2601
|
+
end,
|
|
2602
|
+
Random@1.
|
|
2603
|
+
|
|
2604
|
+
-file("src/gleam/list.gleam", 2233).
|
|
2605
|
+
-spec sample_loop(
|
|
2606
|
+
list(ATP),
|
|
2607
|
+
gleam@dict:dict(integer(), ATP),
|
|
2608
|
+
integer(),
|
|
2609
|
+
float()
|
|
2610
|
+
) -> gleam@dict:dict(integer(), ATP).
|
|
2611
|
+
sample_loop(List, Reservoir, N, W) ->
|
|
2612
|
+
Skip = begin
|
|
2613
|
+
Log@1 = case gleam@float:logarithm(1.0 - W) of
|
|
2614
|
+
{ok, Log} -> Log;
|
|
2615
|
+
_assert_fail ->
|
|
2616
|
+
erlang:error(#{gleam_error => let_assert,
|
|
2617
|
+
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
|
|
2618
|
+
file => <<?FILEPATH/utf8>>,
|
|
2619
|
+
module => <<"gleam/list"/utf8>>,
|
|
2620
|
+
function => <<"sample_loop"/utf8>>,
|
|
2621
|
+
line => 2240,
|
|
2622
|
+
value => _assert_fail,
|
|
2623
|
+
start => 55076,
|
|
2624
|
+
'end' => 55122,
|
|
2625
|
+
pattern_start => 55087,
|
|
2626
|
+
pattern_end => 55094})
|
|
2627
|
+
end,
|
|
2628
|
+
erlang:round(math:floor(case Log@1 of
|
|
2629
|
+
+0.0 -> +0.0;
|
|
2630
|
+
-0.0 -> -0.0;
|
|
2631
|
+
Gleam@denominator -> log_random() / Gleam@denominator
|
|
2632
|
+
end))
|
|
2633
|
+
end,
|
|
2634
|
+
case drop(List, Skip) of
|
|
2635
|
+
[] ->
|
|
2636
|
+
Reservoir;
|
|
2637
|
+
|
|
2638
|
+
[First | Rest] ->
|
|
2639
|
+
Reservoir@1 = gleam@dict:insert(
|
|
2640
|
+
Reservoir,
|
|
2641
|
+
gleam@int:random(N),
|
|
2642
|
+
First
|
|
2643
|
+
),
|
|
2644
|
+
W@1 = W * math:exp(case erlang:float(N) of
|
|
2645
|
+
+0.0 -> +0.0;
|
|
2646
|
+
-0.0 -> -0.0;
|
|
2647
|
+
Gleam@denominator@1 -> log_random() / Gleam@denominator@1
|
|
2648
|
+
end),
|
|
2649
|
+
sample_loop(Rest, Reservoir@1, N, W@1)
|
|
2650
|
+
end.
|
|
2651
|
+
|
|
2652
|
+
-file("src/gleam/list.gleam", 2215).
|
|
2653
|
+
?DOC(
|
|
2654
|
+
" Returns a random sample of up to n elements from a list using reservoir\n"
|
|
2655
|
+
" sampling via [Algorithm L](https://en.wikipedia.org/wiki/Reservoir_sampling#Optimal:_Algorithm_L).\n"
|
|
2656
|
+
" Returns an empty list if the sample size is less than or equal to 0.\n"
|
|
2657
|
+
"\n"
|
|
2658
|
+
" Order is not random, only selection is.\n"
|
|
2659
|
+
"\n"
|
|
2660
|
+
" ## Examples\n"
|
|
2661
|
+
"\n"
|
|
2662
|
+
" ```gleam\n"
|
|
2663
|
+
" sample([1, 2, 3, 4, 5], 3)\n"
|
|
2664
|
+
" // -> [2, 4, 5] // A random sample of 3 items\n"
|
|
2665
|
+
" ```\n"
|
|
2666
|
+
).
|
|
2667
|
+
-spec sample(list(ATM), integer()) -> list(ATM).
|
|
2668
|
+
sample(List, N) ->
|
|
2669
|
+
{Reservoir, Rest} = build_reservoir(List, N),
|
|
2670
|
+
case gleam@dict:is_empty(Reservoir) of
|
|
2671
|
+
true ->
|
|
2672
|
+
[];
|
|
2673
|
+
|
|
2674
|
+
false ->
|
|
2675
|
+
W = math:exp(case erlang:float(N) of
|
|
2676
|
+
+0.0 -> +0.0;
|
|
2677
|
+
-0.0 -> -0.0;
|
|
2678
|
+
Gleam@denominator -> log_random() / Gleam@denominator
|
|
2679
|
+
end),
|
|
2680
|
+
maps:values(sample_loop(Rest, Reservoir, N, W))
|
|
2681
|
+
end.
|
|
2682
|
+
|
|
2683
|
+
-file("src/gleam/list.gleam", 1731).
|
|
2684
|
+
-spec permutation_zip(list(AOI), list(AOI), list(list(AOI))) -> list(list(AOI)).
|
|
2685
|
+
permutation_zip(List, Rest, Acc) ->
|
|
2686
|
+
case List of
|
|
2687
|
+
[] ->
|
|
2688
|
+
lists:reverse(Acc);
|
|
2689
|
+
|
|
2690
|
+
[Head | Tail] ->
|
|
2691
|
+
permutation_prepend(
|
|
2692
|
+
Head,
|
|
2693
|
+
permutations(lists:reverse(Rest, Tail)),
|
|
2694
|
+
Tail,
|
|
2695
|
+
[Head | Rest],
|
|
2696
|
+
Acc
|
|
2697
|
+
)
|
|
2698
|
+
end.
|
|
2699
|
+
|
|
2700
|
+
-file("src/gleam/list.gleam", 1724).
|
|
2701
|
+
?DOC(
|
|
2702
|
+
" Returns all the permutations of a list.\n"
|
|
2703
|
+
"\n"
|
|
2704
|
+
" ## Examples\n"
|
|
2705
|
+
"\n"
|
|
2706
|
+
" ```gleam\n"
|
|
2707
|
+
" assert permutations([1, 2]) == [[1, 2], [2, 1]]\n"
|
|
2708
|
+
" ```\n"
|
|
2709
|
+
).
|
|
2710
|
+
-spec permutations(list(AOE)) -> list(list(AOE)).
|
|
2711
|
+
permutations(List) ->
|
|
2712
|
+
case List of
|
|
2713
|
+
[] ->
|
|
2714
|
+
[[]];
|
|
2715
|
+
|
|
2716
|
+
L ->
|
|
2717
|
+
permutation_zip(L, [], [])
|
|
2718
|
+
end.
|
|
2719
|
+
|
|
2720
|
+
-file("src/gleam/list.gleam", 1749).
|
|
2721
|
+
-spec permutation_prepend(
|
|
2722
|
+
AOP,
|
|
2723
|
+
list(list(AOP)),
|
|
2724
|
+
list(AOP),
|
|
2725
|
+
list(AOP),
|
|
2726
|
+
list(list(AOP))
|
|
2727
|
+
) -> list(list(AOP)).
|
|
2728
|
+
permutation_prepend(El, Permutations, List_1, List_2, Acc) ->
|
|
2729
|
+
case Permutations of
|
|
2730
|
+
[] ->
|
|
2731
|
+
permutation_zip(List_1, List_2, Acc);
|
|
2732
|
+
|
|
2733
|
+
[Head | Tail] ->
|
|
2734
|
+
permutation_prepend(El, Tail, List_1, List_2, [[El | Head] | Acc])
|
|
2735
|
+
end.
|