jOOQ / jOOL

jOOλ - The Missing Parts in Java 8 jOOλ improves the JDK libraries in areas where the Expert Group's focus was elsewhere. It adds tuple support, function support, and a lot of additional functionality around sequential Streams. The JDK 8's main efforts (default methods, lambdas, and the Stream API) were focused around maintaining backwards compatibility and implementing a functional API for parallelism.
http://www.jooq.org/products
Apache License 2.0
2.09k stars 168 forks source link

consider adding mapWithIndex #316

Closed pgrisafi closed 7 years ago

pgrisafi commented 7 years ago

Minor enhancement: I've noted that in many cases having the index while you map is useful, specially when you need to log, or handle errors.

My pattern now is writing something like

    Seq.seq(list)
    .zipWithIndex()
    .map(elementAndIndex -> doSomethingWith(elementAndIndex.v1, elementAndIndex.v2))
    .whatever 

but then, it is kind of annoying. I think it would be better to write

    Seq.seq(list)
    .mapWithIndex(element, index -> doSomethingWith(element, index))
    .whatever 

It is just one line of code, be it enhances readability since not everyone is familiar with zip functions, and you got to use more meaningful variable names since you are not using a tuple.

(maybe there is already a function like that and couldn't find it?)

Thanks again for this little gem!

lukaseder commented 7 years ago

Thank you very much for your suggestion. Just to be sure, you mean for mapWithIndex to take a single BiFunction argument:

mapWithIndex((element, index) -> doSomethingWith(element, index))

Am I right? Because that's already implemented: https://github.com/jOOQ/jOOL/pull/262.

I'm closing this as a duplicate of #262. Feel free to reopen if I misunderstood.

pgrisafi commented 7 years ago

Agg, my bad. Thanks again