3 ms·
Rust: let results = foo .into_iter() .filter(|s| s.contains("banned")) .collect::<Vec<&str>>(); C#: var results = foo
by neonsunset 2y ago
Rust:
let results = foo
.into_iter()
.filter(|s| s.contains("banned"))
.collect::<Vec<&str>>();
C#:
var results = foo
.Where(s => s.Contains("banned"))
.ToArray();
Convenient, easy to understand and fast.
- maccard 2y agoI think Rusts terseness shows here - I think C#'s approach is the best. Also, if you don't use `.ToArray()`, you still have an IEnumerable which is very usable.
- School-Cotton 2y agoAlso true in Rust. You don't have to use `collect` at the end and you still get an iterator. let results = foo .into_iter() .filter(|s| s.contains("banned"))
- didntcheck 2y agoAnd I think Kotlin would just be val results = foo.filter { "banned" in it} Though I'm not sure I'm a fan of it eagerly finishing with a List. If you chained several operations you could accidentally be wasting a load of allocations (with the solution being to start with foo.asSequence() instead)
- neonsunset 2y agoOf course. This was just to illustrate the point, whether to snapshot/collect a sequence or not is another matter entirely. It just goes to show that idiomatic and fast* iterator expressions is something that modern general-purpose PLs are ought to have. * I know little about performance characteristics of Kotlin but assume it is subject to behaviors similar to Java as run by OpenJDK/GraalVM. Perhaps similar caveats as with F#?
- maccard 2y agoUnfortunately Kotlin fails very, very hard on the "iteration speed" side of things. The compilation speed is so unbelieveably slow, and it suffers very very much from the "JVM Startup time" problem. If it were an order of magnitude faster to compile I'd consider it.