❔ LINQ build an array with an arbitrary value at the start
So, I want to make a palette that's an array of
int
s where index 0 is equal to 0 and the rest repeat a rainbow pattern.
Question: How could I get this same result with LINQ?23 Replies
Working with HSV colors would be the easiest, since you'd only need to increment the hue
The colors aren't the point, it's a LINQ question
Well, assuming HSV...
That isn't the same result. That would return an
IEnumerable<HSV>
. The question is how to return an int[]
Also, that doesn't include the 0
value at the start.It would return
Hsv[]
strictly speaking
And I thought the colors weren't the point?The 0 value at the start is the point
Uh,
Enumerable.Range(0, someMaxValue)
will give you integers from 0
to some max value...?See, my for loop starts with 1. It leaves index zero blank, then it repeats the rainbow pattern from 1 to 255,
So start at 1 instead of 0?
If so,
Enumerable.Range(1, max)
Ah, wait, then 1
would be at index 0
and you don't want thatI guess I could check it like
Enumerable.Range(0,255).Select(i => i == 0 ? 0 : Rainbow[(i - 1) % Rainbow.Count])
but it seems like there should be some kind of way to do something like .Add(0).Add(Enumerable.Range(1, 255).Select(i => Rainbow[i % Rainbow.Count])
and then it could concatonate the zero at the start before getting into the range ... maybe?Generate a list of colors, then
list.Insert(0, 0)
Will insert 0
at the start of the list and shift everything else upOh, OK, that makes sense.
So
Something like this
Insert doesn't return the list
that except 254, because otherwise you end up with a list that's 257 items long
Ah, right
Honestly anything i think of makes me fall back to "this would be better if it's just a loop"
I went with
actually I went with LINQ because I didn't want it to be a method
just wanted it to be a data member if that makes sense
Bad thinking, stop doing that
That works. I agree, better as a loop. And also we could find a thousand ways to do it, increasingly complex 😛
Like if you took the Math.Sign of i and multiplied it by the value, no conditional
You can populate your data member via a method. You want it to be a method because it makes it much easier to change it later, if you want the colors to be different or come in from somewhere else, and because realistically all of these are just really confusing looking
I would just
Make it readonly or whatever if you need
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.