https://bfe.dev is like a LeetCode for FrontEnd developers. I’m using it to practice my skills.
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'
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
}
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)
}
}
Ok, passed!
It is much more challenging than BFE.dev#1. implement curry()
Hope it helps. See you next time.
Top comments (0)