DEV Community

jser
jser

Posted on • Updated on

BFE.dev coding #2 implement curry() with placeholder support

https://bfe.dev is like a LeetCode for FrontEnd developers. I’m using it to practice my skills.

Alt Text

This article is about the coding problem BFE.dev#2. implement curry() with placeholder support

Different from the BFE.dev#1. implement curry(), placeholder support is required this time.

curriedJoin(1, 2, 3) // '1_2_3'
curriedJoin(_, 2)(1, 3) // '1_2_3'
curriedJoin(_, _, _)(1)(_, 3)(2) // '1_2_3'
Enter fullscreen mode Exit fullscreen mode

General idea is the same, yet when judging whether arguments are enough, we need to filter out placeholders, like this.

const expectedArgLength = func.length
const isArgsEnough = args.length >= expectedArgLength &&
  args.slice(0, expectedArgLength)
    .every(arg => arg !== curry.placeholder)

if (isArgsEnough) {
  return func.apply(this, args)
} else {
  // TODO
}
Enter fullscreen mode Exit fullscreen mode

If arguments are not enough, we need to do as before to store the arguments for next call.

Problem is that Function.prototype.bind just concatenate two argument list. What we need is to place new arguments into the placeholder.

So for this case, we return a new function, which merges the old & new argument list by ourself rather than bind().

if (isArgsEnough) {
  return func.apply(this, args)
} else {
  return function(...newArgs) {
    // we need merge two arg list,  newArgs and args
    const finalArgs = []
    let i = 0
    let j = 0
    while (i < args.length && j < newArgs.length) {
      if (args[i] === curry.placeholder) {
        finalArgs.push(newArgs[j])
        i += 1
        j += 1
      } else {
        finalArgs.push(args[i])
        i += 1
      }
    }

    while (i < args.length) {
      finalArgs.push(args[i])
      i += 1
    }

    while (j < newArgs.length) {
      finalArgs.push(newArgs[j])
      j += 1
    }

    // then just call the curried function again
    return curried(...finalArgs)
  }
}
Enter fullscreen mode Exit fullscreen mode

Ok, passed!

Alt Text

It is much more challenging than BFE.dev#1. implement curry()
Hope it helps. See you next time.

Top comments (0)