Range golf challenge: apply predicate to a subrange, returning the original range modified.
FeepingCreature
feepingcreature at gmail.com
Fri Oct 7 14:00:57 UTC 2022
AKA "Do My Job For Me Challenge"
You have a range. You have an expression that's a chain of
`std.algorithm` functions, ie. `filter`, `find`, `until`, `drop`,
etc. that select a subset of the range. Let's abbreviate it with
`.select`.
The subset has the same order of elements as the original range.
The only change is that some elements of the original range will
be missing.
The elements of the range are not mutable, so you cannot mutate
the range via `foreach (ref)`. Instead, you have a predicate,
let's say `.modify`, that takes a value of the range and returns
a new value.
Task: Given a range `range`, return a new range that consists of
the elements of `range`, except modified with the `modify`
predicate at all elements that are part of the subrange selected
by `.select`.
While `@nogc`.
Note: You are allowed to redefine the types of range elements, as
long as `select` can see the original value, for instance with
`value`.
Sketch of a solution: I've been thinking something like
"`enumerate` the range, apply selector, "left join zip" (zip
where the enumeration matches, to be written) with the original
range, apply predicate where left join zip has a match. I'm
thinking a syntax like
`range.selectSubrange!select.modifySubrange!modify`.
Thoughts?
More information about the Digitalmars-d
mailing list