refer to Array pattern in scala - arrays

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?

Related

Convert an array with n elements to a tuple with n elements in Scala

I have an array of elements (numbers in my case)
var myArray= Array(1, 2, 3, 4, 5, 6)
//myArray: Array[Int] = Array(1, 2, 3, 4, 5, 6)
and I want to obtain a tuple than contain all of them:
var whatIwant= (1,2,3,4,5,6)
//whatIwant: (Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6)
I tried the following code but it doesn't work:
var tuple = myArray(0)
for (e <- myArray)
{
var tuple = tuple :+ e
}
//error: recursive variable tuple needs type
The trivial answer is this:
val whatIwant =
myArray match {
case Array(a, b, c, d, e, f) => (a, b, c, d, e, f)
case _ => (0, 0, 0, 0, 0, 0)
}
If you want to support different numbers of element in myArray then you are in for a world of pain because you will lose all the type information associated with a tuple.
If you are using Spark you should probably use its mechanisms to generate the data you want directly rather than converting to Array first.
The number of elements of a tuple is not infinitely superimposed. In earlier versions, there were only 22 at most. Scala treats tuples with different numbers of elements as different classes. So you can't add elements like a list.
Apart from utilizing metaprogramming techniques such as reflection, tuple objects may only be generated explicitly.

Julia Quick way to initialise an empty array that's the same size as another?

I have an array
array1 = Array{Int,2}(undef, 2, 3)
Is there a way to quickly make a new array that's the same size as the first one? E.g. something like
array2 = Array{Int,2}(undef, size(array1))
current I have to do this which is pretty cumbersome, and even worse for higher dimension arrays
array2 = Array{Int,2}(undef, size(array1)[1], size(array1)[2])
What you're looking for is similar(array1).
You can even change up the array type by passing in a type, e.g.
similar(array1, Float64)
similar(array1, Int64)
Using similar is a great solution. But the reason your original attempt doesn't work is the number 2 in the type parameter signature: Array{Int, 2}. The number 2 specifies that the array must have 2 dimensions. If you remove it you can have exactly as many dimensions as you like:
julia> a = rand(2,4,3,2);
julia> b = Array{Int}(undef, size(a));
julia> size(b)
(2, 4, 3, 2)
This works for other array constructors too:
zeros(size(a))
ones(size(a))
fill(5, size(a))
# etc.

Is there a way I could iterate over elements in Swift while using method names?

let x = [1, 2, 2, 3, 3, 3, 1].reversed()
for element in x.method_name() {
print(element)
}
This returns
Value of type 'ReversedCollection<[Int]>' has no member 'method_name'.
Why? How do I reference the method I have created and have it do the functions I need it to do?
However, if I use the below, the problem seems to disappear. I would just like to pass in an array and do let the function do all, i.e.:
let x = Array([1, 2, 2, 3, 3, 3, 1].reversed())
Just in case you don't fully understand the motivation behind this overload of reversed returning a ReversedCollection instead of an Array, a ReversedCollection is just a "reversed view" of your original array. It is not a reversed copy of the original array. This is to save time and space, like a "lazy" collection. See this post for more details.
This is why you need the Array(...) initialiser to turn the reversed collection back into an array. You are opting out of the laziness.
On the other hand, there is another overload of reversed that returns an Array directly. Normally this overload is not selected because it is defined in a less specific type - Sequence, as opposed to Array. You need to give enough information about the type to use this overload:
let x: [Int] = [1, 2, 2, 3, 3, 3, 1].reversed()

In scala, how can I get the count of elements that never shown in both arrays?

for example, i have array a
Array[Int] = Array(1, 1, 2, 2, 3)
array b
Array[Int] = Array(2, 3, 4, 5)
i'd like to count how many elements that only shown in a or b. in this case, it's (1, 1, 4, 5), so the count is 4.
I tried diff, union, intersect, but I couldn't find a combination of them to get the result I want.
I think you can try something like this one but this is not good approach, still this will do the trick.
a.filterNot(b contains).size + b.filterNot(a contains).size
Same idea as the other answer, but linear time:
a.iterator.filterNot(b.toSet).size + b.iterator.filterNot(a.toSet).size
(.iterator to avoid creating intermediate collections)

How can I select a non-sequential subset elements from an array using Scala and Spark?

In Python, this is how I would do it.
>>> x
array([10, 9, 8, 7, 6, 5, 4, 3, 2])
>>> x[np.array([3, 3, 1, 8])]
array([7, 7, 9, 2])
This doesn't work in the Scala Spark shell:
scala> val indices = Array(3,2,0)
indices: Array[Int] = Array(3, 2, 0)
scala> val A = Array(10,11,12,13,14,15)
A: Array[Int] = Array(10, 11, 12, 13, 14, 15)
scala> A(indices)
<console>:28: error: type mismatch;
found : Array[Int]
required: Int
A(indices)
The foreach method doesn't work either:
scala> indices.foreach(println(_))
3
2
0
scala> indices.foreach(A(_))
<no output>
What I want is the result of B:
scala> val B = Array(A(3),A(2),A(0))
B: Array[Int] = Array(13, 12, 10)
However, I don't want to hard code it like that because I don't know how long indices is or what would be in it.
The most concise way I can think of is to flip your mental model and put indices first:
indices map A
And, I would potentially suggest using lift to return an Option
indices map A.lift
You can use map on indices, which maps each element to a new element based on a mapping lambda. Note that on Array, you get an element at an index with the apply method:
indices.map(index => A.apply(index))
You can leave off apply:
indices.map(index => A(index))
You can also use the underscore syntax:
indices.map(A(_))
When you're in a situation like this, you can even leave off the underscore:
indices.map(A)
And you can use the alternate space syntax:
indices map A
You were trying to use foreach, which returns Unit, and is only used for side effects. For example:
indices.foreach(index => println(A(index)))
indices.map(A).foreach(println)
indices map A foreach println

Resources