1 | | See [http://cobra-language.com/forums/viewtopic.php?f=4&t=216 "Streams" discussion] for information about streams. |
| 1 | {{{ |
| 2 | #!python |
| 3 | class Example |
| 4 | |
| 5 | def main is shared |
| 6 | pass |
| 7 | |
| 8 | def bill(customers as Customer*) |
| 9 | for c in customers |
| 10 | c.billIfNeeded |
| 11 | |
| 12 | def foo(ints as int*) |
| 13 | t = for i in ints where i > 0 |
| 14 | print t |
| 15 | |
| 16 | def bar(ints as int*) |
| 17 | t = List<of int>(t) |
| 18 | t.sort |
| 19 | t.reverse |
| 20 | print t |
| 21 | }}} |
| 22 | * The general form of a stream type is "foo*" where "foo" is the element type. |
| 23 | * The "*" suffix can be thought of as "zero or more". |
| 24 | * The type "foo*" can be read as "foo stream". |
| 25 | * Streams can be applied to both primitive and complex types such as "int*" and "Shape*". |
| 26 | * Streams are portable between backends, whereas .NET's IEnumerable<of> and JVM's Iterable<of> are not. |
| 27 | * Streams are two-way compatible with IEnumerable/Iterable: |
| 28 | * Streams can be used whereever a compatible IEnumerable/Iterable is expected. |
| 29 | * An IEnumerable/Iterable can be used whereever a compatible stream type is expected. |
| 30 | * The term "compatible" means the same inner type: int* is compatible with IEnumerable<of int>/Iterable<of int>, but not IEnumerable<of String>/Iterable<of String> |
| 31 | * However, compatibility does not mean that you can use the methods of IEnumerable or Iterable, since these vary between platforms. |
| 32 | * Streams are an abstract type, so you cannot create them directly with a call on the type such as `int*()`. Instead, use a concrete class such as `List<of>` or `yield` results out of a method. |
| 33 | * Streams are defaulted to be empty rather than nil. |
| 34 | * Portability and readability are the main motivations for having streams in the Cobra language. |
| 35 | * See also: [http://cobra-language.com/forums/viewtopic.php?f=4&t=216 "Streams" discussion] |