Skip to content

ClojureScript's persistent data structures and supporting API from the comfort of vanilla JavaScript

Notifications You must be signed in to change notification settings

tailrecursion/mori

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

mori

Mori

A simple bridge to ClojureScript's persistent data structures and supporting APIs for vanilla JavaScript. Pull requests welcome.

Getting it

It's available for Node.js via npm:

npm install mori

The installed package contains two JavaScript files: mori.node.js and mori.js. The first is used by Node.js, the second may be used in a Web browser or other JavaScript environments in the usual way.

Load mori in your Node.js programs as you would any other module:

var mori = require("mori");

In a browser, you can load mori with a script tag, as you would any other JavaScript library:

<script src="mori.js" type="text/javascript"></script>

Caveats

Pre-pre-pre alpha. ClojureScript is constantly being improved, especially in terms of performance. That said, it's probably still already useful.

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.git
cd 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 two JavaScript files: mori.node.js and mori.js. The first is used by Node.js, the second may be used in a Web browser or other JavaScript environments in the usual way.

Usage

You can use it from your projects like so:

var inc = function(n) {
  return n+1;
};

mori.into_array(mori.map(inc, mori.vector(1,2,3,4,5)));
// => [2,3,4,5,6]

Efficient non-destructive updates!

var v1 = mori.vector(1,2,3);
var v2 = mori.conj(v1, 4);
v1.toString(); // => '[1 2 3]'
v2.toString(); // => '[1 2 3 4]'
var sum = function(a, b) {
  return a + b;
};
mori.reduce(sum, mori.vector(1, 2, 3, 4)); // => 10

Lazy sequences!

var _ = mori;
_.into_array(_.interpose("foo", _.vector(1, 2, 3, 4)));
// => [1, "foo", 2, "foo", 3, "foo", 4]

Or if it's more your speed, use it from CoffeeScript!

inc = (x) -> x+1  
r = mori.map inc, mori.vector(1,2,3,4,5)
mori.into_array r

Reducers

Mori includes the new Clojure reducers framework. Zero allocation collection operations FTW:

var m = mori;
var a = [];

for(var i = 0; i < 1000000; i++) {
  a.push(i);
}

// make it immutable
var v = m.into(m.vector(), a);

var mul3 = function(n) {
  return n*3;
}

function time(f) {
  var s = new Date();
  f();
  console.log(((new Date())-s)+"ms");
}

// 513ms
time(function() {
  m.reduce(m.sum, 0, m.rmap(m.inc, m.rfilter(m.is_even, m.rmap(mul3, v))));
});

// 254ms
time(function() {
  a.map(mul3).filter(m.is_even).map(m.inc).reduce(m.sum);
})

// impressive given the level of abstraction
// expect us to get more competitive :D

Pipelines

mori.pipeline(mori.vector(1,2,3),
              function(v) { return mori.conj(v,4) },
              function(v) { return mori.drop(2, v) });

// => [3 4]

Currying

mori.pipeline(mori.vector(1,2,3),
              mori.curry(mori.conj, 4),
              mori.curry(mori.conj, 5));

// => [1 2 3 4 5]

Partial Application

mori.pipeline(mori.vector(1,2,3),
              mori.curry(mori.conj, 4),
              mori.partial(mori.drop, 2));

// => (3 4)

Function Composition

var second = mori.comp(mori.first, mori.rest);

second(mori.vector(1,2,3));
// => 2

Juxtaposition

var pos_and_neg = mori.juxt(mori.identity, function (v) { return -v; });
pos_and_neg(1);
// => [1 -1]

mori.knit(mori.inc, mori.dec)(pos_and_neg(1));
// => [2 -2]

Copyright (C) 2012 David Nolen and contributors

Distributed under the Eclipse Public License, the same as Clojure.

About

ClojureScript's persistent data structures and supporting API from the comfort of vanilla JavaScript

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Clojure 45.2%
  • JavaScript 28.1%
  • HTML 23.9%
  • Shell 2.3%
  • PowerShell 0.5%