Transform Array in Spark - arrays

val degrees: VertexRDD[Int] = graph.degrees
val ngb=graph.collectNeighbors(EdgeDirection.Out)
val deg2 = degrees.leftOuterJoin(ngb)
Now i want a key/value pair RDD where key is degree and value is neighbor vertex id.
Basically i want to change from (id,Array[1,2,3]) to {(id,1)(id,2)(id,3)}
I have tried nested map and creating a string for the outer map. I am stuck here. If anyone can help. Thanks in advance.

Sorry, got it myself.
Did flatMap(y => y) with identity function. It was so simple.

Related

kotlin add elements in array as sections

Can anyone teach me how to initiate/add items in array as section?
something like - array[["john"],["daniel"],["jane"]]
tried a few like var d = ArrayList<CustomClass>(arrayListOf<CustomClass>())
that does not work.
for example, if i add "david" i want it look like array[["john"],["daniel", "david"],[“keith"]] by finding the index of array contains letter "d"
how can i display them on a custom base adapter / listview? currently using a viewHolder
val viewHolder = ViewHolder(row.name) to display.
Thanks!
If I am right, you're trying to create an ArrayList that contains other ArrayLists. So writing:
var d = ArrayList<CustomClass>(arrayListOf<CustomClass>())
Won't work because the type of the main ArrayList is not String, but ArrayList. So you need to write:
var names = ArrayList<ArrayList<String>>()
names.add(arrayListOf("jane", "john))
Regarding the second question, check out this course that has a section about RecyclerView: https://classroom.udacity.com/courses/ud9012 If you are a beginner, I strongly encourage you to follow the whole tutorial.

Storing values obtained from for each loop Scala

Scala beginner who is trying to store values obtains in a Scala foreach loop but failing miserably.
The basic foreach loop looks like this currently:
order.orderList.foreach((x: OrderRef) => {
val references = x.ref}))
When run this foreach loop will execute twice and return a reference each time. I'm trying to capture the reference value it returns on each run (so two references in either a list or array form so I can access these values later)
I'm really confused about how to go about doing this...
I attempted to retrieve and store the values as an array but when ran, the array list doesn't seem to hold any values.
This was my attempt:
val newArray = Array(order.orderList.foreach((x: OrderRef) => {
val references = x.ref
}))
println(newArray)
Any advice would be much appreciated. If there is a better way to achieve this, please share. Thanks
Use map instead of foreach
order.orderList.map((x: OrderRef) => {x.ref}))
Also val references = x.ref doesn't return anything. It create new local variable and assign value to it.
Agree with answer 1, and I believe the reason is below:
Return of 'foreach' or 'for' should be 'Unit', and 'map' is an with an changed type result like example below:
def map[B](f: (A) ⇒ B): Array[B]
Compare To for and foreach, the prototype should be like this
def foreach(f: (A) ⇒ Unit): Unit
So If you wanna to get an changed data which is maped from your source data, considering more about functions like map, flatMap, and these functions will traverse all datas like for and foreach(except with yield), but with return values.

Ruby on Rails : How to find a matching value in JSON arrays?

irb(main):001:0> d = OurDomains.find(3)
=> #<OurDomains:0x000000056ea410 #id="2", #internal_domains=["1.com", "2.com", "3.com"], #external_domains=["2.com"]>
Using the above response as an example, could someone give me a guide on how to compare values in the internal_domains and external_domains array please? Specially, I am interested in displaying all the values in internal_domains that corresponds to a value in external_domains in my view.html.erb; that is "2.com" in this case
#internal_domains & #external_domains

How can I bind the radius of my Geofire query in AngularFire?

I have a model in angularJS which is bound to firebase $scope.items=$firebase(blah) and I use ng-repeat to iterate through the items.
Every item in firebase has a corresponding geofire location by the key of the item.
How can I update my controller to only include items by a custom radius around the user? I don't want to filter by distance in angular, just ask firebase to only retrieve closer items (say 0.3km around a location). I looked around geoqueries but they have a different purpose and I don't know how to bind them to the model anyway. The user may change the radius and the items list should be updated accordingly, so they need to be bound somehow.
Any suggestion is welcome, but an example would be greatly appreciated as I don't have fluency in this trio of angular/firebase/geofire yet :P
It's difficult to figure out what you need to do without seeing your code. But in general you'll need to query a Firebase ref that contains the Geohash as either the name of the child or the priority.
A good example of such a data structure can be found here: https://publicdata-transit.firebaseio.com/_geofire/i
i
9mgzcy8ewt:lametro:8637: true
9mgzgvu3hf:lametro:11027: true
9mgzuq55cc:lametro:11003: true
9mue7smpb9:nctd:51117: true
...
l
...
lametro:11027
0: 33.737797
1: -118.294708
actransit:1006
actransit:1011
actransit:1012
...
The actual transit verhicles are under the l node. Each of them has an array contains the location of that vehicle as a longitutude and latitude pair.
The i node is an index that maps each vehicle to a Geohash. You can see that the name of each node is built up as <geohash>:<metroarea>:<vehicleid>.
Since the Geohash is at the start of the name, we can filter on Geohash with a Query:
var ref = new Firebase("https://publicdata-transit.firebaseio.com/_geofire");
var query = ref.child('i').startAt(null, '9mgzgvu3ha').endAt(null, '9mgzgvu3hz');
query.once('child_added', function(snapshot) { console.log(snapshot.name()); });
With this query Firebase will give us all nodes whose name falls within the range. If all is well, this will output the name of one node:
9mgzgvu3hf:lametro:11027
Once you have that node, you can parse the name to extract the vehicleid and then lookup the actual location of the vehicle under l.
Calculating Geohashes based on a location and a range
In the snippet above, I hardcoded the geohash values to use. Normally you'll want to to get all nodes in a certain range around a center. Instead of calculating these yourself, I recommend using the geohashQueries function from GeoFire for that:
var whitehouse = [38.8977, -77.0366];
var rangeInKm = 0.3;
var hashes = geohashQueries(center, radiusInKm*1000);
console.log(JSON.stringify(hashes));
This outputs a number of Geohash ranges:
[["dqcjqch","dqcjqc~"],["dqcjr10","dqcjr1h"],["dqcjqbh","dqcjqb~"],["dqcjr00","dqcjr0h"]]
You can pass each of these Geohash ranges into a Firebase query:
hashes.forEach(function(hash) {
var query = geoFireRef.child('i').startAt(null, hash[0]).endAt(null, hash[1]);
query.once('child_added', function(snapshot) { log(snapshot.name()); });
});
I hope this helps you settings things up.
Here is a Fiddle that I created a while ago to experiment with this stuff: http://jsfiddle.net/aF9mN/.

Matlab: Unable to store information into array

Im trying to store values from radialspeed (a function from the phased array toolbox) into an array, but im getting errors:
Conversion to cell from double is not possible.
Error in modelCar (line 40)
Cell(1,T)= Rspeed;
^^Error Message
Cell = cell(1,12)
for T = 1:11
[POS,v] = step(H,T);
Rspeed = radialspeed(POS,v,[25; 25; 70],[0; 0; 0]);
typecast(Rspeed,'uint16');
Cell(1,T)= Rspeed;
%%Rspeed = Vel.Radspeed(:,T);
disp(Rspeed);
end
^^^Excerpt of the code im using.
Another question any tips to plot a graph continuously while in the loop, the draw now function doesn't seem to work
Thank you.
You should not use Cell as a variable since cell is reserved keyword in MATLAB. Though using Cell will pose no problems, but a simple typing mistake can inject errors into your code. You may use myCell, R_cell etc.
By writing, Cell(1,T)= Rspeed you are trying to assign Rspeed of type double to a cell datatype. You should write Cell{1,T}=Rspeed or Cell{T}=Rspeed. You can also visualize your output for each iteration as follows:
Replace disp(Rspeed) by:
hold on;scatter(T,Rspeed,'ro');
pause(0.001);
1st question: Cell(1,T) will return a cell so you need to change your code to Cell{T}= Rspeed;.
2nd question: recall plot is a possible solution if speed is not a main concern.

Resources