From 01f00ca8a3b162f0548eb78b55cf39ad2e97ea21 Mon Sep 17 00:00:00 2001 From: vkobinski Date: Fri, 28 Jun 2024 22:37:11 -0300 Subject: [PATCH] Fixed examples --- crates/benda/src/benda_ffi/mod.rs | 2 +- examples/insertion_sort.py | 15 ++++++++++++++- examples/quicksort.py | 23 ++++++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/crates/benda/src/benda_ffi/mod.rs b/crates/benda/src/benda_ffi/mod.rs index 5995d7e..afbe41d 100644 --- a/crates/benda/src/benda_ffi/mod.rs +++ b/crates/benda/src/benda_ffi/mod.rs @@ -9,7 +9,7 @@ pub fn run(book: &Book) -> Option<(Term, String, Diagnostics)> { let args = None; bend::run_book( - book.clone(), + book.to_owned(), run_opts, compile_opts, diagnostics_cfg, diff --git a/examples/insertion_sort.py b/examples/insertion_sort.py index 597a6ce..2aebacc 100644 --- a/examples/insertion_sort.py +++ b/examples/insertion_sort.py @@ -18,12 +18,25 @@ def random(n): else: return (random(n - 1) * 16 + 101387) % 429453 +def print_list(list): + print("[", end="") + while True: + match list: + case book.adts.List.tCons(value, tail): + print(value, end=", ") + list = tail + case book.adts.List.tNil(): + break + print("]") + def main(): data = rnd(10) result = book.defs.insertion_sort(data) - print("Result: ", result.to_adt(book.adts.List)) + sorted = result.to_adt(book.adts.List) + print("Result: ", end="") + print_list(sorted) if __name__ == "__main__": diff --git a/examples/quicksort.py b/examples/quicksort.py index 43465e0..70a5b89 100644 --- a/examples/quicksort.py +++ b/examples/quicksort.py @@ -60,8 +60,20 @@ def from_cons_list(xs) -> list[u24]: result.append(value) xs = tail +def print_list(list): + print("[", end="") + while True: + match list: + case book.adts.List.tCons(value, tail): + print(value, end=", ") + list = tail + case book.adts.List.tNil(): + break + print("]") + + def main(): - data = gen_list(10, 1000) + data = gen_list(5, 1000) print("Data: ", data) expected = sorted(data) @@ -71,8 +83,13 @@ def main(): sorted_res = book.defs.Sort(cons_list) sorted_arr = sorted_res.to_adt(book.adts.List) - print("Result: ", sorted_arr) - print("Sum: ", book.defs.Sum(sorted_res)) + + sum = book.defs.Sum(sorted_res) + + + print("Result: ", end="") + print_list(sorted_arr) + print("Sum: ", sum) #mocked_sorted = mock_sort(cons_list) #mocked_sorted_arr = mocked_from_cons_list(mocked_sorted)