Last active
December 11, 2015 10:28
-
-
Save timyates/4586502 to your computer and use it in GitHub Desktop.
First 25 squares of all integers with groovy-stream
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
List results = Stream.from { x } map { x * x++ } using( x:1 ).take( 25 ).collect() |
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
(take 25 (squares-of (integers))) |
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
@Grab( 'com.bloidonia:groovy-stream:0.5.1' ) | |
import groovy.stream.Stream | |
// Create a lazy unending stream of integers from 1 | |
Stream integers = Stream.from { x++ } using x:1 | |
// Create a stream of squares based on this stream of integers | |
Stream squares = Stream.from integers map { it * it } | |
// Create an Iterator that looks at the first 25 elements of squares | |
Iterator first25 = squares.take( 25 ) | |
// Collect the elements | |
assert first25.collect() == [ 1, 4, 9, 16, 25, | |
36, 49, 64, 81, 100, | |
121, 144, 169, 196, 225, | |
256, 289, 324, 361, 400, | |
441, 484, 529, 576, 625 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment