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.139 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.138 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.137 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.136 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.135 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.134 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.133 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.132 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.131 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.130 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.129 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.128 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.127 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.126 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.125 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.124 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.123 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.122 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.121 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.120 tracks/javascript/exercises/list-ops/example.js