What's a good mathematical sets implementation in JavaScript?

前端 未结 7 1385
予麋鹿
予麋鹿 2021-02-06 16:19

Where is a good mathematical sets implementation for JavaScript? It should include efficient implementations of intersection, union, complement, and (for bonus points) the Carte

7条回答
  •  不思量自难忘°
    2021-02-06 16:35

    Using Underscore's reduce method.

    function cartesianProductOf(){
        return _.reduce(arguments, function(mtrx, vals){
            return _.reduce(vals, function(array, val){
                return array.concat(
                    _.map(mtrx, function(row){ return row.concat(val); })
                );
            }, []);
        }, [[]]);
    }
    

提交回复
热议问题