What's a good mathematical sets implementation in JavaScript?

前端 未结 7 1386
予麋鹿
予麋鹿 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:48

    I personally like how it is done in jPaq (http://jpaq.org/documentation/Arrays+as+Sets/1.0/). Here are three examples that I tested out successfully:

    alert([1,2,3,4,5].subtract([2,3,5]));  // evaluates to [1,4]
    alert([1,2,5].union([1,3,4,5]));  // evaluates to [1,2,5,3,4]
    alert([1,2,3,5].intersect([0,1,2,4,6]));  // evaluates to [1,2]
    

    The nice thing about jPaq is the fact that you can just download the code for these three functions. jPaq makes it so you don't have to download the extra stuff that you will not be using anyway.

提交回复
热议问题