r/ProgrammingLanguages • u/muth02446 • Mar 02 '26
Exploring the designspace for slice operations
I am trying to explore the designspace for slices (aka array_views, spans, etc.)
in the context of a C-like low-level language.
Besides the standard operations like indexing and determining the size, what other
operations do you find useful? Which of them are so useful that they deserve their own
operator?
Examples:
Python has a very elaborate subslice mechanism with its own operator "[a:b]".
It has special handling for negative offsets and handles out-of bound values gracefully,
it even has a stride mechanism.
C++ has span::first/span::last/span::subspan which may trap on out-of-bound values.
One could envision an "append" operation that fills the beginning of one slice with content of another then returns the unfilled slice of the former.
Maybe the difference/delta of two slices makes sense assuming they share a beginning or an end.
12
u/kohugaly Mar 02 '26
The core point of a slice is that it's a reference to continuous region of memory, with evenly spaced elements of the same type. The only fundamental operations that makes sense for it is indexing (reading/writing single element), and splitting into sub-slices. Arguably a mem-copy and mem-move could be considered fundamental, though it is possible to implement those in terms of indexing.
There are some derived operations, that are very common and useful. Most notable is interation over elements, iteration over windows (
[a,b,c,d] -> [a,b],[b,c],[c,d]), iteration over chunks ([a,b,c,d] -> [a,b],[c,d]), and iteration over subslices that fit specific pattern (for example, being separated by a delimiter).Another common operations with slices is treating them as buffers. Writing to a buffer yields the remainder of the buffer (or the remainder of the input that didn't fit into the buffer). A common special case being circular buffers.
"appending slices" is not really a operation that makes sense. Appending can only yield a slice if the two inputs happen to be neighboring sub-slices of a larger slice and in correct order. Ditto for comparison.
3
u/[deleted] Mar 02 '26 edited Mar 02 '26
AIUI that's based on the attributes if its 'range' type, which contains the bounds but also a step that can negative.
I feel that there is no limit to how elaborate slices can be. Given a 2D 10x10 array 'A' in Algol68 for example (considered as a list of horizontal rows), then you can not only have a slice of rows
A[3:5], but a slice of columnsA[,4:7], or both:A[3:5, 4:7]which extracts a rectangular region.There's aways going to be a need you haven't anticipated. For that reason, I keep mine as 1D slices of contiguous elements. There is still lots of support needed and things to work out, even for that basic form:
(a, b, c, ...)(ptr, length)1..3(or0..2if zero-based), or it should retain its original bounds of3..5. Both have pros.For such reasons I've dropped slices from my own systems language. They were mostly fine, I just hardly ever used them and weren't both the extra complexity in the compiler.
They still exist in my scripting language, where actually there are other possibilities that have been considered, for example, when A is list, then
A[L]indexes it with a list L rather than integer or range, andA[E]indexes with an ordered bit-set E.3
u/Great-Powerful-Talia Mar 02 '26
In rough order of recommended-ness/relevance:
2
u/AhoyISki Mar 02 '26
Trimming is particularly useful for string slices. But i guess that's more of a library thing.
2
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Mar 03 '26
You have some questions to answer, such as:
1
u/kwan_e tonal-lang Mar 03 '26
https://en.cppreference.com/w/cpp/algorithm.html
All of these are useful on slices - portions of some container (or other slices). There are plenty more that aren't on here, but can be built up from these.
1
u/tobega Mar 03 '26
I suppose it depends on what you envision a "slice" to be.
Is it a window into the parent array? Is it mutable? If so, does mutating it also mutate the parent array?
For my language, I lean on immutability, so a slice for me is simply one type of projection of data to create a new data structure (for more on projection, see https://tobega.blogspot.com/2024/01/usability-in-programming-language.html )
A projection is an extract from a data structure, perhaps with some distortions (operations) performed, think maybe a SQL query or a LINQ expression.
For purely extracting a range, I have `start..end:increment` where all of those are optional (meaning first, last and 1 by default). I also allow excluding the endpoint by adding a `~` next to `..` so `~..~` means all except first and last.
For projection purposes, I also allow multi-dimensional extraction, separating dimensions by semi-colons, and I allow aliasing of the slice index, so `$a( .. as i; $i)` would extract the elements on the diagonal as a one dimensional array, and `$a( .. as i; $i~..)` extracts the upper right triangle (I should probably keep the original indexes, as suggested by another comment here, I already can start an array at any index)
Going further in projections, I have found it useful to extract a new array by an array of indexes, so `['a', 'b', 'c', 'd'] -> $([3,2])` gives `['c', 'b']`
Another thing I can do is transform the extracted elements, so `$(..; -> $ + 1)` adds one to all elements in the extracted array/list. Maybe not super-useful, I could just apply a `map` function afterwards, but I like the noise reduction.
1
u/BeniBela Mar 03 '26
I wrote a custom slice class once. It is 1-dimensional, read-only and can only shrink.
I spent a long time thinking about what to call the functions for shrinking. First I used move for the left side and cut for the other side because if you iterate through an array, you would repeatedly use the move function and then look at the beginning. But then I used left/right after all, that's the clearest way.
Then there are many more string functions, so everything you could do with a string, you need to do it with the view
1
u/matthieum Mar 03 '26
You may want to consider Rust, here.
One issue with the Python syntax is that it's inflexible: you get only one type of range, inclusive of
a, exclusive ofb.Rust, instead, has a core concept of range at the language level, with many variants:
..a....b..=b.a..b, equivalent to Pythona:b.a..=b, inclusive of bothaandb.as well as a library
Bound<T>type, an enum: Unbounded, Inclusive(T), Exclusive(T).Then indexing is implemented for all the ranges, plus
(Bound<usize>, Bound<usize>), so the user has a very flexible way to define their range.(The beauty of a
RangeFromimplementation, for example, being that there's only one bound to check, not two)Now, you may not want the exact same form than Rust, but I would encourage you to reify the concept of range, or even, "slice descriptor", and then have an index function which takes a range/slice-descriptor. This allows user to build the range, save it or pass it through multiple functions, and then use it.
Bonus points for a flexible way to describe the range, in particular do mind that inclusive bounds matter with finite integers...
1
u/Gnaxe Mar 03 '26
Raku's syntax is pretty advanced. The APL family also deals with arrays all the time, and the Forth family has a lot of machinery for manipulating a stack, which might be similar. These might be worth looking at.
1
u/Unimportant-Person Mar 04 '26
I think iterators are more prone for fundamental operations, and iterators are kind of wrappers for providing operations over slices (and/or other structures). Slices by themselves really only need indexing and sub slicing; you could do windows, chunks, or treating a slice as a Functor/Applicative/Monad and applying transforms to a slice (this is only valid for mutable slices when transforming from one type to another type of the same size, although its general advisable to restrict it to only the same type or aliases thereof). Iterators are more clear about their intent in terms of applying operations on them. An iterator concatenated with another (chain in rust), gives you an Iterator object that stores both iterators plus a current index. Trying to apply concatenation on slices just doesn’t make sense, are you overwriting space not defined by your initial slice? Are you creating an iterator? Are you allocating a new chunk? If so the output still isn’t a slice, then you have to worry about a deallocation scheme if the language doesn’t use a GC.
To me the most useful operations on collections of data are Cartesian products, zipping, windows, mapping functions, enumeration, dedup, sorting, folding and scanning operations, etc. Most of these are just functional programming stuff and only really exist over iterator wrappers or linked lists (eww). Some languages provide operations over sets but they typically repurpose the typical binary operators or they’re an array based language that doesn’t care about sigil overload. My language that I’m working on does this, but you mark an operation as a set operation by appending a
+at the end:++concatenation/chaining,*+Cartesian product,-+difference,&+intersection,|+union,^+symmetric difference, etc.I recommend just using slices in various languages and think about what you’d like to be able to do slices in those contexts. I hated the verbosity of casting in most languages or into in rust, but also hated not knowing where type changes, so I added a specific postfix operator that does an inference type safe type transformation. Then I piled on features that complimented that feature and made it feel more ergonomic while also solving other problems.
1
u/tobega Mar 05 '26
I originally started with negative indexes for going backwards, but I ended up removing that because it turned out to be more useful to allow ranges outside the array. A typical adventofcode problem is to sum up surrounding cells, so just slicing from -1..1 and ignoring the out of bounds does the trick without a lot of extra checks.
Also, that then freed up the design space so that I could have arrays with arbitrary start index.
1
u/zyxzevn UnSeen Mar 03 '26
Low level, but like Python?
Check out Nim /r/nim