Groovy web console

subscribe to the feed Subscribe
to this
site
Leveraging the implicit call() method (via #groovywebconsole)
tweet this snippet Tweet
this
script

Leveraging the implicit call() method

Published 5 years ago by glaforge with tags call dsl sequence
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class Sequencer {
    private collection = []
    def seq(list) {
        collection = list
        this 
    }
    def yield(c) { collection.collect c }
    def call(obj) { obj }
}

def sequencer = new Sequencer()
def seq = sequencer.&seq
def yield = sequencer.&yield

seq(1..10) yield { it ** 2 }

// equivalent to
// seq(1..10).call(yield { it ** 2 })