diff --git a/src/types/fn_ptr.rs b/src/types/fn_ptr.rs index ca678840..34dcc483 100644 --- a/src/types/fn_ptr.rs +++ b/src/types/fn_ptr.rs @@ -421,9 +421,12 @@ impl FnPtr { ) -> RhaiResult { #[cfg(not(feature = "no_function"))] if let Some(arity) = self.fn_def().map(|f| f.params.len()) { + if arity == N + self.curry().len() { + return self.call_raw(ctx, this_ptr, args); + } if let Some(move_to_args) = move_this_ptr_to_args { if this_ptr.is_some() { - if arity == N + 1 { + if arity == N + 1 + self.curry().len() { let mut args2 = FnArgsVec::with_capacity(args.len() + 1); if move_to_args == 0 { args2.push(this_ptr.as_mut().unwrap().clone()); @@ -434,7 +437,7 @@ impl FnPtr { } return self.call_raw(ctx, None, args2); } - if arity == N + E + 1 { + if arity == N + E + 1 + self.curry().len() { let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len() + 1); if move_to_args == 0 { args2.push(this_ptr.as_mut().unwrap().clone()); @@ -449,10 +452,7 @@ impl FnPtr { } } } - if arity == N { - return self.call_raw(ctx, this_ptr, args); - } - if arity == N + E { + if arity == N + E + self.curry().len() { let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len()); args2.extend(args); args2.extend(extras); diff --git a/tests/arrays.rs b/tests/arrays.rs index f10326dc..e0d5226e 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -303,6 +303,18 @@ fn test_arrays_map_reduce() -> Result<(), Box> { engine.eval::("let x = [1, 2, 3]; x.for_each(|| this += 41); x[0]")?, 42 ); + assert_eq!( + engine.eval::( + " + let x = [1, 2, 3]; + let sum = 0; + let factor = 2; + x.for_each(|| sum += this * factor); + sum + " + )?, + 12 + ); assert_eq!(engine.eval::("([1].map(|x| x + 41))[0]")?, 42); assert_eq!( engine.eval::("let c = 40; let y = 1; [1].map(|x, i| c + x + y + i)[0]")?,