To whom it may concern:
I am having trouble adding elements to my dictionary and then passing it
into a function:
function f(;kw...)
for (k,v) in kw
println(k,",",v)
end
end
d = {:a => 1, :b => "c"} # this works
# d = Dict() # but this line
# d["d"] = 2 # with this fails
f(;d...)
Thanks,
Ed
On Tuesday, October 15, 2013 at 9:46:17 PM UTC-4, Jeff Bezanson wrote:
>
> Although converting a string to a symbol is reasonably efficient, it's too
> much extra work to do for something like a function call where every cycle
> counts. You could easily end up doing this work repeatedly, for each one of
> many calls. It also might lead somebody to think strings are equally valid
> in this context, and use them exclusively, which I would consider a
> performance trap.
> On Aug 7, 2013 3:04 PM, "Westley Hennigh" <[email protected]
> <javascript:>> wrote:
>
>> I think the issue is transparency for the user. If they have a dictionary
>> and get an error when they call (I kind of like Elliot's
>> name) `dictsplat()`, it's pretty obvious what the problem is. If they just
>> use the dictionary of strings and we do the conversion for them it might
>> not be as clear.
>>
>> On Wednesday, August 7, 2013 12:55:42 AM UTC-7, Ivar Nesje wrote:
>>>
>>> What is the problem with having minus or space in a symbol? You'd have
>>> to generate the code using Expr() and symbol("var-with minus&space"), as
>>> the parser uses those characters as delimiters when it parses code. Errors
>>> should be given when you use a dictionary with keys that does not match the
>>> keyword arguments of the function, so there is no reason to give a
>>> different error if some of the characters should be invalid.
>>>
>>> Ivar
>>>
>>> kl. 07:40:50 UTC+2 tirsdag 6. august 2013 skrev Elliot Saba følgende:
>>>>
>>>> One of the problems with this is that strings have quite a bit more
>>>> leeway in what they can contain than symbols. What happens if your
>>>> strings
>>>> have spaces? Or minus symbols? Getting an error from the `symbol()`
>>>> function while trying to splat a dictionary seems like it might be
>>>> confusing.
>>>>
>>>> If doing this conversion automatically wouldn't take too much cpu time,
>>>> then doing it manually won't take too much cpu time either. :) It should
>>>> be pretty straightfoward to write e.g. a `dictsplat()` function that
>>>> converts any string keys to symbols, and throws a meaningful error if they
>>>> are unable to do so.
>>>>
>>>>
>>>> On Mon, Aug 5, 2013 at 6:38 PM, Westley Hennigh <[email protected]>
>>>> wrote:
>>>>
>>>>> Hmm, maybe it is. I'm a partial judge.
>>>>>
>>>>> But just to be clear: I don't want to actually modify the dict or
>>>>> pursue this if it will be considerably slower.
>>>>>
>>>>> I do think it's fairly unambiguous. And if you pulled a dictionary out
>>>>> of some json and wanted to use it as parameters, I think this would seem
>>>>> pretty straightforward / convenient.
>>>>>
>>>>> On Monday, August 5, 2013 6:08:12 PM UTC-7, Jacob Quinn wrote:
>>>>>>
>>>>>> Auto-converting strings to symbols in this case seems pretty
>>>>>> auto-magical (in a bad way). I would just expect the user to call the
>>>>>> symbol(x) call themselves before passing them in.
>>>>>>
>>>>>> -Jacob
>>>>>>
>>>>>>
>>>>>> On Monday, August 5, 2013 8:01:56 PM UTC-5, Westley Hennigh wrote:
>>>>>>>
>>>>>>> Ok, I have -perhaps- a slightly more interesting question based on
>>>>>>> this.
>>>>>>>
>>>>>>> In your above example, suppose you have a dict `d = {"x" => 3, "y"
>>>>>>> => 4}`. Note the strings.
>>>>>>> If you called your function f with said dict, I think (could be
>>>>>>> wrong) that what you're after is pretty unambiguous (to treat the
>>>>>>> strings
>>>>>>> as symbols). What's more, I think that the conversion between strings
>>>>>>> and
>>>>>>> symbols can be pretty cheap.
>>>>>>>
>>>>>>> So maybe we could add support for more efficiently calling functions
>>>>>>> with dictionaries of strings of named args (without generating one
>>>>>>> containing symbols).
>>>>>>>
>>>>>>> I see line 429 in https://github.com/JuliaLang/julia/blob/master/
>>>>>>> src/julia-syntax.scm, I think dictionaries are being expanded
>>>>>>> around like 1670 and then passed as an unsorted list. So maybe this
>>>>>>> wouldn't be more efficient... Not sure, are you more familiar with this
>>>>>>> code?
>>>>>>>
>>>>>>>
>>>>>>> On Monday, August 5, 2013 5:14:02 PM UTC-7, Westley Hennigh wrote:
>>>>>>>>
>>>>>>>> Lol. I apologize for taking your time, thanks!
>>>>>>>>
>>>>>>>> On Monday, August 5, 2013 5:08:45 PM UTC-7, Mike Nolta wrote:
>>>>>>>>>
>>>>>>>>> ```
>>>>>>>>> julia> f(;x=0,y=0) = (x,y)
>>>>>>>>> # methods for generic function f
>>>>>>>>> f() at none:1
>>>>>>>>>
>>>>>>>>> julia> d = {:x => 3, :y => 4}
>>>>>>>>> {:y=>4,:x=>3}
>>>>>>>>>
>>>>>>>>> julia> f(;d...)
>>>>>>>>> (3,4)
>>>>>>>>> ```
>>>>>>>>>
>>>>>>>>> -Mike
>>>>>>>>>
>>>>>>>>> On Mon, Aug 5, 2013 at 8:02 PM, Westley Hennigh
>>>>>>>>> <[email protected]> wrote:
>>>>>>>>> > Heh, ya, but I can always pass a dictionary. It would be sweet
>>>>>>>>> if the names
>>>>>>>>> > could be broken out so that you can read the defaults and we
>>>>>>>>> don't end up
>>>>>>>>> > with big blocks of `get(args, thing, default)` at the top of
>>>>>>>>> functions.
>>>>>>>>> >
>>>>>>>>> > On Monday, August 5, 2013 4:53:55 PM UTC-7, Mike Nolta wrote:
>>>>>>>>> >>
>>>>>>>>> >> ```
>>>>>>>>> >> julia> f(;kw...) = for (k,v) in kw; println(k,",",v); end
>>>>>>>>> >> # methods for generic function f
>>>>>>>>> >> f() at none:1
>>>>>>>>> >>
>>>>>>>>> >> julia> d = {:a => 1, :b => "c"}
>>>>>>>>> >> {:b=>"c",:a=>1}
>>>>>>>>> >>
>>>>>>>>> >> julia> f(;d...)
>>>>>>>>> >> b,c
>>>>>>>>> >> a,1
>>>>>>>>> >> ```
>>>>>>>>> >>
>>>>>>>>> >> -Mike
>>>>>>>>> >>
>>>>>>>>> >> On Mon, Aug 5, 2013 at 7:17 PM, Westley Hennigh
>>>>>>>>> >> <[email protected]> wrote:
>>>>>>>>> >> > I hope I'm being dense and that this already exists.
>>>>>>>>> >> >
>>>>>>>>> >> > I have some pre-named-args julia which -effectively- flattens
>>>>>>>>> a
>>>>>>>>> >> > dictionary
>>>>>>>>> >> > of named things into an ordered tuple of args that are passed
>>>>>>>>> into a
>>>>>>>>> >> > function. I'd really like to just use the dictionary as a set
>>>>>>>>> of named
>>>>>>>>> >> > args.
>>>>>>>>> >> > Is that possible?
>>>>>>>>>
>>>>>>>>
>>>>