UNPKG

functionalpriorityqueue

Version:
39 lines (31 loc) 1.14 kB
#A functional heap-based priority queue utility written in Javascript This is a heap that I built for a recent set of interviews and thought they might come in handy for someone in the future. I've been trying to get my mind in the habit of thinking more functionally and this was a fun discipline project. I don't believe this a great a example of what I consider functional programming, however - it exhibits how I chose to approach the problem within a budgeted amount of time. Usage === ```javascript const shouldSwap = (child, parent) => (parent > child); //min const {heapPop,heapPush} = heapFactory(shouldSwap); const heap = []; const heap0 = heapPush(heap,0) const heap1 = heapPush(heap0,1) const heap2 = heapPush(heap1,2) const heap3 = heapPush(heap2,3) const heap4 = heapPush(heap3,4) const heap5 = heapPush(heap4,5) const [nextHeap,poppedValue] = heapPop(heap5); //returns [ [ 1, 3, 2, 5, 4 ], 0 ] ``` You can provide a comparator to the erroneously named `heapFactory`. It defaults to:`(child, parent) => (parent > child);` #Running ``` npm install functionalpriorityqueue ``` and ``` npm install ``` and ``` npm test ```