EasyJavaScriptTypeScript

Partial Application

Functional ProgrammingPartial ApplicationHigher-Order Functions

Partial application pre-fills some arguments of a function, returning a new function that accepts the rest.

// without partial — repeat the base config everywhere
fetchData('https://api.example.com', 'GET', '/users');
fetchData('https://api.example.com', 'GET', '/posts');

// with partial — lock in the repeated args
const getFromApi = partial(fetchData, 'https://api.example.com', 'GET');
getFromApi('/users');
getFromApi('/posts');

Implement partial(fn, ...presetArgs) that returns a new function pre-filled with presetArgs.

solve(label, ...args) dispatches to predefined partials:

  • 'add5' → partial of (a,b)=>a+b with first arg 5
  • 'multiply3' → partial of (a,b)=>a*b with first arg 3
  • 'greet' → partial of (greeting,name)=>greeting+' '+name with 'Hello'

Sample tests

Test #1partial(add, 5)(10) = 15
Input: ["add5",10]
Output: 15
Test #2partial(multiply, 3)(7) = 21
Input: ["multiply3",7]
Output: 21
Test #3partial(greet, "Hello")("Alice") = "Hello Alice"
Input: ["greet","Alice"]
Output: "Hello Alice"