Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

make sum(), min(), max() tolerate unstable first/rest #414

Merged
merged 1 commit into from

3 participants

@sgalles

An humble contribution based on a what was recently done for reduce(), but this time for sum(), min() and max().
If this modification was not desirable please just ignore this pull request

@tombentley tombentley commented on the diff
src/ceylon/language/max.ceylon
@@ -17,6 +17,8 @@ shared Absent|Value max<Value,Absent>(Iterable<Value,Absent> values)
return max;
}
else {
- return first;
+ "iterable must be empty"
+ assert (is Absent null);
@tombentley Collaborator

I'm not sure we really need this assert. Absent can only be Null or Nothing for the possibly-empty and non-empty cases. If it's non-empty (Nothing) we never get to the else block: If we get to the else block then Absent must be null Null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gavinking gavinking added this to the 1.1 milestone
@gavinking gavinking self-assigned this
@sgalles

Actually I had shamelessly copied what @gavinking had done here bf20c0c
Indeed, it looks like a defensive assert. Maybe @gavinking could tell us if we must keep it or remove it.

@gavinking
Owner

Well in my code I had an issue where null was not necessarily a subtype of Absent, so to return null I had to do the assert first. Not sure if you have the same problem.

@sgalles
@sgalles

Confirmed, it is the same problem.
We must keep the assert in min() and max()
FTR if I try to remove the assert :
returned expression must be assignable to return type of min: null is not assignable to Absent|Value

@tombentley tombentley merged commit 3935034 into from
@tombentley
Collaborator

Thanks very much!

@sgalles

Thanks for the merge !

@tombentley
Collaborator

No, thank you. And sorry it took so long.

@sgalles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 21, 2014
  1. @sgalles
This page is out of date. Refresh to see the latest.
View
10 src/ceylon/language/max.ceylon
@@ -6,10 +6,10 @@ see (`interface Comparable`,
shared Absent|Value max<Value,Absent>(Iterable<Value,Absent> values)
given Value satisfies Comparable<Value>
given Absent satisfies Null {
- value first=values.first;
- if (exists first) {
+ value it = values.iterator();
+ if (is Value first = it.next()) {
variable value max=first;
- for (val in values.rest) {
+ while (is Value val = it.next()) {
if (val>max) {
max=val;
}
@@ -17,6 +17,8 @@ shared Absent|Value max<Value,Absent>(Iterable<Value,Absent> values)
return max;
}
else {
- return first;
+ "iterable must be empty"
+ assert (is Absent null);
@tombentley Collaborator

I'm not sure we really need this assert. Absent can only be Null or Nothing for the possibly-empty and non-empty cases. If it's non-empty (Nothing) we never get to the else block: If we get to the else block then Absent must be null Null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return null;
}
}
View
10 src/ceylon/language/min.ceylon
@@ -6,10 +6,10 @@ see (`interface Comparable`,
shared Absent|Value min<Value,Absent>(Iterable<Value,Absent> values)
given Value satisfies Comparable<Value>
given Absent satisfies Null {
- value first=values.first;
- if (exists first) {
+ value it = values.iterator();
+ if (is Value first = it.next()) {
variable value min=first;
- for (val in values.rest) {
+ while (is Value val = it.next()) {
if (val<min) {
min=val;
}
@@ -17,6 +17,8 @@ shared Absent|Value min<Value,Absent>(Iterable<Value,Absent> values)
return min;
}
else {
- return first;
+ "iterable must be empty"
+ assert (is Absent null);
+ return null;
}
}
View
6 src/ceylon/language/sum.ceylon
@@ -3,8 +3,10 @@
see (`function product`)
shared Value sum<Value>({Value+} values)
given Value satisfies Summable<Value> {
- variable value sum = values.first;
- for (val in values.rest) {
+ value it = values.iterator();
+ assert (is Value first = it.next());
+ variable value sum = first;
+ while (is Value val = it.next()) {
sum+=val;
}
return sum;
Something went wrong with that request. Please try again.