Created
April 30, 2012 09:45
-
-
Save timyates/2556910 to your computer and use it in GitHub Desktop.
groovy-stream post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import groovy.stream.Stream | |
| // Repeat an object indefinitely | |
| Stream s = Stream.from { 1 } | |
| assert s.take( 5 ).collect() == [ 1, 1, 1, 1, 1 ] | |
| // Use an Iterable | |
| Stream s = Stream.from 1..3 | |
| assert s.collect() == [ 1, 2, 3 ] | |
| // Use an iterator | |
| def iter = [ 1, 2, 3 ].iterator() | |
| Stream s = Stream.from iter | |
| assert s.collect() == [ 1, 2, 3 ] | |
| // Use a map of iterables | |
| Stream s = Stream.from x:1..2, y:3..4 | |
| assert s.collect() = [ [x:1,y:3],[x:1,y:4],[x:2,y:3],[x:2,y:4] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Try and limit an endless Stream with a using block | |
| Stream s = Stream.from { 1 } where { idx < 5 ?: STOP } transform { idx++ ; it } using idx:0 | |
| // The length of this is 1 greater than expected as transform is executed | |
| // (and so idx is updated) AFTER the where condition has already passed the | |
| // last element. | |
| assert s.collect().size() == 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| s = Stream.from a:1..2, b:3..4 transform { a + b } | |
| assert s.collect() == [ 4, 5, 5, 6 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Maps are passed as the delegate to where | |
| Stream s = Stream.from a:1..2, b:3..4 where { ( a + b ) % 2 == 0 } | |
| assert s.collect() == [ [a:1,b:3], [a:2,b:4] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def x = 0 | |
| def eternal = [ hasNext:{ true }, next:{ x++ } ] as Iterator | |
| // True whilst it < 5 otherwise, STOP | |
| Stream s = Stream.from eternal where { it < 5 ?: STOP } | |
| assert s.collect() == [ 0, 1, 2, 3, 4 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Stream s = Stream.from 1..5 transform { it + 5 } | |
| assert s.collect() == [ 6, 7, 8, 9, 10 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Starting with idx:0, return elements with an index element before them | |
| Stream s = Stream.from 'a'..'c' transform { [ idx++, it ] } using idx:0 | |
| assert s.collect() == [ [0,'a'], [1,'b'], [2,'c'] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Stream s = Stream.from 1..10 where { it % 2 == 0 } | |
| assert s.collect() == [ 2, 4, 6, 8, 10 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment