added List.keep_if_try! (#7804)

* added keep_if_try!

* cleanup types

* cleanup var name
This commit is contained in:
Anton-4 2025-05-21 21:12:24 +02:00 committed by GitHub
parent e1f4ac1a96
commit c93e27ca9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 1479 additions and 1382 deletions

View file

@ -1050,6 +1050,23 @@ mod cli_tests {
let cli_dev_out = cli_dev.run();
cli_dev_out.assert_clean_success();
}
#[test]
#[cfg_attr(windows, ignore)]
fn effectful_keep_if_try() {
build_platform_host();
let cli_dev = ExecCli::new(
roc_cli::CMD_DEV,
file_from_root(
"crates/cli/tests/test-projects/effectful",
"keep_if_try.roc",
),
);
let cli_dev_out = cli_dev.run();
cli_dev_out.assert_clean_success();
}
}
// this is for testing the benchmarks (on small inputs), to perform proper benchmarks see crates/cli/benches/README.md

View file

@ -0,0 +1,56 @@
app [main!] { pf: platform "../test-platform-effects-zig/main.roc" }
import pf.Effect
main! : {} => {}
main! = \{} ->
when run!({}) is
Ok({}) ->
Effect.put_line!("Success!")
Err(err) ->
crash "Error: ${Inspect.to_str(err)}"
run! : {} => Result {} [StrErr(Str), NotZeroOrOne]
run! = \{} ->
sanity_check_bool = is_zero!(0)?
err_on_false(sanity_check_bool, "sanity check")?
two_zeros = List.keep_if_try!([1, 0, 0], is_zero!)?
err_on_false((two_zeros == [0, 0]), "test 1")?
one_zero = List.keep_if_try!([0, 1], is_zero!)?
err_on_false((one_zero == [0]), "test 2")?
one_zero_again = List.keep_if_try!([1, 0], is_zero!)?
err_on_false((one_zero_again == [0]), "test 3")?
three_zeros = List.keep_if_try!([0, 0, 0], is_zero!)?
err_on_false((three_zeros == [0, 0, 0]), "test 4")?
just_one = List.keep_if_try!([1], is_zero!)?
err_on_false((just_one == []), "test 5")?
one_zero_input = List.keep_if_try!([0], is_zero!)?
err_on_false((one_zero_input == [0]), "test 6")?
empty_list = List.keep_if_try!([], is_zero!)?
err_on_false((empty_list == []), "test 7")?
Ok({})
is_zero! : U64 => Result Bool [NotZeroOrOne]
is_zero! = \num ->
_ = Effect.id_effectful!(num)
if num == 0 then
Ok(Bool.true)
else if num == 1 then
Ok(Bool.false)
else
Err(NotZeroOrOne)
err_on_false : Bool, Str -> Result {} [StrErr(Str)]
err_on_false = |bool, test_name|
if bool then
Ok({})
else
Err(StrErr("Test failed: ${Inspect.to_str(test_name)}"))