Skip to content

Instantly share code, notes, and snippets.

@timyates
Last active December 11, 2015 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timyates/4586502 to your computer and use it in GitHub Desktop.
Save timyates/4586502 to your computer and use it in GitHub Desktop.
First 25 squares of all integers with groovy-stream
List results = Stream.from { x } map { x * x++ } using( x:1 ).take( 25 ).collect()
(take 25 (squares-of (integers)))
@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