这个问题的标题可能具有误导性。我在理解这个概念时遇到了一些麻烦。我想将所有参数传递给一个函数,但稍后用这些参数调用它。
我目前正在做这样的事情:
function copyRecords(userId, options) {
const getRecordsWrapper = (userId, options) => () => getRecords(userId, options);
abstractionService = new MyAbstractionService(getRecordsWrapper);
...etc
}
Run Code Online (Sandbox Code Playgroud)
这是我保存并稍后调用函数的地方:
class MyAbstractionService {
constructor(getRecords) {
this.getRecords = getRecords;
}
performSomeAction() {
return this.getRecords();
}
}
Run Code Online (Sandbox Code Playgroud)
我需要这样做,因为getRecords在不同的领域采用不同的参数,我想将它们抽象成一个服务,而不必将一堆参数传递到服务中。这会变得混乱,因为我还需要为其他一些功能执行此操作。
我是否正确地考虑了这一点?除了让函数返回另一个返回我的函数的函数之外,还有其他方法可以做到这一点吗?另外,不确定这是否重要但getRecords返回Promise. 我不想开始工作,Promise直到我稍后进入抽象服务。
我试图展平数组的对象数组。例如,我们可能会有类似的内容:
[{ numbers: [1, 2, 3] }, { numbers: [4, 5] }, { numbers: [6] }]
Run Code Online (Sandbox Code Playgroud)
我想将其展平为:
[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
我有一个可行的解决方案,像这样:
[{ numbers: [1, 2, 3] }, { numbers: [4, 5] }, { numbers: [6] }]
Run Code Online (Sandbox Code Playgroud)
有谁知道这里更简单或更高效的解决方案,最好没有almostFlattened中间步骤?
我正在解决HackerRank上的一些问题,我想我会尝试在Python中实现我已经在Java中正确解决的相同解决方案.虽然我的代码几乎完全反映了我之前的Python解决方案,但我if input_str[i-1] == input_str[i]在行中遇到了一个超出范围的异常.Python循环中是否存在可能导致此差异的不同行为?两者的测试用例相同.
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
solve(input);
}
public static void solve(String str) {
String s = new String(str);
for (int i=1; i < s.length(); i++) {
if (s.charAt(i-1) == s.charAt(i)) {
s = s.substring(0, i-1) + s.substring(i+1, s.length());
i = 0;
}
if (s.length() == 0) {
System.out.println("Empty String");
return;
}
}
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud)
这是同一问题的代码,但使用的是Python 2.7.
input_str = raw_input() …Run Code Online (Sandbox Code Playgroud)