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.159 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.158 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.157 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.156 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.155 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.154 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.153 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.152 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.151 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.150 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.149 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.148 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.147 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.146 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.145 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.144 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.143 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.142 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.141 tracks/javascript/exercises/list-ops/example.js
trackler-2.2.1.140 tracks/javascript/exercises/list-ops/example.js