Fix builtin formatting

This commit is contained in:
Anthony Bullard 2025-01-08 15:20:04 -06:00
parent 739dda6f5e
commit 3a6225c354
No known key found for this signature in database
6 changed files with 227 additions and 142 deletions

View file

@ -133,8 +133,10 @@ hash_dict = \hasher, dict -> Hash.hash_unordered(hasher, to_list(dict), List.wal
to_inspector_dict : Dict k v -> Inspector f where k implements Inspect & Hash & Eq, v implements Inspect, f implements InspectFormatter
to_inspector_dict = \dict ->
Inspect.custom(\fmt ->
Inspect.apply(Inspect.dict(dict, walk, Inspect.to_inspector, Inspect.to_inspector), fmt))
Inspect.custom(
\fmt ->
Inspect.apply(Inspect.dict(dict, walk, Inspect.to_inspector, Inspect.to_inspector), fmt),
)
## Return an empty dictionary.
## ```roc
@ -142,13 +144,15 @@ to_inspector_dict = \dict ->
## ```
empty : {} -> Dict * *
empty = \{} ->
@Dict({
@Dict(
{
buckets: [],
data: [],
max_bucket_capacity: 0,
max_load_factor: default_max_load_factor,
shifts: initial_shifts,
})
},
)
## Return a dictionary with space allocated for a number of entries. This
## may provide a performance optimization if you know how many entries will be
@ -169,13 +173,15 @@ reserve = \@Dict({ buckets, data, max_bucket_capacity: original_max_bucket_capac
if List.is_empty(buckets) || requested_shifts > shifts then
(buckets0, max_bucket_capacity) = alloc_buckets_from_shift(requested_shifts, max_load_factor)
buckets1 = fill_buckets_from_data(buckets0, data, requested_shifts)
@Dict({
@Dict(
{
buckets: buckets1,
data: List.reserve(data, Num.sub_saturated(size, current_size)),
max_bucket_capacity,
max_load_factor,
shifts: requested_shifts,
})
},
)
else
@Dict({ buckets, data, max_bucket_capacity: original_max_bucket_capacity, max_load_factor, shifts })
@ -191,13 +197,15 @@ release_excess_capacity = \@Dict({ buckets, data, max_bucket_capacity: original_
if min_shifts < shifts then
(buckets0, max_bucket_capacity) = alloc_buckets_from_shift(min_shifts, max_load_factor)
buckets1 = fill_buckets_from_data(buckets0, data, min_shifts)
@Dict({
@Dict(
{
buckets: buckets1,
data: List.release_excess_capacity(data),
max_bucket_capacity,
max_load_factor,
shifts: min_shifts,
})
},
)
else
@Dict({ buckets, data, max_bucket_capacity: original_max_bucket_capacity, max_load_factor, shifts })
@ -280,14 +288,16 @@ is_empty = \@Dict({ data }) ->
## ```
clear : Dict k v -> Dict k v
clear = \@Dict({ buckets, data, max_bucket_capacity, max_load_factor, shifts }) ->
@Dict({
@Dict(
{
buckets: List.map(buckets, \_ -> empty_bucket),
# use take_first to keep around the capacity
data: List.take_first(data, 0),
max_bucket_capacity,
max_load_factor,
shifts,
})
},
)
## Convert each value in the dictionary to something new, by calling a conversion
## function on each of them which receives both the key and the old value. Then return a
@ -822,21 +832,25 @@ remove_bucket = \@Dict({ buckets: buckets0, data: data0, max_bucket_capacity, ma
bucket_index3 = scan_for_index(buckets2, bucket_index2, Num.to_u32(last_data_index))
swap_bucket = list_get_unsafe(buckets2, bucket_index3)
@Dict({
@Dict(
{
buckets: List.set(buckets2, bucket_index3, { swap_bucket & data_index: data_index_to_remove }),
data: List.drop_last(data1, 1),
max_bucket_capacity,
max_load_factor,
shifts,
})
},
)
else
@Dict({
@Dict(
{
buckets: buckets2,
data: List.drop_last(data0, 1),
max_bucket_capacity,
max_load_factor,
shifts,
})
},
)
scan_for_index : List Bucket, U64, U32 -> U64
scan_for_index = \buckets, bucket_index, data_index ->
@ -863,13 +877,15 @@ increase_size = \@Dict({ data, max_bucket_capacity, max_load_factor, shifts }) -
new_shifts = shifts |> Num.sub_wrap(1)
(buckets0, new_max_bucket_capacity) = alloc_buckets_from_shift(new_shifts, max_load_factor)
buckets1 = fill_buckets_from_data(buckets0, data, new_shifts)
@Dict({
@Dict(
{
buckets: buckets1,
data,
max_bucket_capacity: new_max_bucket_capacity,
max_load_factor,
shifts: new_shifts,
})
},
)
else
crash("Dict hit limit of $(Num.to_str(max_bucket_count)) elements. Unable to grow more.")

View file

@ -1092,11 +1092,15 @@ min = \list ->
min_help : List (Num a), Num a -> Num a
min_help = \list, initial ->
List.walk(list, initial, \best_so_far, current ->
List.walk(
list,
initial,
\best_so_far, current ->
if current < best_so_far then
current
else
best_so_far)
best_so_far,
)
max : List (Num a) -> Result (Num a) [ListWasEmpty]
max = \list ->
@ -1109,11 +1113,15 @@ max = \list ->
max_help : List (Num a), Num a -> Num a
max_help = \list, initial ->
List.walk(list, initial, \best_so_far, current ->
List.walk(
list,
initial,
\best_so_far, current ->
if current > best_so_far then
current
else
best_so_far)
best_so_far,
)
## Like [List.map], except the transformation function wraps the return value
## in a list. At the end, all the lists get joined together into one list.
@ -1156,11 +1164,15 @@ find_last = \list, pred ->
## If no satisfying element is found, an `Err NotFound` is returned.
find_first_index : List elem, (elem -> Bool) -> Result U64 [NotFound]
find_first_index = \list, matcher ->
found_index = List.iterate(list, 0, \index, elem ->
found_index = List.iterate(
list,
0,
\index, elem ->
if matcher(elem) then
Break(index)
else
Continue(Num.add_wrap(index, 1)))
Continue(Num.add_wrap(index, 1)),
)
when found_index is
Break(index) -> Ok(index)
@ -1171,13 +1183,17 @@ find_first_index = \list, matcher ->
## If no satisfying element is found, an `Err NotFound` is returned.
find_last_index : List elem, (elem -> Bool) -> Result U64 [NotFound]
find_last_index = \list, matches ->
found_index = List.iterate_backwards(list, List.len(list), \prev_index, elem ->
found_index = List.iterate_backwards(
list,
List.len(list),
\prev_index, elem ->
answer = Num.sub_wrap(prev_index, 1)
if matches(elem) then
Break(answer)
else
Continue(answer))
Continue(answer),
)
when found_index is
Break(index) -> Ok(index)
@ -1214,10 +1230,14 @@ intersperse = \list, sep ->
capacity = 2 * List.len(list)
init = List.with_capacity(capacity)
new_list =
List.walk(list, init, \acc, elem ->
List.walk(
list,
init,
\acc, elem ->
acc
|> List.append_unsafe(elem)
|> List.append_unsafe(sep))
|> List.append_unsafe(sep),
)
List.drop_last(new_list, 1)
@ -1359,9 +1379,16 @@ chunks_of_help = \list_rest, chunk_size, chunks ->
## If it returns `Ok` for every element, [map_try] returns `Ok` with the transformed list.
map_try : List elem, (elem -> Result ok err) -> Result (List ok) err
map_try = \list, to_result ->
walk_try(list, [], \state, elem ->
Result.map(to_result(elem), \ok ->
List.append(state, ok)))
walk_try(
list,
[],
\state, elem ->
Result.map(
to_result(elem),
\ok ->
List.append(state, ok),
),
)
## Same as [List.walk], except you can stop walking early by returning `Err`.
##

View file

@ -51,19 +51,25 @@ is_eq = \xs, ys ->
if len(xs) != len(ys) then
Bool.false
else
walk_until(xs, Bool.true, \_, elem ->
walk_until(
xs,
Bool.true,
\_, elem ->
if contains(ys, elem) then
Continue(Bool.true)
else
Break(Bool.false))
Break(Bool.false),
)
hash_set : hasher, Set k -> hasher where hasher implements Hasher
hash_set = \hasher, @Set(inner) -> Hash.hash(hasher, inner)
to_inspector_set : Set k -> Inspector f where k implements Inspect & Hash & Eq, f implements InspectFormatter
to_inspector_set = \set ->
Inspect.custom(\fmt ->
Inspect.apply(Inspect.set(set, walk, Inspect.to_inspector), fmt))
Inspect.custom(
\fmt ->
Inspect.apply(Inspect.set(set, walk, Inspect.to_inspector), fmt),
)
## Creates a new empty `Set`.
## ```roc
@ -326,8 +332,12 @@ map : Set a, (a -> b) -> Set b
map = \set, transform ->
init = with_capacity(capacity(set))
walk(set, init, \answer, k ->
insert(answer, transform(k)))
walk(
set,
init,
\answer, k ->
insert(answer, transform(k)),
)
## Like [Set.map], except the transformation function wraps the return value
## in a set. At the end, all the sets get joined together
@ -338,8 +348,12 @@ join_map : Set a, (a -> Set b) -> Set b
join_map = \set, transform ->
init = with_capacity(capacity(set)) # Might be a pessimization
walk(set, init, \answer, k ->
union(answer, transform(k)))
walk(
set,
init,
\answer, k ->
union(answer, transform(k)),
)
## Iterate through the values of a given `Set` and build a value, can stop
## iterating part way through the collection.

View file

@ -940,14 +940,16 @@ matches_at = \haystack, haystack_index, needle ->
needle_length = Str.count_utf8_bytes(needle)
end_index = min(Num.add_saturated(haystack_index, needle_length), haystack_length)
matches_at_help({
matches_at_help(
{
haystack,
haystack_index,
needle,
needle_index: 0,
needle_length,
end_index,
})
},
)
matches_at_help = \state ->
{ haystack, haystack_index, needle, needle_index, needle_length, end_index } = state

View file

@ -108,10 +108,12 @@ err = \a -> @Task(\{} -> Err(a))
## matching to handle the success and possible failure cases.
attempt : Task a b, (Result a b -> Task c d) -> Task c d
attempt = \@Task(task), transform ->
@Task(\{} ->
@Task(
\{} ->
@Task(transformed) = transform(task({}))
transformed({}))
transformed({}),
)
## Take the success value from a given [Task] and use that to generate a new [Task].
##
@ -131,14 +133,16 @@ attempt = \@Task(task), transform ->
## ```
await : Task a b, (a -> Task c b) -> Task c b
await = \@Task(task), transform ->
@Task(\{} ->
@Task(
\{} ->
when task({}) is
Ok(a) ->
@Task(transformed) = transform(a)
transformed({})
Err(b) ->
Err(b))
Err(b),
)
## Take the error value from a given [Task] and use that to generate a new [Task].
##
@ -149,14 +153,16 @@ await = \@Task(task), transform ->
## ```
on_err : Task a b, (b -> Task a c) -> Task a c
on_err = \@Task(task), transform ->
@Task(\{} ->
@Task(
\{} ->
when task({}) is
Ok(a) ->
Ok(a)
Err(b) ->
@Task(transformed) = transform(b)
transformed({}))
transformed({}),
)
## Transform the success value of a given [Task] with a given function.
##
@ -167,10 +173,12 @@ on_err = \@Task(task), transform ->
## ```
map : Task a c, (a -> b) -> Task b c
map = \@Task(task), transform ->
@Task(\{} ->
@Task(
\{} ->
when task({}) is
Ok(a) -> Ok(transform(a))
Err(b) -> Err(b))
Err(b) -> Err(b),
)
## Transform the error value of a given [Task] with a given function.
##
@ -181,10 +189,12 @@ map = \@Task(task), transform ->
## ```
map_err : Task c a, (a -> b) -> Task c b
map_err = \@Task(task), transform ->
@Task(\{} ->
@Task(
\{} ->
when task({}) is
Ok(a) -> Ok(a)
Err(b) -> Err(transform(b)))
Err(b) -> Err(transform(b)),
)
## Use a Result among other Tasks by converting it into a [Task].
from_result : Result a b -> Task a b
@ -197,8 +207,11 @@ from_result = \res ->
batch : Task a c -> (Task (a -> b) c -> Task b c)
batch = \current ->
\next ->
await(next, \f ->
map(current, f))
await(
next,
\f ->
map(current, f),
)
## Combine the values of two tasks with a custom combining function.
##
@ -214,11 +227,13 @@ batch = \current ->
## ```
combine : Task a err, Task b err, (a, b -> c) -> Task c err
combine = \@Task(left_task), @Task(right_task), combiner ->
@Task(\{} ->
@Task(
\{} ->
left = try(left_task, {})
right = try(right_task, {})
Ok(combiner(left, right)))
Ok(combiner(left, right)),
)
## Apply each task in a list sequentially, and return a list of the resulting values.
## Each task will be awaited before beginning the next task.
@ -232,14 +247,20 @@ combine = \@Task(left_task), @Task(right_task), combiner ->
##
sequence : List (Task ok err) -> Task (List ok) err
sequence = \task_list ->
Task.loop((task_list, List.with_capacity(List.len(task_list))), \(tasks, values) ->
Task.loop(
(task_list, List.with_capacity(List.len(task_list))),
\(tasks, values) ->
when tasks is
[task, .. as rest] ->
Task.map(task, \value ->
Step((rest, List.append(values, value))))
Task.map(
task,
\value ->
Step((rest, List.append(values, value))),
)
[] ->
Task.ok(Done(values)))
Task.ok(Done(values)),
)
## Apply a task repeatedly for each item in a list
##
@ -253,8 +274,12 @@ sequence = \task_list ->
##
for_each : List a, (a -> Task {} b) -> Task {} b
for_each = \items, fn ->
List.walk(items, ok({}), \state, item ->
state |> await(\_ -> fn(item)))
List.walk(
items,
ok({}),
\state, item ->
state |> await(\_ -> fn(item)),
)
## Transform a task that can either succeed with `ok`, or fail with `err`, into
## a task that succeeds with `Result ok err`.
@ -274,5 +299,7 @@ for_each = \items, fn ->
##
result : Task ok err -> Task (Result ok err) *
result = \@Task(task) ->
@Task(\{} ->
Ok(task({})))
@Task(
\{} ->
Ok(task({})),
)

View file

@ -107,7 +107,6 @@ fn format_expr_only(
if buf.flags().parens_and_commas {
fmt_pnc_apply(loc_expr, &Collection::with_items(loc_args), indent, buf);
} else if !apply_needs_parens || loc_args.is_empty() {
println!("No need parens");
fmt_apply(loc_expr, loc_args, indent, buf);
} else {
fmt_parens(item, buf, indent);