Skip to content

Instantly share code, notes, and snippets.

@lorenzoongithub
Created May 19, 2015 21:12
Show Gist options
  • Save lorenzoongithub/ab6d188f80ec6591c926 to your computer and use it in GitHub Desktop.
Save lorenzoongithub/ab6d188f80ec6591c926 to your computer and use it in GitHub Desktop.
mori.js
//
// mori.js - A library for using ClojureScript's persistent data structures and supporting API
// from the comfort of vanilla JavaScript.
//
// http://swannodette.github.io/mori/
//
// Inspired from https://raw.githubusercontent.com/swannodette/mori/master/spec/mori-spec.js (9/May/2015)
//
load('https://cdnjs.cloudflare.com/ajax/libs/mori/0.3.2/mori.js');
// 1. demonstrates mapping a function over a vector
inc = function (n) { return n + 1; },
outArr = mori.intoArray(mori.map(inc, mori.vector(1,2,3,4,5)));
if (outArr.length != 5) throw '';
if (outArr[0] != 2) throw '';
if (outArr[1] != 3) throw '';
if (outArr[2] != 4) throw '';
if (outArr[3] != 5) throw '';
if (outArr[4] != 6) throw '';
// 2. demonstrates a non-destructive collection update
v1 = mori.vector(1,2,3),
v2 = mori.conj(v1, 4);
if (JSON.stringify(mori.intoArray(v1)) !== "[1,2,3]") throw '';
if (JSON.stringify(mori.intoArray(v2)) !== "[1,2,3,4]") throw '';
// 3. demonstrates functional reduction on a vector
sum = function(a, b) { return a + b; };
reduced = mori.reduce(sum, mori.vector(1, 2, 3, 4));
if (reduced != 10) throw '';
// 4. demonstrates interposition on a lazy sequence
_ = mori;
interposed = _.intoArray(_.interpose("foo", _.vector(1, 2, 3, 4)));
if (interposed.length != 7) throw '';
if (interposed[0] != 1) throw '';
if (interposed[1] != 'foo') throw '';
if (interposed[2] != 2) throw '';
if (interposed[3] != 'foo') throw '';
if (interposed[4] != 3) throw '';
if (interposed[5] != 'foo') throw '';
if (interposed[6] != 4) throw '';
// 5. demonstrates function pipelining
pipeRes = mori.pipeline(
mori.vector(1,2,3),
function(v) { return mori.conj(v,4); },
function(v) { return mori.drop(2, v); });
array = mori.intoArray(pipeRes);
if (array.length != 2) throw '';
if (array[0] != 3) throw '';
if (array[1] != 4) throw '';
// 6. demonstrates function currying
curRes = mori.pipeline(
mori.vector(1,2,3),
mori.curry(mori.conj, 4),
mori.curry(mori.conj, 5));
array = mori.intoArray(curRes);
if (array.length != 5) throw '';
if (array[0] != 1) throw '';
if (array[1] != 2) throw '';
if (array[2] != 3) throw '';
if (array[3] != 4) throw '';
if (array[4] != 5) throw '';
// demonstrates partial function application
parRes = mori.pipeline(
mori.vector(1,2,3),
mori.curry(mori.conj, 4),
mori.partial(mori.drop, 2));
array = mori.intoArray(parRes)
if (array.length != 2) throw '';
if (array[0] != 3) throw '';
if (array[1] != 4) throw '';
// demonstrates function composition
second = mori.comp(mori.first, mori.rest);
secRes = second(mori.vector(1,2,3));
if (secRes != 2) throw '';
// demonstrates function juxtaposition
posAndNeg = mori.juxt(mori.identity, function (v) { return -v; });
pnRes = posAndNeg(1);
if (mori.intoArray(pnRes)[0] != 1) throw ''
if (mori.intoArray(pnRes)[1] != -1) throw ''
knitRes = mori.knit(mori.inc, mori.dec)(posAndNeg(1));
if (mori.intoArray(knitRes)[0] != 2) throw ''
if (mori.intoArray(knitRes)[1] != -2) throw ''
// --------------------------
// demonstrates conversion from clojurescript values to javascript objects, and vice versa"
jsObj = { a: 1, b: "two" }
jsArr = [1, 2, 3];
cljMap = mori.hashMap("a", 1, "b", "two");
cljMapKeywordized = mori.hashMap(mori.keyword("a"), 1, mori.keyword("b"), "two");
cljVec = mori.vector(1, 2, 3);
if (mori.equals(mori.toClj(jsObj), cljMap) == false) throw '';
if (mori.equals(mori.toClj(jsObj,true), cljMapKeywordized) == false) throw '';
if (mori.equals(mori.toClj(jsArr), cljVec) == false) throw '';
if (mori.isVector(mori.toClj(jsArr)) == false) throw '';
if (mori.toJs(cljMap).a != 1) throw '';
if (mori.toJs(cljMap).b != 'two') throw '';
if (mori.toJs(cljVec)[0] != 1) throw '';
if (mori.toJs(cljVec)[1] != 2) throw '';
if (mori.toJs(cljVec)[2] != 3) throw '';
// demonstrates function `distinct`
vec = mori.vector(1,1,1,1,2,2,3,4,5,6,6);
distinctVector = mori.distinct(vec);
array = mori.toJs(distinctVector)
if (array[0] != 1) throw '';
if (array[1] != 2) throw '';
if (array[2] != 3) throw '';
if (array[3] != 4) throw '';
if (array[4] != 5) throw '';
if (array[5] != 6) throw '';
// can tune *print-length*
mori.configure("print-length", 5);
if (mori.range(10).toString() !== "(0 1 2 3 4 ...)") throw '';
mori.configure("print-length", 3);
if (mori.range(5).toString() !== "(0 1 2 ...)") throw '';
mori.configure("print-length", null);
if (mori.range(5).toString() !== "(0 1 2 3 4)") throw '';
// can tune *print-level*"
mori.configure("print-level", 3);
m = mori;
if (m.vector(1, m.vector(2, m.vector(3, m.vector(4, m.vector(5))))).toString() !== "[1 [2 [3 #]]]") throw '';
mori.configure("print-level", 1);
if (m.hashMap(1, m.hashMap(2, 3)).toString() !== "{1 #}") throw '';
mori.configure("print-level", null);
if (m.vector(1, m.vector(2, m.vector(3, m.vector(4)))).toString() !== "[1 [2 [3 [4]]]]") throw '';
// can be initialized empty"
q = mori.queue();
if (mori.isEmpty(q) == false) throw '';
// can be initialized with values"
q = mori.queue('a', 'b');
if (mori.isEmpty(q)==true) throw '';
if (mori.peek(q) !== 'a') throw '';
// can be used build non-stack-blowing seq functions"
var m = mori;
var fib = function(a, b) {
return m.cons(a, m.lazySeq(function() {
return fib(b, b + a);
}));
};
fibs = m.take(10, fib(1, 1));
array = m.toJs(fibs);
if (array[0] != 1) throw '';
if (array[1] != 1) throw '';
if (array[2] != 2) throw '';
if (array[3] != 3) throw '';
if (array[4] != 5) throw '';
if (array[5] != 8) throw '';
if (array[6] != 13) throw '';
if (array[7] != 21) throw '';
if (array[8] != 34) throw '';
if (array[9] != 55) throw '';
// Numbers can only be so big
var bigFib = m.last(m.take(2000, fib(1, 1)));
if (bigFib != Infinity) throw '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment