returns a CharProgression, and there are equivalents for many built in types, but not for user-defined types.
Could the stdlib define the
step
function for
ClosedRange<T>
, to return a new stdlib type
Progression<T>
.?
s
Sam
02/08/2023, 8:01 AM
Not every comparable can be added and subtracted, so it only really works for numeric types. What would step mean for e.g. a
String
? Unless it could take a lambda, like
generateSequence
...
Sam
02/08/2023, 9:04 AM
Copy code
infix fun ClosedRange<String>.step(step: (String) -> String): Iterable<String> =
generateSequence(start, step).takeWhile { it in this }.asIterable()
for (s in "foo".."foooooo" step { it + "o" }) println(s)
I kind of like it! 😄
❤️ 3
Sam
02/08/2023, 9:04 AM
not entirely sure how sensible it is, though…
n
natpryce
02/08/2023, 12:54 PM
I kind of like it too!
natpryce
02/08/2023, 12:54 PM
But yeah, I get your point. There is no way to express “addable” in Kotlin.