Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created April 29, 2020 00:40
Show Gist options
  • Save bdkosher/14c81a2ac80a7ae29985cd66066b1ba1 to your computer and use it in GitHub Desktop.
Save bdkosher/14c81a2ac80a7ae29985cd66066b1ba1 to your computer and use it in GitHub Desktop.
/*def imewi = Iterable.metaClass.eachWithIndex
def lmewi = List.metaClass.eachWithIndex
println imewi
println lmewi
def iewi = Iterable.&eachWithIndex
def lewi = List.&eachWithIndex
*/
def eachWithLast = { Closure c ->
int size = delegate.size()
delegate.eachWithIndex { item, idx ->
boolean last = idx == size - 1
c(item, last)
}
}
def cewl = { item, last ->
println "$item is ${last ? '' : 'not '}last"
}
List.metaClass.eachWithLast << eachWithLast
[1, 2, 3].eachWithLast cewl
['a','b','c'].eachWithLast cewl
Iterable.metaClass.eachWithLast << eachWithLast
import java.nio.file.*
Path path = FileSystems.getDefault().getPath(/C:\Users\jwolf2\Desktop/);
path.eachWithLast { item, last ->
println "$item is ${last ? '' : 'not '}last"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment