A set of 'functional programming' tools
``
function itemMapper(a: number): string { // return new value … }
// usually is written like this
function arrayMapper(a: number[]): string[] { return a.map(doSmth); }
// can now write it like this
const arrayMapper = mapWith(itemMapper);
`
When the mapper function has several arguments, first parameter is element of the array, the rest of the parameters are the same for each array element.
`
function itemMapper(a: number, b: number): string { // return new value … }
// usually is written like this
function arrayMapper(a: number[], b: number): string[] { return a.map(a1 => itemMapper(a1, b)); }
// alternatively
const arrayMapper = mapWith(itemMapper);
// or just call directly
const result: string[] = mapWith(itemMapper)([1, 2, 3], 4);
`
`
function predicate(a: number, b: number): boolean { // return bool … }
// usually is written like this
function arrayFilter(a: number[], b: number): number[] { return a.filter(a1 => predicate(a1, b)); }
// alternatively
const arrayFilter = filterWith(predicate);
// or just call directly
const result: number[] = filterWith(predicate)([1, 2, 3], 4);
`
`
export function findEmployeeById(employees: Employee[], id: EmployeeId): Employee {
return employees.find(employee => employeeMatches(employee, id));
}
function employeeMatches(employee: Employee, id: EmployeeId): boolean {
return employeeIdEquals(employee.id, id);
}
`
export function findEmployeeById(employees: Employee[], id: EmployeeId): Employee {
// instead of this
// return employees.find(employee => employeeMatches(employee, id));
// write this
return employee.find(partial(employeeMatches, id));
// or this
return employee.find(partial2(employeeMatches, id));
}
``Here "partial2", means that you fix the target function to having 2 parameters exactly. As well as "partial3" is fixed to 3 and "partial4" to 4 correspondingly.
And "partial" has overrides for any amount of arguments in target function up to 4, which is a bit less strict, but it has limitation, you can't predefine all arguments.
I could not make safe typing for that case.