Search code examples
functioniteratorjuliaone-liner

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

  • 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)