Range golf challenge: apply predicate to a subrange, returning the original range modified.
FeepingCreature
feepingcreature at gmail.com
Sat Oct 8 17:08:17 UTC 2022
On Friday, 7 October 2022 at 20:19:07 UTC, Ali Çehreli wrote:
> On 10/7/22 12:19, FeepingCreature wrote:
> > On Friday, 7 October 2022 at 16:49:33 UTC, rassoc wrote:
> >> On 10/7/22 16:00, FeepingCreature via Digitalmars-d wrote:
> >>> Thoughts?
> >>
> >> How about your selector func wraps the elements, similar to
> Nullable
> >> or via SumType, and your modifier func operates on the
> marked ones
> >> during iteration while returning the others as is? Poor
> man's monads?
> >
> > Y'all are missing the point a bit. :)
>
> It's because of your example! :)
>
> > The type spec of the selector is "takes a range, returns a
> subrange." A
> > subrange here being "a range like the original but with some
> amount of
> > elements missing."
>
> Your example does not miss the "missing" elements (the odd
> ones):
>
Right: the *selector* here is `filter!isEven`, which produces a
range of `0, 2, 4, 6, 8` or variations thereof. The point is to
get a range where every element that `filter!isEven` selected has
been modified by `square`, while *keeping* the unselected
elements in the same order.
And that's fairly straightforward for `filter!isEven` because
`isEven` is a predicate. But, for instance, `find` or `until`
aren't predicates. So in a way, the question is "how do you
predicatize an arbitrary subrange selection expression."
> assert(result.equal([0, 1, 4, 3, 8, 5, 12, 7, 16, 9]));
>
> Maybe you mean selectSubrange is already written and does miss
> some elements and the goal is to merge those back into where
> they were missing from.
>
> Ali
Yes, exactly.
More information about the Digitalmars-d
mailing list