groovy - Getting "calling" list size in list closure -
i need "calling" list size in groovy list closure, e.g.:
def foo = [1,2,3,4,5] def bar = foo.findall { somecondition(it) }.collect { processelement(it, <self>.size()) }
where <self>
list resulting filtering foo
findall
.
of course, 1 can save intermediate result , size, possible without it?
the best can think of is:
def bar = foo.findall { somecondition(it) } .with { list -> list.collect { processelement(it, list.size()) } }
but uses with
instead of intermediate result.
or, use delegate of closure:
def foo = [1,2,3,4,5] def collector = { -> processelement(it, delegate.size()) } (collector.delegate = foo.findall { somecondition(it) }).collect collector
but using delegate intermediate result ;-)
Comments
Post a Comment