4 ms·
Here's "clippy", Rust's style enforcement system, complaining about an insufficiently functional form. warning: manual implementation of `Option::map`
by Animats 5y ago
Here's "clippy", Rust's style enforcement system, complaining about an insufficiently functional form.
warning: manual implementation of `Option::map`
--> src/viewer/regionindex.rs:265:13
|
265 | / match vlinkopt {
266 | | Some(vlink) => Some((*vlink).clone()),
267 | | None => None
268 | | }
| |_____________^ help: try this: `vlinkopt.map(|vlink| (*vlink).clone())`
|
note: `#[warn(clippy::manual_map)]` on by default
for further information visit
https://rust-lang.github.io/rust-clippy/master/index.html#manual_map
It wants a map and a closure used on a single value, to replace a simple conditional. I am told that the map and closure will all optimize out, but I haven't looked at the generated code.
- nextaccountic 5y agoThe issue here is that None => None is really a code smell, because all you are doing is mapping the internal value of Some(..). Hence, calling .map() actually makes the intent of the code more clear.
- jkelleyrtp 5y agoIn some cases I've found it impossible to use .map() - especially when needing to use "self" and dealing with lifetimes of data borrowed from "self". I really wish Rust had proper disjoint capture for private methods in these instances.
- NobodyNada 5y agoDisjoint capture in closures is coming in Rust 2021 next month: https://doc.rust-lang.org/edition-guide/rust-2021/disjoint-capture-in-closures.html https://doc.rust-lang.org/edition-guide/rust-2021/disjoint-c...
- valzam 5y agoBecoming fluent in using all the different ways to iterate over traditional collections (map,flatMap, fold, reduce etc.) already takes some time. Extending that to things that programmers don't normaly think of when they think "loop over something" is definitely a hurdle. It doesn't help that these operations have very generic names. That being said, once you get used to it it is extremely convenient to just map over any sort of "container" and focus on business logic. The above concept even extends to async programming. You can do things like iterating over a Future and let the runtime handle waiting for the value. Functions returning a Future or a Promise can be composed just like you would iterate over a list. asyncDbOperationGetUser(userId).flatMap(user => asyncDbOperationGetPosts(user.friends)) There are of course different ways of achieving this (async/await) but the power of learning FP concepts is that you can apply the same fundamentals to almost everything that has a defined notion of iteration (--> is a monad).
- dnautics 5y ago> It doesn't help that these operations have very generic names As if "for" and "while" aren't generic names. They're so interchangeable that ziglang has basically replaced C's "for" with "while". And with imperative loops, you have to reconstruct in your mind the state of what you're looping over and remember all possible variables closed over the loop and how they could be in flight during the loop. Map is basically declarative: "this thing is going to happen to each thing in your list"; Reduce is like a for loop, except you explicitly declare what you have to keep track over through the loop. The advantages should be clear.
- valzam 5y agofor and while aren't generic in the context of collections. It's very obvious what for item in itemsList: doSomething(item) means. In any case I think map etc. are perfectly ok but it gets tricky when you talk about Options, Futures, Either etc. since I would argue most people don't immediately see how "looping" (I know it's not really looping) can apply here.
- dnautics 5y agoNot all FPs have options, futures, either (lisp, Julia, erlang, elixir)
- patrick451 5y agoEh. It reminds my of writing vectorized mathematical code in numpy or matlab. Oh it's so much cleaner because you don't have an ugly loop! If all you have is a basic dot product, sure. But a lot of times, a loop is a hell of a lot easier to understand, even if it is slower. Almost universally, I find the map/reduce/filter idioms to be inscrutable, write-only code compared to a good old loop
- dnautics 5y agoHere's some real code from our work system (variable names have been changed): invoice_id |> Audit.get_entries() |> Enum.map(fn entry -> entry.action end) |> Enum.map(fn action -> to_string(action) end) |> Enum.join("\n") Tell me you can write that as an "easier to understand" set of loops. Tell me that this code is "write-only" and that you don't immediately understand what is happening, without even knowing the language it's in.