Git Product home page Git Product logo

js-ds's Introduction

A library implementing various data structures in Javascript. Decomposed into AMD modules. Can be used as individual pieces, or all in one piece.

Overview

The library so far contains constructors for the following structures:

  1. Iterable
  2. Stacks
  3. Queues
  4. Doubly Linked Lists
  5. Heaps
  6. Union-Find
  7. Binary Search Trees
  8. Red-Black trees
  9. Sorting Algorithms
  10. Algorithms dealing with graphs

The Iterable Mixin

The Iterable mixin is a set of methods for manipulating "iterable objects". For the purposes of the mixin, an iterable object needs to have an iterator function, which when called returns an object with two methods, next and done. Most mixin methods return an iterable object. A toArray method returns an array from an iterable.

  1. forEach(fun) calls fun for each 'element' of the iterable.
  2. map(fun) passes the iterable's values through fun. Returns an iterable.
  3. toArray() returns an array of the values in the iterable.
  4. foldl(fun) performs a fold left. The function needs to have signature fun(acc, item).
  5. foldr(fun) performs a fold right. The function needs to have signature fun(item, acc).
  6. take(n) returns the first n elements of the iterable, as an iterable.
  7. skip(n) skips the first n elements of the iterable. Returns an iterable.
  8. slice(a, b) skips the first a elements, then returns all elements until we've exceeded a total of b elements. E.g. slice(2, 5) will return the 2nd through 4th elements.

The Iterable mixing also comes with a constructor to build an iterable object out of an array or an iterator function, and also comes with built-in class methods for constructing standard (possibly infinite) iterable objects (for example the sequence 1,2,3,...).

Stacks

Implemented in Stack.js, Stacks use a doubly-linked list to provide a standard stack interface. All methods except iteration take constant time. The following methods are implemented:

  • push(item) pushes the item at the top of the stack.
  • pushAll(array) push all items from the array at the top of the stack. Anything with a forEach method would do.
  • pop() removes and returns the item at the top of the stack.
  • peek() returns the item at the top of the stack, without removing it.
  • isEmpty() returns true if and only if the stack has no items.
  • processAll(fun) iterates over the elements in the stack, calling fun on each. The elements are removed at the same time, resulting in an empty stack.
  • iterator() returns an iterator for the stack, which allows usage of the Iterable mixin

Examples

var Stack = require('Stack.js'),
    stack = new Stack();
    
stack.pushAll([1,2,3]);
stack.push(4);
stack.pop();             // ----> 4
stack.peek();            // ----> 3 but does not remove it
stack.each(console.log); // ----> 3 2 1

js-ds's People

Contributors

skiadas avatar

Watchers

James Cloos avatar  avatar  avatar

js-ds's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.