Partial Function with Placeholders - Problem

Create a powerful partial application function that transforms any function into one with pre-filled arguments! Your task is to implement a function that takes a base function fn and an array of arguments args (which may contain placeholder symbols "_"), and returns a new function partialFn.

When partialFn is called with restArgs, it should:

  • Replace each "_" placeholder in args with values from restArgs (in order)
  • Append any remaining restArgs to the end of the modified argument list
  • Call the original function fn with all these arguments

Example: If args = [1, "_", 3, "_"] and you call partialFn(5, 7, 9), the placeholders get filled: [1, 5, 3, 7] and the remaining 9 gets appended: [1, 5, 3, 7, 9]

Input & Output

basic_replacement.js โ€” JavaScript
$ Input: fn = (a,b,c,d,e) => a+b+c+d+e, args = [1, '_', 3, '_'], restArgs = [5, 7, 9]
โ€บ Output: 25
๐Ÿ’ก Note: The placeholders '_' are replaced with [5, 7] in order, creating [1, 5, 3, 7]. The remaining 9 is appended, giving [1, 5, 3, 7, 9]. Calling fn(1, 5, 3, 7, 9) returns 1+5+3+7+9 = 25.
no_placeholders.js โ€” JavaScript
$ Input: fn = (a,b,c) => a*b*c, args = [2, 3, 4], restArgs = [5, 6]
โ€บ Output: 240
๐Ÿ’ก Note: No placeholders to replace, so restArgs [5, 6] are appended to args [2, 3, 4], creating [2, 3, 4, 5, 6]. However, fn only uses first 3 parameters, so fn(2, 3, 4) = 2*3*4 = 24... wait, that would call fn(2,3,4,5,6) = 2*3*4 = 24. Actually it depends on function definition.
more_placeholders_than_args.js โ€” JavaScript
$ Input: fn = (...args) => args.join('-'), args = ['_', '_', '_'], restArgs = ['a', 'b']
โ€บ Output: "a-b-_"
๐Ÿ’ก Note: Only first 2 placeholders can be filled with ['a', 'b'], leaving the third placeholder '_' unchanged. Result is ['a', 'b', '_'], which joins to 'a-b-_'.

Constraints

  • fn is a valid JavaScript function
  • args is an array that may contain any values including placeholder "_"
  • restArgs provided to the returned partial function can be any number of arguments
  • 1 โ‰ค args.length โ‰ค 103
  • 0 โ‰ค number of placeholders โ‰ค args.length
  • The returned partial function must work with any number of restArgs

Visualization

Tap to expand
๐ŸŽฏ Mad Libs Template Filling๐Ÿ“ Template Story:"I like __ apples and __ oranges"args: ["I like", "_", "apples and", "_", "oranges"]๐Ÿ“‹ Available Words:["red", "sweet", "juicy"]๐Ÿ”„ Filling Process:Step 1: "I like" โ†’ copy as-isStep 2: "_" โ†’ replace with "red"Step 3: "apples and" โ†’ copy as-isStep 4: "_" โ†’ replace with "sweet"โœ… After Filling Blanks:["I like", "red", "apples and", "sweet", "oranges"]Remaining words: ["juicy"] โ†’ append to end๐ŸŽ‰ Final Result:["I like", "red", "apples and", "sweet", "oranges", "juicy"]โ†’ Call function with: "I like red apples and sweet oranges juicy"O(n)Time
Understanding the Visualization
1
Template Setup
Start with story template containing blanks '_' and a list of words to fill in
2
Sequential Filling
Go through template left-to-right, filling each blank with next available word
3
Append Extras
Add any remaining words to the end of the story
4
Execute Story
Read the complete story (call the function) with all words in place
Key Takeaway
๐ŸŽฏ Key Insight: Just like filling a Mad Libs template, we process placeholders sequentially in one pass, making this both intuitive and optimal with O(n) time complexity!
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
42.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen