Predefining an array of tuples - multiprocessing

I am attempting to populate an array of tuples in a for-loop. The array needs to be predefined.
I am trying to do something along the following lines:
for class in keys(classes)
arr[class]=pmap(y->func(arg,y),1:length(arg1),batch_size=Int(round(length(arg)/nworkers())))
end
In the specific case, classes is a dictionary of type Dict{String,Tuple{Int64,Int64}}. For e.g. classes=Dict("Item1" => (5000,10000), "Item2" => (5000,10000))
The type-definition of broadcasting operation pmap(...) when class is Item1 is an Array{Tuple{Float64,Float64,Float64,Array{Float64,1}},1}. What is an appropriate way of preallocating arr?
arr[Item1] will be of type Array{Tuple{Float64,Float64,Float64,Array{Float64,1}},1}. So, I presume arr would have to defined as an Array{Array{Tuple{Float64,Float64,Float64,Array{Float64,1}},1}}, or something to this extent. But, I couldn't come up with the right notation for defining this.

It seems like I have overthought this; defining arr as a Dict{String,Array{Tuple{Float64,Float64,Float64,Array{Float64,1}},1}} was helpful.

Related

Is it possible to work with more than one array in vuetify v-autocomplete?

Normally one would just make a simple join to merge both arrays in one array, the problem is that i have arrays with different object structures, and depending on the type of object, i need to pass a different value.
Example:
array 1: fruits.type.name
array 2: animals.family.name
Is there any possibility other than having to craft a custom component from scratch using something like v-text-input, for example?
You mean something like this? Check this codesanbox I made:
https://codesandbox.io/s/stack-71429578-switch-autocomplete-array-757lve?file=/src/components/Example.vue
computed: {
autoArray() {
return this.typeAnimal ? this.animals : this.fruits
},
autoTypeId() {
return this.typeAnimal ? 'family.id' : 'type.id'
},
autoText() {
return this.typeAnimal ? 'family.name' : 'type.name'
}
}
With help of a couple computed props you could be able to switch array, item-text and item-value depending of the array you're working with.
As far as I know, there's no easy way to supply two different arrays to v-autocomplete and retain the search functionality.
You could probably join the arrays and write a custom filter property. Then use selection and item slots to change the output of the select based on the structure.
But if your data arrays aren't too complicated, I would avoid the above. Instead, I would loop through both arrays, and build a new combined one with a coherent structure.

What's the difference between map and pluck in RxJS?

I am trying to understand the difference between map and pluck transformational operators in RxJS.
Can anyone help me with this?
The docs say
Pluck : Like map, but meant only for picking one of the nested
properties of every emitted object.
Therefore, let's say you have
[{ name: 'Joe', age: 30, job: { title: 'Developer', language: 'JavaScript' },
{ name: 'Sarah', age: 35 }]
and you want a list of all job titles.
Using map would be kind of a pain (because of the nullability of job), but with 'pluck' you can write pluck('job', 'title') and it will traverse the tree looking for job.title - and won't fail if job is null.
Example taken from : https://www.learnrxjs.io/operators/transformation/pluck.html
https://jsfiddle.net/btroncone/n592m597/
As #mgm87 said, you can perform an operation with map.
On the opposite, pluck is just taking a value.
For example, with map you could do something like that:
this.http.get('...some api url to get a user...')
.map(response => response.json())
.map(user => user.age > 18 ? 'major': 'minor')
.do(isMajorOrMinor => console.log(isMajorOrMinor))
So you can manipulate your data down the chain even conditionally.
BUT, for me one of the big differences is that map is typed.
Which means if you have some data let say:
interface IUser {
name: string;
age: number;
dogs: IDog[];
}
And you receive at some point a user, from which you want to get his dogs:
user$
.map(user => user.dogs)
.do(dogs => ...) // here, you do NOT need to precise (dogs: IDog[]) because Typescript will make a type inference
And that's why I'm always using map even to just "pluck" some data.
Stop using pluck!
Pluck is now planned to be removed in RxJS v8.
Do you know what is the reason?
Because after the addition of the optional chaining operator in JS, it's essentially, just a weak version of Map.
So what's the difference between the two?
Both are used to "transform" the data that is going to be emitted.
Map can be used to... map an observable emission (like we do in JS with Array.prototype.map), while Pluck is used to select/pick a property to emit (without having to emit properties that we don't care for, hence improving the performance).
But even before the optional chaining operator, you could just map the properties instead of plucking them. The result & performance were/are about the same.
pluck('prop')
is just a shorthand for:
map(x => x.prop)
Well, then what was the reason behind the implementation of Pluck?
It was basically implemented to achieve path traversal safety, meaning you could try to pluck a nested property without getting the error (that Map would throw) if the property is not defined:
pluck('foo', 'bar', 'baz'); // no error if foo is not defined
map(x => x.foo.bar.baz) // error if foo is not defined
With optional chaining, this advantage doesn't exists anymore, since we can just do this:
map(x => x?.foo?.bar?.baz)
This is the main reason why the Pluck operator is going to be deprecated and removed in the future.
Another important reason is TS typing for pluck is quite complex and not as robust as the map operator.
Got that info in GitHub:
The commit of Pluck deprecation is almost one year old but I still don't see any warning of future deprecation in the docs, hence I am posting here since I think it's a good thing to know. I already stopped plucking for this reason.
Map can perform an operation on each emitted value.
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-map
https://www.learnrxjs.io/operators/transformation/map.html
// value from observable = 10
map(x => 10*x)
// value from map = 100
Pluck simply picks one of the nested properties of each emitted value.
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-pluck
https://www.learnrxjs.io/operators/transformation/pluck.html
// value from observable = {p = 10, w = 100}
pluck('p')
// value from pluck = 10
They are very similar, but as I understand it, map works with an array whereas pluck takes the values from an object.
This is the place to go for specifics.

data structure for wildcard based strings

I have a list of strings as follows,
`|Country A|City A|Street A| => Foo
|Country A|City B|Street A| => Bar
|Country C|City B|Street B| => Gee
|Country A|*|Street E| => Unkown`
Each country, city and street represent a value like Foo.
Sometimes, the Country|City|State can be a wildcard(*) and then it represents a value Unkown.
Is there a data structure that I could use to represent this input.
As a user, when I enter a country|city|street combo, I expect to get a value. If it is not there, then it returns empty.
I guess I should use some sort of tree strucutre to store this data. But I am not sure how it would be structured.
Why do you want to use a data structure. Try encapsulating this in a class and then creating methods that do your calculations for you. It will make things simpler for you and anyone else who works on your code if you abstract things into separate classes and perform functions/methods objects.
In this example you could create a method that does your comparison and create a class that houses country/city/state.

Algorithm for visiting nodes only once

Let's say I have an array of N elements. I call a recursive function somehow like this: (no specific language here, just pseudocode)
recursive(myArray){
// do something awesome and provide base case etc
// also get mySecondArray based on myArray
for(i=0;i<mySecondArray.length;i++){
recursive(mySecondArray[i];
}
}
As you can see I need to call this function on every element of another array created inside based on some conditions and other functions called on myArray.
The problem I am having is that mySecondArray always has some of the elements that were already in myArray. I do not want to call recursion again on those elements.
Q: What would be the best algorithm approach to solve this?
If you need more info just let me know (I didn't get into details since it gets more complicated)
Thanks
You can have a hashmap/set/dictionary/whatever-you-call-it to look up the elements.
Python solution:
def recursive(myArray, mySet = None):
if mySet is None:
mySet = { myArray }
else:
mySet.add(myArray)
for mySecondArray in myArray:
if mySecondArray not in mySet:
recursive(myArray, mySet)
By the way writing recursive functions like that is a very bad idea in general. You should use a single function and a stack of the arguments if possible.
P.S.: Your code was incomplete by the way but the idea is the same.

Question about a terminology which implements something like this list.Add(new{a=1, b=2})

What is the terminology for the usage of "new" in:
list.Add(new{a=1, b=2})
And what type should I replace the T in List getList if I want to use the list as the returned value? I don't want to replace T with "object" because I want to parse it in Linq query.
Thanks.
Since you did not specify a type: new {1), it's called object initializers with anonymous types. (MSDN Explaining them) The Object Initializer part is where you do { a=1, b=2}.
If you want to be able to reference a type, you will have to create a type and stuff the values in.
list.Add(
new MyType() {
a=1,
b=2
});
If you are just going to be pairing two items look into using the Pair Class. There is also a Triplet Class just in case you might want to store 3 items.

Resources