Replace element in immutable array matching predicate [Answered]
Got a simple issue, I want to replace an element in an
ImmutableArray<T>
which matches a predicate with another item. Is there some handy Linq method which could do this easily or am I just gonna have to write my own?17 Replies
forgive the ignorance, but doesn't that kind of defeat the purpose of an immutable array?
I say "replace an item" when I really mean "return a new immutable array with the item replaced"
I guess you'd just have to create your own
ToImmutableArray()
extension
other than that just use regular LINQ to exclude the item then pass it into a new ImmutableArray.Create<T>(immutable.Where(x => x != y).ToArray())
the extension method would basically be the same deal
oh you want to replace that specific item, not remove it, i can't read
then yeah LINQ doesn't have a replace out of the box,, so you'd have to write that toothinker227#5176
REPL Result: Success
Result: <ReplaceElement>d__2<Person>
Compile: 724.712ms | Execution: 174.702ms | React with ❌ to remove this embed.
yeah that works
implement
ToImmutableArray()
and ReplaceElement
and you're goldenToImmutableArray()
already exists does it? then even less work implemented my own immutable collections if I needed them, first time I'm hearing of an immutable collections namespace and nuget package
thinker227#5176
REPL Result: Success
Result: ImmutableArray<int>
Compile: 470.805ms | Execution: 41.198ms | React with ❌ to remove this embed.
feel like a bit of a now
It's actually just part of the standard library now. I use
ImmutableArray<T>
pretty often since it's really useful (and optimized).ah, I haven't needed one in quite a while, so probably not that much of a clown
Nah, depends a lot on what you're doing
I think I needed one a couple years ago and literally just copy pasted an existing implementation I had so if it was around then I didn't even check
If you're a masochist like me and really like functional patterns then immutability is a necessity
mostly do immutability through exposing
IReadOnly
collections really
but that's only technically immutable
well I guess so are the immutable collections if you try hard enough✅ This post has been marked as answered!