mori
A simple bridge to ClojureScript's persistent data structures and supporting APIs for vanilla JavaScript. Pull requests welcome.
Breaking changes in 0.3.0
This release includes several breaking changes:
- More idiomatic JavaScript API naming scheme
- No EDN reader
- No zippers
- No data diffing
These missing options will likely find their way back into Mori when Mori builds can leverage Google Closure Modules.
Getting it
You can install the latest release via npm:
npm install mori
The installed package contains a single optimized JavaScript file mori.js
.
Load mori
in your Node.js programs as you would any other module:
var mori = ;
In a browser, you can load mori with a script tag, as you would any other JavaScript library:
You can also load it as an AMD module, e.g. with RequireJS.
Build
Prerequisites
You will first need to install the Java SDK, if it's not already installed on your system.
On Windows, you will need to manually install Leiningen. On UNIX-like systems, Leiningen will be installed within the project automatically if the lein
executable is not found on your path or if your lein
version predates 2.0.0
.
Clone the repo
git clone https://github.com/swannodette/mori.gitcd mori
On a UNIX-like system build with
./scripts/build.sh
Alternatively using npm
npm run-script build
On Windows
./scripts/build.ps1
The build process will generate an optimized JavaScript file mori.js
, which is suitable for use with Node.js, or in a Web browser or other JavaScript environments. You can also load it as an AMD module.
Usage
You can use it from your projects like so:
var { return n+1;}; mori;// => [2,3,4,5,6]
Efficient non-destructive updates!
var v1 = mori;var v2 = mori;v1; // => '[1 2 3]'v2; // => '[1 2 3 4]'
var { return a + b;};mori; // => 10
Lazy sequences!
var _ = mori;_;// => [1, "foo", 2, "foo", 3, "foo", 4]
Or if it's more your speed, use it from CoffeeScript!
= x+1 r = morimap incmorivector12345moriintoArray r
Documentation
You can find extensive documentation and examples here.
More Examples
Efficient Freeze/Thaw
For vectors and maps we provide an efficient thaw and freeze operations:
var m = mori; // ~220ms with V8 version 3.29.80 MBP 2.26ghzforvar j = 0; j < 10; j++ var s = ; var arr = ; forvar i = 0; i < 10000000; i++ arr; ; ; // ~70msforvar j = 0; j < 10; j++ s = ; var mv = mmutable; forvar i = 0; i < 10000000; i++ mv = mmutableconj; var v = mmutable; ; ;
ES6 Map/Set inspired interfaces
All Mori maps and sets support all the non-mutating methods of the
proposed ES6
Map
and
Set
interfaces. The main difference with the spec is that key lookup is
based on value not reference. keys
, values
, and entries
methods
return the proposed mutable iterators:
var m = mori;var h = m; h; // => trueh; // => 1 var iter = h;iternext; // => {done: false, value: "foo"}
This feature is subject to changes in the ES6 proposal.
Transducers
Mori includes Transducers. Zero allocation collection operations FTW:
var m = mori;var a = ; forvar i = 0; i < 1000000; i++ a; // make it immutablevar v = m; { var s = ; ; console;} // ~190ms V8 version 3.29.80 MBP 2.26ghz; // ~440ms;
Copyright (C) 2012-2015 David Nolen and contributors
Distributed under the Eclipse Public License, the same as Clojure.