I have data represented in CF as an array of structs e.g.:
var foo = [{key = 'bar', value = 'baz', ... }...];
This structure gets iterated over sequentially and then translated to another related struct which looks like:
foo2[key] = {key = 'bar', value = 'baz', ...};
This is then sent to the SerializeJSON() method and sent to the browser. The problem is that the order of the keys in either foo or foo2 are alphabetical instead of in the order they were added. This is causing a problem on the client side as this collection is iterated over again and is expected to be ordered. Any suggestions?
If your collection is expected to be ordered you need to use an array.
Structs don't guarantee any ordering, and shouldn't be used as such.
Related
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.
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.
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.
I have a list of users each of which contains a list of associated storefront IDs. I have a separate list of integers and I want to find where any storefront id of a user matches any of the integers in the separate list.
I'm expecting something like this:
clientUsers = clientUsers.Where(x => x.Storefronts.Intersect(allowedStorefrontIds));
I'm told the type arguments can't be inferred from the usage on the Where extension method.
Do you know how I should structure my linq in this case?
You just need a .Any() in the lambda to check if the set-intersection contains any elements:
x => x.Storefronts.Intersect(allowedStorefrontIds).Any()
Personally, I would do something like this for efficiency:
var allowedIds = new HashSet<int>(allowedStorefrontIds);
var allowedUsers = clientUsers.Where(x => x.StoreFronts.Any(allowedIds.Contains));
Where expects a function that returns a boolean expression. Intersect returns a list. I think clientUsers.Intersect(allowedStorefrontIds) should return the list you're expecting, unless there is another list not mentioned in the code snippet.
[I've read the Lua manual, but it did not provide solid answers.]
Let's say I have a Lua Table, acting as an indexed array:
local myArray = {};
myArray[1] = "Foo";
myArray[2] = "Bar";
How do I best dispose of this Table? Do I just set myArray to nil? Or do I have to iterate through array and set each indexed element to nil?
Similarly, let's say I have I have a Lua Table, acting as a dictionary:
local myDictionary = {};
myDictionary["key1"] = "Foo";
myDictionary["key2"] = "Bar";
Can I just set 'myDictionary' to nil, or do I have to iterate through?
Lastly, what do I do, memory-management wise, where I have nested Tables? e.g.
local myNestedCollection = {};
myNestedCollection[1] = {1, 2, 3};
myNestedCollection[2] = {4, 5, 6};
Do I need to iterate through each of these sub-tables, setting them to nil? Thanks for any help.
It should be sufficient just to set the local variable to nil; there's no need to iterate through all of the keys and set them to nil to. According to this page, Lua uses a mark-and-sweep garbage collection algorithm. As soon as you set your local variable to nil, the keys in its table become unreachable, so they will be collected by the garbage collector on the next collection. Similarly, if those objects are also tables, their keys will also become unreachable, so they too will be collected.
In most GC an object will be collected when there are no references to it. Setting the top of your reference chain to nil removes a reference to the children. If that was the only reference then the children will be collected.