@vlr/partial v1.0.1
@vlr/partial
Very simple implementation of partial application function.
#Where it is applicable. Sometimes you have to use functions having 2 arguments in a one-argument context. for example, here employeeMatches is used like that
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);
}
#Suggestion is to shorten partial application a bit, just like that:
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.
#Alternatively Can use lodash _.partialRight, which is a bit longer definition, larger package, and yes, has that defect with unsafe typing for no-arguments return function.
7 years ago