From 2da0a6a28ba79fbfbbf4daaecdd93c445c21bc53 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 24 Jun 2024 15:07:11 -0400 Subject: [PATCH] Clean up; Add example --- dust-lang/src/abstract_tree/async_block.rs | 37 +++++++++++----------- examples/async_count.ds | 19 +++++++++++ 2 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 examples/async_count.ds diff --git a/dust-lang/src/abstract_tree/async_block.rs b/dust-lang/src/abstract_tree/async_block.rs index d7734aa..61d46e2 100644 --- a/dust-lang/src/abstract_tree/async_block.rs +++ b/dust-lang/src/abstract_tree/async_block.rs @@ -39,29 +39,30 @@ impl AbstractNode for AsyncBlock { } fn evaluate(self, _context: &Context, _: bool) -> Result, RuntimeError> { - let statement_count = self.statements.len(); let final_result = Mutex::new(Ok(None)); + let statement_count = self.statements.len(); + let error_option = self.statements.into_par_iter().enumerate().find_map_any( + |(index, statement)| -> Option { + let result = statement.evaluate(&_context, false); - self.statements - .into_par_iter() - .enumerate() - .find_map_any( - |(index, statement)| -> Option, RuntimeError>> { - let result = statement.evaluate(&_context, false); + if let Err(error) = result { + return Some(error); + } - if result.is_err() { - return Some(result); - } + if index == statement_count - 1 { + // It is safe to unwrap here because only one thread uses the Mutex + *final_result.lock().unwrap() = result; + } - if index == statement_count - 1 { - // It is safe to unwrap here because only one thread uses the Mutex - *final_result.lock().unwrap() = result; - } + None + }, + ); - None - }, - ) - .unwrap_or(final_result.into_inner()?) + if let Some(error) = error_option { + Err(error) + } else { + final_result.into_inner()? + } } fn expected_type(&self, _context: &Context) -> Result, ValidationError> { diff --git a/examples/async_count.ds b/examples/async_count.ds new file mode 100644 index 0000000..b4cada0 --- /dev/null +++ b/examples/async_count.ds @@ -0,0 +1,19 @@ +count_slowly = fn (multiplier: int) { + i = 0 + + while i < 10 { + sleep_time = i * multiplier; + + thread.sleep(sleep_time) + io.write_line(i as str) + + i += 1 + } +} + +async { + count_slowly(50) + count_slowly(100) + count_slowly(200) + count_slowly(250) +}