Reputation: 4102
I have the following logic and this.preAppliedList
which is an array of IResFilter
.
Not that the shape of the IResFilter
object matters, but I am gonna paste it in for completeness sake.
So my question is;
Given the output of includesFilter
function in RifElse
function I will either remove or add filter.
That means that first I traverse the array to determine whether the item is already in the array, and then I will traverse the same array to either add or remove an item.
Max length of the array could be 200, but it is very unlikely that a user would apply so many filters of one type.
For the sake of scalability argument, how could I write the same logic in Ramda to improve the performance by traversing the array only once.
P.S. I know I can ensure single array traversal with short-circuiting by using the for loop - but I'd like to know the Ramda way.
export interface IResFilter {
DisplayKeyItem: string;
DisplayKeyValue: string;
KeyItem: string;
Countor: string;
FilterName: string;
selected?: boolean;
}
export const areFiltersSame = (f1: IResFilter, f2: IResFilter) =>
ReqBy(Rpick(['DisplayKeyValue', 'KeyItem']), f1, f2);
updatePreAppliedList(filter: IResFilter): void {
const areFiltersSameCurried = Rcurry(areFiltersSame)(filter);
const includesFilter = Rany(areFiltersSameCurried);
const removeFilter = Rreject(areFiltersSameCurried);
this.preAppliedList = RifElse(
includesFilter,
removeFilter,
Rappend(filter)
)(this.preAppliedList);
}
Upvotes: 0
Views: 89
Reputation: 50797
My standard advice about this is not to focus on performance, especially when you don't expect your numbers to get even near 200 array elements, unless you've profiled the application and found that this code is a hot-spot.
Ramda is very much designed for compositional coding. So, while you might be able to do something here with Ramda's transducers, I would never bother adding even that complexity unless there was a very good reason.
Two minor questions:
What advantage do you find in renaming the Ramda functions with an R
prefix?
Why do you call curry
inside every invocation of your main function rather than once on the outside? That is, why not:
areFiltersSame = curry ( (f1, f2) => ... )
Upvotes: 1