Sha256: 2d3e9549161d44f8ac4bae94fb9936fdce332e3979c3a7a3ff87f5d51f2e3ac7

Contents?: true

Size: 1.41 KB

Versions: 73

Compression:

Stored size: 1.41 KB

Contents

'use strict';

function List(arr) {
  this.values = arr || [];
}

List.prototype = {
  append: function (otherList) {
    var appended = this.values;

    for (var i = 0; i < otherList.length(); i++) {
      appended.push(otherList.values[i]);
    }

    return new List(appended);
  },

  concat: function (otherList) {
    return this.append(otherList);
  },

  cons: function (item, arr) {
    var x = new List([item]);
    var xs = new List(arr);
    return x.append(xs).values;
  },

  foldl: function (func, start) {
    var acc = start;

    for (var i = 0; i < this.length(); i++) {
      acc = func(this.values[i], acc);
    }

    return acc;
  },

  foldr: function (func, start) {
    var acc = start;

    for (var i = this.length() - 1; i >= 0; i--) {
      acc = func(this.values[i], acc);
    }

    return acc;
  },

  length: function () {
    var count = 0;
    this.values.forEach(function () { count++; });

    return count;
  },

  reverse: function () {
    return new List(this.foldl(this.cons, []));
  },

  map: function (func) {
    var applyFuncThenCons = function (x, acc) {
      return this.cons(func(x), acc);
    };

    return new List(this.foldr(applyFuncThenCons.bind(this), []));
  },

  filter: function (pred) {
    var consIfPred = function (x, acc) {
      return pred(x) ? this.cons(x, acc) : acc;
    };

    return new List(this.foldr(consIfPred.bind(this), []));
  }
};

module.exports = List;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.179 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.178 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.177 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.176 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.175 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.174 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.173 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.172 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.171 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.170 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.169 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.167 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.166 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.165 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.164 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.163 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.162 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.161 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.160 tracks/javascript/exercises/list-ops/example.js