Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make ceylon.language::sum fast #728

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/ceylon/language/sum.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@ see (`function product`)
shared Value sum<Value>({Value+} values)
given Value satisfies Summable<Value> {
value it = values.iterator();
assert (!is Finished first = it.next());
variable value sum = first;
while (!is Finished val = it.next()) {
sum += val;
value first = it.next();
if (is Integer first) {
// unbox; don't infer type Value&Integer
variable Integer sum = first;
while (is Integer val = it.next()) {
Integer unboxed = val;
sum += unboxed;
}
assert (is Value result = sum);
return result;
}
else if (is Float first) {
// unbox; don't infer type Value&Float
variable Float sum = first;
while (is Float val = it.next()) {
Float unboxed = val;
sum += unboxed;
}
assert (is Value result = sum);
return result;
}
else {
assert (!is Finished first);
variable value sum = first;
while (!is Finished val = it.next()) {
sum += val;
}
return sum;
}
return sum;
}