Skip to content

Commit

Permalink
better fold()
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Jan 10, 2025
1 parent 1aec830 commit 0cbe5a8
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,33 +234,29 @@ impl<'a, 'b> Builder<'a, 'b> {
code.push('(');
if variadic {
if let Some((last, non_variadic_args)) = function_args.split_last() {
code.push_str(&non_variadic_args.iter().enumerate().fold(
String::new(),
|acc, (i, v)| {
if i == 0 {
v.name.clone()
} else {
format!("{}, {}", acc, v.name.as_str())
}
},
));
code.push_str(
&non_variadic_args.iter().skip(1).fold(
non_variadic_args
.first()
.map(|v| v.name.clone())
.unwrap_or(String::new()),
|acc, v| format!("{}, {}", acc, v.name),
),
);
if !non_variadic_args.is_empty() {
code.push_str(", ");
}
code.push_str((String::from("...") + &last.name).as_str())
}
} else {
code.push_str(
&function_args
.iter()
.enumerate()
.fold(String::new(), |acc, (i, v)| {
if i == 0 {
v.name.clone()
} else {
format!("{}, {}", acc, v.name.as_str())
}
}),
&function_args.iter().skip(1).fold(
function_args
.first()
.map(|v| v.name.clone())
.unwrap_or(String::new()),
|acc, v| format!("{}, {}", acc, v.name),
),
);
}
code.push_str(") {\n");
Expand Down

0 comments on commit 0cbe5a8

Please sign in to comment.