Home > Back-end >  How to rewrite this function in one line?
How to rewrite this function in one line?

Time:05-10

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?

CodePudding user response:

What about

Dict(Symbol(k)=>Symbol(v) for (k, v) in pairs(names))

?

CodePudding user response:

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)

  • Related