Search⌘ K

Solution Review: Club Entry System

Explore how to implement function composition by combining filter and map methods to process customer data for club entry. Understand the use of reduceRight for right-to-left function application and learn how to filter and map arrays effectively with functional programming in JavaScript.

We'll cover the following...

Solution review #

Node.js
const filter = func => arr => arr.filter(func);
const map = func => arr => arr.map(func);
const funcCompose = (...funcs) => args => funcs.reduceRight((arg, fn) => fn(arg), args);
function test(customers){
const ans = funcCompose(
map(x => x.name),
filter(x => x.age >= 18)
)(customers)
return ans
}
const customers = [ { name: "Hermoine", age: 15 },
{ name: "Ron", age: 18 },
{ name: "Harry", age: 24 },]
console.log(test(customers))

Explanation #

You were given two functions, filter and map (lines 1 & 2).

  • filter applies the function, func, on each element of the array, arr, on which it is called.

  • map applies the function, func, on each element of the array, arr, on which it is called.

On line 4, we define our funcCompose function.

const funcCompose = (...funcs) => args => funcs.reduceRight((arg, fn) => fn(arg), args);

It accepts multiple functions, funcs. We use the spread operator here so that it can store any number of functions passed inside of an array. In our case, we are passing two functions, map and filter.

In function composition, the result of one function is passed ...