Can i use function “get” for my variable a and t? I mean get all elements which are in Array/Tuple and in key in my dictionary. I want to know if it’s possible do in one line. Without using any loops. Only put something in blank place below.
get!(d, , ["66"])
or
get(d, ,[*here i want number what we check if is in keys(d)* ])
there are good functions for this: intersect and union
d=Dict("1"=>[1,11], "2"=>[2], "3"=>[3,13,33])
a=["2","3","4","5"]
xx = intersect(a,keys(d)) # ["2","3"]
map(x->d[x],intersect(a,keys(d))) #one liner
#all the elements of the dictionary d that have a key in common with a
is good to note that is not necessary to do Tuple(a), it works with any iterable
I still don’t quite get it. Could you write out the inputs and the expected output? I guess you have written down the inputs, but I still don’t know what the output should be.
Why do you want to get rid of the for-loop? This looks quite readable, and in Julia, there’s nothing wrong with just using a for-loop. You might be coming from Python, where native loops are quite slow, but in Julia, these don’t come at any extra cost compared to broadcasting, for example, so you are free to use whatever feels more natural.
i was reading the julia documentation ,is only one line as requested, but your loop is the most elegant solution in my opinion (and in julia, that will run pretty fast)
I reading about tuples and using it in function like do_sth(tuple…) and i think how write this in one line. If is possible do something not only for one element like get(Dict, element, new_value) but for more than one element like get(Dict, tuple…, new_val)(yep i know this syntax isn’t correct).
I coming from C++
This syntax is very powerful in a lot of cases, but it doesn’t really seem applicable here. The purpose of splatting is just to pass the elements of an iterable to a function as different arguments, but there aren’t any methods defined for get! with more than three arguments, so this will just throw an error.
I’m not 100% sure what is going on here, but I don’t know how representative such a micro-benchmark is, since you’re measuring a lot of different things here. The compiler might even be able to do some fancy stuff for small collections, but I would still recommend lazy generators for most applications, since the compiler doesn’t have to allocate the whole array on the heap.