I am very fresh with functional programming concept so perhaps what I'm asking is very simple and/or can't be done.
Let us imagine some code (array, order, count are used as formal parameters in Alt but these also exist in namespace for this example - to keep it more readable. Score is defined elsewhere and it works).
So, the code is aimed to process an object. One of the operations is taking n
first or last elements (actually an object became an array at that time - n
is represented by a count variable).
The story is that I have no idea how to inject a function that switches between taking them from the beginning or the end into that lodash chain.
It should replace the .takeRight
function call.
I have checked several potential ways of writing it without any practical results. How could it be done? Are there simple methods to use conditionals in such a situations?
var Alt = (array,order,count) => {
const result = (order) ? _.take(array, count) : _.takeRight(array, count);
return result;
};
const black = _.chain(array)
.groupBy(score)
.values()
.takeRight(count)
.flatten()
.value();
console.log (black);