Clean up; Add example
This commit is contained in:
parent
7dc7f7a351
commit
2da0a6a28b
@ -39,29 +39,30 @@ impl AbstractNode for AsyncBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn evaluate(self, _context: &Context, _: bool) -> Result<Option<Evaluation>, RuntimeError> {
|
fn evaluate(self, _context: &Context, _: bool) -> Result<Option<Evaluation>, RuntimeError> {
|
||||||
let statement_count = self.statements.len();
|
|
||||||
let final_result = Mutex::new(Ok(None));
|
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<RuntimeError> {
|
||||||
|
let result = statement.evaluate(&_context, false);
|
||||||
|
|
||||||
self.statements
|
if let Err(error) = result {
|
||||||
.into_par_iter()
|
return Some(error);
|
||||||
.enumerate()
|
}
|
||||||
.find_map_any(
|
|
||||||
|(index, statement)| -> Option<Result<Option<Evaluation>, RuntimeError>> {
|
|
||||||
let result = statement.evaluate(&_context, false);
|
|
||||||
|
|
||||||
if result.is_err() {
|
if index == statement_count - 1 {
|
||||||
return Some(result);
|
// It is safe to unwrap here because only one thread uses the Mutex
|
||||||
}
|
*final_result.lock().unwrap() = result;
|
||||||
|
}
|
||||||
|
|
||||||
if index == statement_count - 1 {
|
None
|
||||||
// It is safe to unwrap here because only one thread uses the Mutex
|
},
|
||||||
*final_result.lock().unwrap() = result;
|
);
|
||||||
}
|
|
||||||
|
|
||||||
None
|
if let Some(error) = error_option {
|
||||||
},
|
Err(error)
|
||||||
)
|
} else {
|
||||||
.unwrap_or(final_result.into_inner()?)
|
final_result.into_inner()?
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expected_type(&self, _context: &Context) -> Result<Option<Type>, ValidationError> {
|
fn expected_type(&self, _context: &Context) -> Result<Option<Type>, ValidationError> {
|
||||||
|
19
examples/async_count.ds
Normal file
19
examples/async_count.ds
Normal file
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user