I am having two for loops. One nested in another. I want to iterate on a single Object and change a property in it with another value, something like this:
for(i=0;i<items.length;<i++){
obj.changeThisAttribute = "abc";
for(j=0;j<items.anotherobj.length;j++){
items.anotherobj.changeThisAttribute = "dyz";
}
}
return items;
Is there any better way of doing this? I have read about Async.map and think that it will be a good solution however there is no good example of the same. Please suggest a running example or any alternative way of achieving this.
You're not performing anything asynchronous here so there is no point in async.map.
Unless this is very CPU intensive (looks fine! profile, how many objects do you have?) , your code looks fine.
It's readable, straightforward and simple, no need to look for alternative ways.
(I'm assuming your inner loop goes through items[i].anotherobj and not items.anotherobj though)
Related
I'd like to achieve the following without using loops or comprehensions (at least explicitly) as I believe this is more elegant, especially considering the degree of nested attributes I am using.
Say I have an object nested like so: a.b.c.d.e, where e is of type NodeList. NodeList holds a list of Node objects internally. You can use a.b.c.d.e.all() to get this list of all objects.
Node has a member val that I would like to set on all nodes in e.
I want syntax like this to work: a.b.c.d.e.all().val = <val>. Is there a way I can implement the all() method such that this is possible?
Thanks.
I've decided to achieve this by implementing:
a.b.c.d.e.all(lambda x: x.val = <val>) where all applies the lambda to each Node.
I think this solution is quite nice. Not ideal. If you have any other ideas, please let me know. Thanks.
My task is to take two numbers obtained from a quite complex pair of sequelize count queries (of around 25 lines each) and do a simple subtraction.
This happens on an express.js controller, and I'm wondering which is the best way to do it, taking in consideration to make it readable but clever and functional.
Keep in mind that this subtraction result will later be used as condition in a third sequelize query. So, bonus points if the controller stays somewhat clean and performing.
Some ideas as promises, but observables are also welcome:
//OP1
var result = (
await (Table1.count(sequelize_query1)) -
await (Table2.count(sequelize_query2))
);
//OP2
var result = await (
Table1.count(sequelize_query1)
) - (
Table2.count(sequelize_query2)
);
//OP3
var number1 = await Table1.count(sequelize_query1);
var number2 = await Table2.count(sequelize_query2);
var result = number1-number2;
When I hear "clever" about this sort of computation, I cringe. I've been clever like that, but not clever enough, a few times. It doesn't end well.
Please, with this kind of computation, make your code as clear and easy-to-read as you can. Clear logic is by far the best way, and maybe the only way, to know you have it exactly right.
If it were me, I would write a SQL VIEW that yielded the answer. That's because I work in a place where people know SQL, and we can read it.
In your case, writing async functions (your third choice) lets you write clear step-by-step code with await operations.
It's twice as hard to debug code as it is to write it. So don't use all your talent ad cleverness writing code, or you'll never be able to debug it.
Algorithms and member functions are suggested over looping for efficiency when working with containers. However, associative containers (unordered_map) does not work with the erase(remove_if) paradigm, it appears that the common method is to fall back on a loop.
uom is a std::unordered_map
for(auto it = uom.begin() ; it!=uom.end(); ){
if(it->second->toErase()) {
delete it->second; // omit delete if using std::unique_ptr
fpc.erase(it++);
}else{
++it;
}
}
//as per Scott Meyers Effective STL pg45
is this as efficient as possible? It seams like there should be a better way to do this using something like the erase(remove_if) paradigm but that works for unordered_map (I understand that the associative containers cannot be "re-ordered" hence the non-support of the remove_if algorithm). Is this really the best way to erase entries from an unordered_map using a predicate? Any suggestions?
Thank you in advance.
That is as efficient as possible. If you want something more convenient, you could use boost's erase_if template - see here. unordered_map maintains a linked list of nodes in each bucket, so it's cheap to erase them. There's no need of remove-if type "compaction", which suits std::vector's use of contiguous memory.
Is there a existing implementation in C/c++/java to convert first order logic to CNF ?
It might be more efficient to use something like: Boolean Normal Form
For implementation, I recommend doing it yourself for something simple like this. An efficient method has psuedo code + explanation here
I understand using an iterator++ inside Parallel.ForEach is not a good option but right now i'm forced to use a counter inside a Parallel.ForEach loop, counter is used to pick up column names of a dynamic object at runtime.Any suggestion what would be the best option?.I read somewhere at StackOverflow that using "Interlocked" is again a bad design inside Parallel.ForEach.
If you really need parallel processing, the indices will have to be pre-computed. Something like Enumerable.Range(0, cols.Length).ToArray(). Otherwise, each column will depend on the previous one, which obviously doesn't parallelize.