'Can someone please explain me this code from ReactJS?
can anyone please explain me this code? I am not able to understand as in what's happening here.
const cartReducer = (state, action) => {
if (action.type === "ADD") {
const updatedTotalAmount =
state.totalAmount + action.item.price * action.item.amount;
const existingCartItemIndex = state.items.findIndex(
(item) => item.id === action.item.id
);
const existingCartItem = state.items[existingCartItemIndex];
let updatedItems;
if (existingCartItem) {
const updatedItem = {
...existingCartItem,
amount: existingCartItem.amount + action.item.amount,
};
updatedItems = [...state.items];
updatedItems[existingCartItemIndex] = updatedItem;
} else {
updatedItems = state.items.concat(action.item);
}
return {
items: updatedItems,
totalAmount: updatedTotalAmount,
};
}
return defaultCartState;
};
Solution 1:[1]
That is a redux reducer. Please read this tutorial to get familiar with the concepts of it: https://redux.js.org/tutorials/fundamentals/part-3-state-actions-reducers
Solution 2:[2]
Reducers were popularized by Redux but are not a concept inherent to Redux in the sense that you can write a reducer without any import from Redux. A reducer is a concept for a particular kind of function i.e.:
a function that receives the current state and an action object, decides how to update the state if necessary, and returns the new state: (state, action) => newState. "Reducer" functions get their name because they're similar to the kind of callback function you pass to the Array.reduce() method.
Source: Redux docs
React now comes with a useReducer hook built-in. See Hooks API Reference.
Solution 3:[3]
An easy to understand way to generate the numbers with an even Hamming weight is to loop over all the numbers, calculate their Hamming weights, and keep only the numbers for which it is even. This way the numbers are also generated in order.
There are other ways, especially if the order is not critical. For example, the formula x ^ (x >> 1) could be used to map even numbers to numbers with even Hamming weight, and similarly the formula x ^ (x << 1) could be used to map a "normal" integer (up to but excluding the numbers with their most significant bit set) to a corresponding integer with an even Hamming weight.
With both of those ways, the numbers are not produced in order, but they are produced with a "nicer" loop (less branchy, more amenable to auto-vectorization). Since there is such a nice correspondence from the index of an element in that list to the value, depending on the application you may be able to skip putting these numbers into a list at all, and instead generate them directly in the place where they are needed.
Solution 4:[4]
The next number is always larger by 1, 2, or 3. We can generate the next number in order by incrementing by 2 and fixing up if the parity is wrong:
t = x + 2
x = t ^ parity(t)
where parity(t) could be calculated in C++ as std::bitset<32>(t).count() % 2. On many modern CPUs this gives quite efficient code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stefan Schmidt |
| Solution 2 | Michael Flores |
| Solution 3 | harold |
| Solution 4 | Falk Hüffner |
