'How to rewrite this function in one line?
I have the following lines of code:
f((k,v)) = Symbol(k) => Symbol(v)
Dict(Iterators.map(f, pairs(names)))
And I want to write it in a single line. I tried this:
Dict(Iterators.map((k,v) -> Symbol(k) => Symbol(v), pairs(names)))
But it throws Method Error:
MethodError: no method matching (::var"#13#14")(::Pair{Symbol, String})
Is it possible to write this in a single line?
Solution 1:[1]
You want this:
Dict(Iterators.map(((k,v),) -> Symbol(k) => Symbol(v), pairs(names)))
(note the comma after (k,v) which forces destruction of the first argument to the anonymous function into two eleements)
Solution 2:[2]
What about
Dict(Symbol(k)=>Symbol(v) for (k, v) in pairs(names))
?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bogumił Kamiński |
| Solution 2 | DNF |
