M
M
Max Zhukov2018-03-19 12:51:50
JavaScript
Max Zhukov, 2018-03-19 12:51:50

How can code duplication be avoided?

in the chain object, you need to make the wrapChain method
and to look like this
take: wrapChain(self.take),
skip: wrapChain(self.skip),
map: wrapChain(self.map),
foreach: wrapChain(self.foreach),
filter: wrapChain(self.filter)

"use strict";
var customArrayFunctions = (function() {
    function isArrayNumber(arr) {
        for (var i = 0; i < arr.length; i++) {
            if (typeof arr[i] !== "number") {
                return false;
            }
        }
        return true;
    }

    function take(arr, n) {
        if (Array.isArray(arr)) {
            return arr.slice(0, n);
        }
    }

    function skip(arr, n) {
        if (Array.isArray(arr)) {
            return arr.slice(n, arr.length);
        }
    }

    function map(arr, callback) {
        if (Array.isArray(arr)) {
            var length = arr.length,
                newarr = [];
            for (var i = 0; i < length; i = i + 1) {
                newarr.push(callback(i, arr[i]));
            }
            return newarr;
        }
    }

    function reduce(arr, callback, val) {
        if (Array.isArray(arr) && isArrayNumber(arr)) {
            var length = arr.length,
                i=0,
                value;
            value = val === "undefined" ? arr[i++] : val; 
            for (; i < length; i++) {
                value = callback(value, arr[i]);
            }
            return value;
        }
    }

    function filter(arr, callback) {
        if (Array.isArray(arr)) {
            var length = arr.length,
                newarr = [];
            for (var i = 0; i < length; i = i + 1) {
                if (callback(i, arr[i])) {
                    newarr.push(arr[i]);
                }
            }
            return newarr;
        }
    }

    function foreach(arr, callback) {
        if (Array.isArray(arr)) {
            var length = arr.length;
            for (var i = 0; i < length; i = i + 1) {
                callback(i, arr[i]);
            }
        }
    }
    function chain(arr) {
        return {
            take: function(n){
              arr = self.take.call(null,arr,n);
              return this;
          },
            skip: function(n) {
                arr = self.skip.call(null, arr,n);
                return this;
            },
            map: function(callback) {
                arr = self.map.call(null, arr,callback);
                return this;

            },
            reduce: function(callback, val) {
                 arr = self.reduce.call(null, arr,callback,val);
                return this;

            },
            filter: function(callback) {
                arr = self.filter.call(null, arr,callback);
                return this;
            },
            value: () => arr
        };
    }
    var self = {
        take: take,
        skip: skip,
        map: map,
        reduce: reduce,
        filter: filter,
        foreach: foreach,
        chain: chain
    };

    return self;
})();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-19
@MaksZhukov

function wrapChain(f) {
  return function(...args) {
    this._value = f.call(null, this._value, ...args)
    return this
  }
}

function chain(arr) {
  return {
    take: wrapChain(self.take),
    skip: wrapChain(self.skip),
    map: wrapChain(self.map),
    foreach: wrapChain(self.foreach),
    filter: wrapChain(self.filter),
    _value: arr,
    value: function() {
      return this._value
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question