Hi Peter,

FWIW, Python has some very powerful but idiosyncratic "slicing" idioms. For
> example (if I recall my python correctly),
>
> 0:-1 means from start to end (i.e. the whole array)
> 1:-2 means from the second to second-last element
> 0:-1:2 means every other element from start to end
> -1:0:-1 means the whole array, but reversed!


In fact, -1 in Python refers to the last element, not the end of the list.
So 0:-1 means all elements of the array except the last one, not the  whole
array.

If you like reverse indices, you can check out the kala-common library I
developed.
In this library, I use bitwise negated indices to represent reverse indices.
Here is the documentation:
https://github.com/Glavo/kala-common/blob/main/docs/Tutorial.md#use-reverse-indexes-in-kala-common

In kala-common, you can use the reverse indices like this:

var seq = Seq.of(1, 2, 3);
// Get the last elementvar _ = seq.get(~1); // ===> 3
// Get a slice of Seq except the first elementvar _ = seq.slice(1,
~0); // ===> [2, 3]
// Get a slice of Seq except the last elementvar _ = seq.slice(0, ~1);
// ===> [1, 2]


I'm very proud of this design. Since reverse indexing uses bitwise negation
instead of a minus sign,
~0 makes sense. I think this is better than Python's design.

Glavo

On Mon, Jul 28, 2025 at 6:45 AM Peter Burka <pe...@quux.net> wrote:

> I agree that this feels way too pythonic to be in commons.
>
> FWIW, Python has some very powerful but idiosyncratic "slicing" idioms. For
> example (if I recall my python correctly),
>
> 0:-1 means from start to end (i.e. the whole array)
> 1:-2 means from the second to second-last element
> 0:-1:2 means every other element from start to end
> -1:0:-1 means the whole array, but reversed!
>
> The optional third term is the "step", which would more traditionally be
> called "stride".
>
> For further fun, the slice syntax actually creates slice objects. You can
> eschew the built-in slice type, and implement your own slice object using
> completely arbitrary indices. Want a slice based on the Fibonacci sequence?
> Not a problem!
>
> With a bit of clever stream chaining, these can all be in done in base
> Java, albeit more verbosely.
>
> Peter
>
> On Sun, Jul 27, 2025 at 3:20 PM Gary Gregory <garydgreg...@gmail.com>
> wrote:
>
> > On Sun, Jul 27, 2025 at 12:46 PM Yokesh Chowdary <
> > bollineniyok...@gmail.com>
> > wrote:
> >
> > > Hi Gary,
> > >
> > > Thanks for the quick reply!
> > >
> > > You're absolutely right, *ArrayUtils.subarray()* and
> > > *StringUtils.substring()
> > > *covers basic slicing. However, my proposal aims to enhance these
> methods
> > > by introducing:
> > >
> > > 1) Support for step
> > > ArrayUtils.slice(new int[]{1, 2, 3, 4, 5, 6}, 0, -1, 2); // returns [1,
> > 3,
> > > 5]
> > >
> > > 2) Support for negative indices
> > > StringUtils.slice("abcdef", -4, -1); // returns "cde"
> > >
> > > These enhancements bring Python-like expressiveness to Java, making
> > slicing
> > > more intuitive and concise for developers, especially when dealing with
> > > dynamic ranges or reverse slicing.
> > > Would love to hear thoughts on whether extending existing utils or
> > > introducing a SliceUtils would be preferable.
> > >
> >
> > Hello Yokesh,
> >
> > I think you're going to need to take a Java POV here, not Python, because
> > this:
> >
> > StringUtils.slice("abcdef", -4, -1); // returns "cde"
> >
> > feels nonsensical for this Commons component.
> >
> > In StringUtils, CharSequenceUtils, and ArrayUtils, negative indices are
> > usually (always?) errors.
> >
> > I don't think we should adopt the naming convention ("slice/SliceUtils")
> of
> > another language. Java and Commons Lang on top of it both have their own
> > vocabulary of APIs. In this case, Java uses "substring", and we're
> reusing
> > that term in StringUtils. In ArrayUtils, we derived that term into
> > "subarray".
> >
> > Adopting Python naming would set the expectation that this would maintain
> > the Python-ness behavior over time, and encourage further Python creep,
> > which is not what Commons components are about. They are about providing
> > Java-centric APIs, not "it works like X in language Y, so let's implement
> > that".
> >
> > There are odd bits in Lang to be sure, but that's mostly because the code
> > base has been around for a long time.
> >
> > WRT: ArrayUtils.slice(new int[]{1, 2, 3, 4, 5, 6}, 0, -1, 2); // returns
> > [1, 3,
> > 5]
> >
> > I have no idea how -1 makes sense or what these parameters mean. You'll
> > want to provide actual descriptions of each parameter so we know what
> > you're talking about. Assuming "it works like Python" is not a good idea.
> > Think about the whole class and how a new API fits in. Saying this lives
> in
> > a new class SliceUtils is not good IMO (see above on terminology).
> >
> > HTH,
> > Gary
> >
> >
> > > Regards,
> > > Yokesh
> > >
> > > On Sun, Jul 27, 2025 at 10:02 PM Gary Gregory <garydgreg...@gmail.com>
> > > wrote:
> > >
> > > > Hello Yokesh,
> > > >
> > > > This functionality already exists in ArrayUtils (subarray()) and
> > > > StringUtils (substring()).
> > > >
> > > > Gary
> > > >
> > > > On Sun, Jul 27, 2025, 12:06 Yokesh Chowdary <
> bollineniyok...@gmail.com
> > >
> > > > wrote:
> > > >
> > > > > Hi All,
> > > > >
> > > > > I'd like to propose adding a new utility method slice() to Apache
> > > Commons
> > > > > Lang that brings Python-style slicing capabilities to Java for
> > > > Collections,
> > > > > arrays (primitive and wrapper types), and String.
> > > > > The proposed method would follow the form: *slice(input, start,
> end,
> > > > step)*
> > > > >
> > > > > This would be a highly reusable utility that simplifies what is
> > > currently
> > > > > verbose or error-prone logic in standard Java. It could live in a
> new
> > > > class
> > > > > like *SliceUtils *or be integrated into existing utility classes
> such
> > > > > as *ArrayUtils
> > > > > *and *StringUtils*.
> > > > >
> > > > > *Highlights:*
> > > > > 1) Supports positive and negative indices for start,end and step
> > > > > 2) Works on Collections, Arrays and Strings
> > > > >
> > > > > *Examples:*
> > > > > List<String> names = Arrays.asList("Hello","Hi","Bye");
> > > > > SliceUtils.slice(names, 0,2); // returns ["Hello", "Hi"]
> > > > >
> > > > > String s = "abcdef";
> > > > > SliceUtils.slice(s, 1, 4); // returns "bcd"
> > > > >
> > > > > int[] numbers = {1, 2, 3, 4, 5, 6};
> > > > > SliceUtils.slice(numbers, 0, -1, 2); // returns [1, 3, 5]
> > > > >
> > > > > If the community agrees this is in scope for Commons Lang, I’d be
> > happy
> > > > to
> > > > > implement the feature with full test coverage and documentation.
> > > > >
> > > > > Looking forward to your thoughts.
> > > > >
> > > > > Regards,
> > > > > Yokesh
> > > > > [bollineniyok...@gmail.com]
> > > > >
> > > >
> > >
> >
>

Reply via email to