I have the following object:
languages:
english: [ 1, 2, 3 ]
german: [ 4, 5, 6 ]
My goal is to get an array of all values of languagesso that the result looks like [ 1, 2, 3, 4, 5, 6 ].
This is what I have tried:
(word for word in value for key, value of languages)
or
(word for word in languages[lang] for lang in Object.keys languages)
Both methods return a two dimensional array the arrays as first dimension and the values as second dimension
Is there a way to get the desired result using a one-liner?
Use the concat() function:
[1, 2, 3].concat [4, 5, 6]
Yes, you can:
[].concat (val for key, val of languages)...
or
Array::concat (val for key, val of languages)...
which are the same.
(val for key, val of languages) here is the array of all languages arrays to concatenate with one another.
... operator is just a shortcut for java-script apply function.
I am not sure why it has to be in one line ... but here you have it in 2 LOC
result = []
result.splice(result.length, 0, languages[key]...) for key of languages
Related
So i have this Array of Hashes
{"id"=>50823, "code"=>"1PLAK", "name"=>"Eselente", "order"=>1}
{"id"=>74327, "code"=>"1MAGP", "name"=>"Mango", "order"=>2}
{"id"=>50366, "code"=>"1ANGC", "name"=>"Tabnie", "order"=>3}
{"id"=>76274, "code"=>"1FABD", "name"=>"Slamtab", "order"=>4}
And i want to select the field order (just one field at the same time) for comparing afterwards.
What's the correct way to do it?
Thanks!
When you only want to extract the values of the orders then I would do this:
array = [
{"id"=>50823, "code"=>"1PLAK", "name"=>"Eselente", "order"=>1},
{"id"=>74327, "code"=>"1MAGP", "name"=>"Mango", "order"=>2},
{"id"=>50366, "code"=>"1ANGC", "name"=>"Tabnie", "order"=>3},
{"id"=>76274, "code"=>"1FABD", "name"=>"Slamtab", "order"=>4},
]
array.map { |hash| hash["order"] }
#=> [1, 2, 3, 4]
When you are only interested in the very first value (1 like you wrote in the comments above) then you can do:
array.first["order"] # or array[0]["order"]
#=> 1
I have two arrays:
a = [a_first_element, a_second_element, a_third_element, a_fourth_element]
b = [b_first_element, b_second_element, b_third_element, b_fourth_element]
I would like to insert in the first array, at even positions, elements of the second array.
So the final array shoud look like :
[a_first_element, b_first_element, a_second_element, b_second_element, a_third_element,b_third_element, etc]
The arrays are made of the same number of items (around 30)
How could I do that ?
It looks like you want to zip the arrays together. Doing this:
a = [1, 2, 3, 4]
b = [111, 222, 333, 444]
c = a.zip(b)
will set c to:
[[1, 111], [2, 222], [3, 333], [4, 444]]
which is almost what you want, but you probably don't want the nested arrays. To get rid of the nested arrays, just call flatten:
c = a.zip(b).flatten()
Now c is set to:
[1, 111, 2, 222, 3, 333, 4, 444]
val Array(direction, value, power, type, zone) = Array(1, 2, 3, 4, 5)
Is there any way to refer Array(1, 2, 3, 4, 5) from some reference that we can use to perform other array operations like iterating array, etc..
i want to use direction, value, power, type, zone as they are more meaningful rather then using arr(0), arr(1), etc.. in addition to doing regular operations on array
You can define your array as follows:
val arr # Array(direction, value, power, t, zone) = Array(1, 2, 3, 4, 5)
This way you can use arr as a normal Array and the other "meaningful" vals.
Note that I changed type by t because the first one is a reserved word of the language.
If you want the object to have meaningful accessors to the values it is containing, I would suggest to simply use a case class:
case class MyDataClass(direction: Int, values: Int, power: Int, type: Int, zone: Int)
val d = MyDataClass(1, 2, 3, 4, 5)
val dir = d.direction
To use it as you would with a traditional array, I would add an implicit conversion to Array[Int]
Store the array as normal, then def the elements as indexes into the array.
val array = Array(1,2,3,4,5)
def direction = array(0)
// etc.
This will still work inside of other methods as Scala allows methods in methods.
Am I missing something here? Why not just do
val arr = Array(1, 2, 3, 4, 5)`
and then subscript the individual elements (arr(0), arr(1), etc.) when you need them?
If I have 2 arrays, for example a = [1, 2, 3, 4, 5] and b = [4, 7, 9, 1]
and I want to use a method to change elements of the first one. For example
a.map {|x| x.to_s}
but I don't want to change elements that are the same as in the array b.
In this case my desire result would be
a = [1, "2", "3", 4, "5"]
1 and 4 are still integers, because the array b has this elements.
So how can I implement this task?
arrays and methods are used just for example to explain what I mean.
you can achieve this with a simple ternary operator inside your map block to either add the respective element as is or as a string depending on whether the second array contains an element with that value:
a.map { |x| (b.include? x) ? x : x.to_s }
a.map!{|e| b.include?(e) ? e : e.to_s}
How to create a key as array in Go for map.
For example in ruby I can implement it such:
quarters = {
[1, 2, 3] => 'First quarter',
[4, 5, 6] => 'Second quarter',
[7, 8 ,9] => 'Third quarter',
[10, 11, 12] => 'Fourh quarter',
}
quarters[[1, 2, 3]]
# => "First quarter"
How the same will be looked in Golang ?
Array types (unlike slices) in Go are comparable, so there is nothing magical in it: you can just define it like any other maps: map[KeyType]ValueType where KeyType will be [3]int and ValueType will be string.
The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice.
m := map[[3]int]string{}
m[[3]int{1, 2, 3}] = "First quarter"
m[[3]int{4, 5, 6}] = "Second quarter"
m[[3]int{7, 8, 9}] = "Third quarter"
m[[3]int{10, 11, 12}] = "Fourth quarter"
fmt.Println(m)
Output:
map[[1 2 3]:First quarter [4 5 6]:Second quarter
[7 8 9]:Third quarter [10 11 12]:Fourth quarter]
Try it on the Go Playground.
To query an element:
fmt.Println(m[[3]int{1, 2, 3}]) // Prints "First quarter"
You can also create the map in one step:
m := map[[3]int]string{
[3]int{1, 2, 3}: "First quarter",
[3]int{4, 5, 6}: "Second quarter",
[3]int{7, 8, 9}: "Third quarter",
[3]int{10, 11, 12}: "Fourth quarter",
}
It should be noted that array in ruby and array in go are different data structures. Go array is not mutable, while ruby array is. Go slice is a lot more similar data structure to ruby array. However, you cannot use slices as keys in go, as pointed out in icza's answer.
So to answer your question:
How the same will be looked in Golang ?
It is impossible, you cannot have a dictionary that maps dynamic arrays to strings in go. However, you could convert slices to arrays and use them as keys, as very well explained by icza.