News for hackers. Stuff that matters.
Re:Why F#? by klysm on Tuesday April 01, 2025 @10:11AM (#43547010)
This isn’t a great example of what linq is good at. There’s no reason to do ToList there, and the ForEach isn’t particularly idiomatic
Re:Why F#? by pjc50 on Tuesday April 01, 2025 @10:19AM (#43547086)
Yeah, I hit the problem that there isn't a null-type equivalent of Select() for Action<T>, nor is there a IEnumerable.ForEach (controversial), so that's a bit of a hack. But I wanted to make it as close to the original example as possible.
Re:Why F#? by bob1029 on Tuesday April 01, 2025 @01:57PM (#43549695)
> There’s no reason to do ToList there

In this case, I would move it to the very end if we are concerned about the underlying data shifting when the collection is actually enumerated.

Forgetting to materialize LINQ results can cause a lot of trouble, oftentimes in ways that happily evade detection while a debugger is attached.

Re:Why F#? by klysm on Tuesday April 01, 2025 @02:37PM (#43550065)
> if we are concerned about the underlying data shifting when the collection is actually enumerated

I’m not sure what you mean by this. You can fulfill the IEnumerable contract without allowing multiple enumerations, but that doesn’t really have to do with the data shifting around. Doing ToList can be an expensive and unnecessary allocation

Re:Why F#? by bob1029 on Tuesday April 01, 2025 @03:43PM (#43550636)
Imagine a live SQL query you are subsequently filtering with LINQ.
Re:Why F#? by klysm on Thursday April 03, 2025 @12:14PM (#43571873)
Then the ToList will force it to be evaluated on the client - not sure that situation applies
Re:Why F#? by DeathArrow on Wednesday April 02, 2025 @08:05AM (#43555783)
Yes, ForEach isn't idiomatic but he could use Select instead.
Re:Why F#? by klysm on Thursday April 03, 2025 @12:15PM (#43571878)
Side effects in a Select is not idiomatic either