简体   繁体   English

什么都不做的数组方法

[英]Array method that does nothing

I like functional programming, it keeps my code, especially scopes, cleaner.我喜欢函数式编程,它让我的代码,尤其是作用域更干净。

I found myself having a pretty heavy Array manipulation in my code, like this:我发现自己在我的代码中进行了非常繁重的数组操作,如下所示:

this.myArray = someArray
    .slice(0, n)
    .map(someFunction)
    // more manipulation;

if (condition) {
    this.myArray = this.myArray.reverse();
}

this.myArray = this.myArray
    .reduce(anotherFunction, [])
    // even more manipulation

Is there some built-in way to join the if to my functional chain?是否有一些内置方法可以将if加入我的功能链? Something like:就像是:

this.myArray = someArray
    .slice(0, n)
    .map(someFunction)
    // ... more manipulation
    [condition ? 'reverse' : 'void']()
    .reduce(anotherFunction, [])
    // ... even more manipulation

The void() method doesn't exist. void()方法不存在。 Is there an alternative?有替代方案吗? Is it popular approach to merge multiple calls to a single chain, even if that means calling methods that do nothing?将多个调用合并到一个链中是否流行的方法,即使这意味着调用什么都不做的方法?

I know I can add my own method to Array:我知道我可以将自己的方法添加到 Array:

Array.prototype.void = function () {
    return this;
}

But that's not the point.但这不是重点。 Is there any standard/built-in way to achieve the same effect?是否有任何标准/内置方式可以达到相同的效果?

As neutral function, you could take作为中性 function,您可以采取

Is it popular approach to merge multiple calls to a single chain, even if that means calling methods that do nothing?将多个调用合并到一个链中是否流行的方法,即使这意味着调用什么都不做的方法?

No. The usual approach is to split the chain, similar to how you wrote it in your first snippet, when there is an optional step.不,通常的方法是拆分链,类似于您在第一个片段中编写它的方式,当有一个可选步骤时。 You wouldn't however repeatedly assign to this.myArray , you would use constant temporary variables:但是,您不会重复分配给this.myArray ,而是使用常量临时变量:

const array1 = someArray.slice(0, n).map(someFunction) // more manipulation;
const array2 = condition ? array1.reverse() : array1;
this.myArray = array2.reduce(anotherFunction, []) // even more manipulation

That said, in functional programming that uses functions, not methods, you sometimes do find the approach of having a configurable chain.也就是说,在使用函数而不是方法的函数式编程中,您有时确实会找到具有可配置链的方法。 They don't need a void method on the object, they just use the identity function.他们不需要 object 上的void方法,他们只需使用identity function。

Example in Haskell: Haskell 中的示例:

let maybeReverse = if condition then reverse else identity
let myArray = fold anotherFunction [] $ maybeReverse $ map someFunction $ take n someArray

Example in JavaScript (where you don't have as many useful builtins and need to write them yourself): JavaScript 中的示例(您没有那么多有用的内置函数,需要自己编写):

const fold = (fn, acc, arr) => arr.reduce(fn, acc);
const reverse = arr => arr.reverse(); // add .slice() to make pure
const identity = x => x;
const map = (fn, arr) => arr.map(fn);
const take = (n, arr) => arr.slice(0, n);

const maybeReverse = condition ? reverse : identity;
const myArray = fold(anotherFunction, [], maybeReverse(map(someFunction, take(n, someArray)))));

Btw, in your particular example I wouldn't use reverse at all, but rather conditionally switch between reduce and reduceRight :-)顺便说一句,在您的特定示例中,我根本不会使用reverse ,而是有条件地在reducereduceRight之间切换 :-)

Another option is to switch reduce() with reduceRight() which wouldn't add an extra step at all for the case shown另一种选择是将reduce()reduceRight()切换,这对于显示的情况根本不会添加额外的步骤

this.myArray = someArray
    .slice(0, n)
    .map(someFunction)   
    [condition ? 'reduce' : 'reduceRight'](anotherFunction, [])

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

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