Skip to content

Instantly share code, notes, and snippets.

@yellowsnow
Forked from timyates/oneline.groovy
Last active December 11, 2015 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yellowsnow/4587759 to your computer and use it in GitHub Desktop.
Save yellowsnow/4587759 to your computer and use it in GitHub Desktop.
(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
def integers = Stream.from { x++ } using x:1
// Create a stream of squares based on this stream of integers
def squares = Stream.from integers map { it * it }
// Create an Iterator that looks at the first 25 elements of squares
def 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 ]
@yellowsnow
Copy link
Author

be groovier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment