简体   繁体   English

通过 function 作为参数应该使用来自同一调用的另一个参数

[英]Passed function as param should use another param from the same call

I'm recreating the filter, map, find functions in js.我正在重新创建过滤器 map,在 js 中查找函数。 And I have a function pipe , that takes the given array and passes it trough an array of functions such as filter, map etc.我有一个 function pipe ,它采用给定的数组并将其传递给一系列函数,如过滤器、map 等。

These are my functions:这些是我的功能:

const filter = (arr, callback) => {
  let newArr = [];
  let j = 0;
  for (let i = 0; i < arr.length; i++) {
    if (callback(arr[i])) {
      newArr[j] = arr[i];
      j++;
    }
  }
  return newArr;
};

const map = (arr, callback) => {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    newArr[i] = callback(arr[i], i);
  }
  return newArr;
};

const pipe = (arr, callbacks) => {
  console.log(arr, "arr");
  console.log(callbacks, "callbacks");
};

I can use filter like so, and the function works fine.我可以像这样使用过滤器,并且 function 工作正常。

const arr = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']

filter(arr, item => item.length > 6) // returns ["exuberant", "destruction", "present"] 

However, when using pipe, I should not pass arr to each function.但是,当使用 pipe 时,我不应该将 arr 传递给每个 function。 Instead filter and map should somehow take arr from pipe.相反,过滤器和 map 应该以某种方式从 pipe 获取 arr。 But since those functions are being called directly in the pipe call, I do not see how can I achieve that functionality.但由于这些函数是在 pipe 调用中直接调用的,所以我不知道如何实现该功能。

const arr = ['spray', 'limit', 'elite', 'exuberant', 'destruction']

pipe(
  arr,
  [
    filter(item => item.length > 6), // returns ["exuberant", "destruction"]
    map((item, index) => ({ id: index, name: item })) // returns [{ id: 0, name: 'exuberant' }, { id: 1, name: 'destruction' }]
  ]
)

// pipe returns [{ id: 0, name: 'exuberant' }, { id: 1, name: 'destruction' }]

Usually filter and map takes 2 params, arr and a callback .通常 filter 和 map 需要 2 个参数, arr和一个callback But in pipe it takes just the callback.但在 pipe 中,它只需要回调。

Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

I see that you are trying to implement a concept of functional programming known as pipe .我看到您正在尝试实现称为pipe的函数式编程概念。

There's a better way to do what you are trying to achieve.有一种更好的方法可以实现您想要实现的目标。

Instead of passing the array to the pipe function, only pass the map and filter functions to the pipe function and have pipe function return another function that takes the input array and returns the result of executing each function from left to right and passing the input array to each function. Instead of passing the array to the pipe function, only pass the map and filter functions to the pipe function and have pipe function return another function that takes the input array and returns the result of executing each function from left to right and passing the input array到每个 function。

Also change map and filter functions to return another function that takes in the input array as an argument and returns a new array after iterating over the array and passing each each element to the callback function.还要更改mapfilter函数以返回另一个 function,该 function 将输入数组作为参数,并在遍历数组并将每个元素传递给回调 ZC1C425268E68385D14AB5074C17ZA 后返回一个新数组。

 const filter = callback => { return (arr) => { let newArr = []; let j = 0; for (let i = 0; i < arr.length; i++) { if (callback(arr[i])) { newArr[j] = arr[i]; j++; } } return newArr; }; }; const map = callback => { return (arr) => { const newArr = []; for (let i = 0; i < arr.length; i++) { newArr[i] = callback(arr[i], i); } return newArr; }; }; const pipe = (...funcs) => { return array => { return funcs.reduce((acc, curr) => curr(acc), array); }; }; const arr = ['spray', 'limit', 'elite', 'exuberant', 'destruction']; const result = pipe( filter(item => item.length > 6), map((item, index) => ({ id: index, name: item })) )(arr); console.log(result);

there is no easy solution for this since you call the function filter and map directly in the pipe creation (simple functions will execute then)没有简单的解决方案,因为您直接在 pipe 创建中调用 function 过滤器和 map(然后将执行简单的功能)

one thing you can do is change your filter and map functions so they return an object with a method.execute that needs to be called to execute the filter or the map您可以做的一件事是更改您的过滤器和 map 函数,以便它们返回一个 object 和一个需要调用的方法。执行过滤器或 map

const filter = (callback) => {
  function execute (arr) = {
    let newArr = [];
    let j = 0;
    for (let i = 0; i < arr.length; i++) {
      if (callback(arr[i])) {
        newArr[j] = arr[i];
        j++;
      }
    }
    return newArr;
  }
  return { // return the object ready to execute
    execute
  }
};

const map = (callback) => {
  function execute (arr) {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
      newArr[i] = callback(arr[i], i);
    }
    return newArr;
  }
  return {
    execute
  }
};

now if you want to use a filter, do现在,如果您想使用过滤器,请执行

const arr = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']

filter(item => item.length > 6).execute(arr) 
// returns ["exuberant", "destruction", "present"] 

and now the pipe function现在是 pipe function

const pipe = (arr, callbacks) => {
  for (let i = 0; i < callbacks.length; i++) {
    callback.execute(arr)
  }
};

now use the pipe as wanted现在根据需要使用 pipe

const arr = ['spray', 'limit', 'elite', 'exuberant', 'destruction']

pipe(
  arr,
  [
    filter(item => item.length > 6),
    map((item, index) => ({ id: index, name: item })) 
  ]
)
// returns [{ id: 0, name: 'exuberant' }, { id: 1, name: 'destruction' }]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM