ReduceRight with Reduce

时间:2018-03-30 21:28:46

标签: javascript ecmascript-6

我正在尝试实施reduceRight。

我想知道我是否可以使用我的减少内部减少权利。我做了大部分工作,但我在某些测试用例中遇到错误。

我的代码:

MyReduce:

#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolygon.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <iostream>

using namespace std;

int main (int argc, char **argv)
{
    struct P { float x, y, z; };
    vector<P> p;
    p.push_back (P {8.0, 3.0, 0.0});
    p.push_back (P {7.0, 5.0, 0.0});
    p.push_back (P {1.0, 3.0, 0.0});
    p.push_back (P {1.0, 2.0, 0.0});
    p.push_back (P {0.0, 3.0, 0.0});
    p.push_back (P {0.0, 1.0, 0.0});
    p.push_back (P {1.0, 1.0, 0.0});
    p.push_back (P {1.0, 0.0, 0.0});

    vtkSmartPointer<vtkPolygon> poly = vtkSmartPointer<vtkPolygon>::New();
    poly->GetPointIds()->SetNumberOfIds(p.size ());

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkCellArray> polygons = vtkSmartPointer<vtkCellArray>::New();

    for (size_t i = 0; i < p.size (); ++i)
    {
        points->InsertNextPoint (p[i].x, p[i].y, p[i].z);
        poly->GetPointIds()->SetId(i, i);
    }

    polygons->InsertNextCell(poly);
    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();

    polydata->SetPoints(points);
    polydata->SetPolys(polygons);

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(polydata);

    vtkSmartPointer<vtkActor> buildingActor = vtkSmartPointer<vtkActor>::New();
    buildingActor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(buildingActor);

    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindow->Render();
    renderWindowInteractor->Start ();

    return 0;
}

我的减少权利(使用myReduce)

Array.prototype.myReduce = function(cb, initialVal) {
    if(!cb)
        throw new Error("No CB defined");

    var accumulator = (initialVal === undefined) ? undefined : initialVal;

    for(var i= 0; i<this.length; i++){
        if(accumulator !== undefined) {
            accumulator = cb.call(undefined, accumulator, this[i], i, this)
        } else {
            accumulator = this[i];
        }
    }

    return accumulator;
}

当我运行方法时:

Array.prototype.myReduceRight2 = function(cb, initialVal) {
    if(!cb)
        throw new Error("No CB defined");

    this.reverse();

        const res = this.myReduce((acc,val) => {
        return acc.concat(val);
    });

    return res;
}

对于测试用例:

const reduceRight2 = test2.myReduceRight2((acc, val) => {
    return acc.concat(val);
}, []);

Test2通过,但test1失败:(

有人可以启发我出错的地方吗?

1 个答案:

答案 0 :(得分:1)

myReduceRight2中,您未将initialVal传递给myReduce方法。

另外,reduce和reduceRight不会更改原始数组,因此在应用更改之前应始终克隆原始数组(例如,反向)。

Array.prototype.myReduce = function(cb, initialVal) {
  if (!cb)
    throw new Error("No CB defined");

  let [accumulator, ...arr] = initialVal === undefined ? [...this] : [initialVal, ...this];

  for (var i = 0; i < arr.length; i++) {
    accumulator = cb.call(undefined, accumulator, arr[i], i, arr);
  }

  return accumulator;
}

Array.prototype.myReduceRight2 = function(cb, initialVal) {
  if (!cb)
    throw new Error("No CB defined");

  const arr = [...this].reverse(); // don't reverse the original array

  const res = arr.myReduce(cb, initialVal); // pass initialVal

  return res;
}

const test1 = [1, 20, 30, 80, 2, 9, 3];
const test2 = [[1,2], [3,4], [5,6]];

const test1R = test1.myReduceRight2((acc, val) => {
    return acc.concat(val);
}, []);

const test2R = test2.myReduceRight2((acc, val) => {
    return acc.concat(val);
}, []);

console.log(test1R);
console.log(test2R);