Add isEmpty to Sequence.

Thanks, Michel and Thorbjørn!
This commit is contained in:
Bob Nystrom
2015-06-30 06:52:29 -07:00
parent ed6d7c5084
commit 4a15653c66
4 changed files with 21 additions and 0 deletions

View File

@ -52,6 +52,8 @@ class Sequence {
}
}
isEmpty { iterate(null) ? false : true }
map(transformation) { new MapSequence(this, transformation) }
where(predicate) { new WhereSequence(this, predicate) }

View File

@ -61,6 +61,13 @@ Iterates over the sequence, passing each element to the given `function`.
:::dart
["one", "two", "three"].each {|word| IO.print(word) }
### **isEmpty**
Returns whether the sequence contains any elements.
This can be more efficient that `count == 0` because this does not iterate over
the entire sequence.
### **join**(sep)
Returns a string representation of the sequence. The string representations of

View File

@ -66,6 +66,8 @@ static const char* coreLibSource =
" }\n"
" }\n"
"\n"
" isEmpty { iterate(null) ? false : true }\n"
"\n"
" map(transformation) { new MapSequence(this, transformation) }\n"
"\n"
" where(predicate) { new WhereSequence(this, predicate) }\n"

View File

@ -0,0 +1,10 @@
IO.print([].isEmpty) // expect: true
IO.print([1].isEmpty) // expect: false
class InfiniteSequence is Sequence {
iterate(iterator) { true }
iteratorValue(iterator) { iterator }
}
// Should not try to iterate the whole sequence.
IO.print((new InfiniteSequence).isEmpty) // expect: false