What is 入

Vote on Hacker News Star

入 is a bunch of macros and datastructures ported from clojure to javascript

入 is based on 森(mori) + sweet.js macros

入 is begin something

入 is become something

入 is a chinese character trying his best pretending to be a λ

May the 入 be with You!

Why 入

so 入 here to to rip all good parts of clojure into native macros of JavaScript

the following spec is written with macros ruspec in Joy of Clojure style

arity

  fact 'defn arity func' {
    $defn f {
      (a){a}
      (a, b) {a+b}
    }
    should f(1) => 1;
    should f(2,3) => 5;
  }

lambda

 fact 'with place holder' {
    should [1,2,3,4].reduce(#($+$2),0) => 10
  }

let

  fact 'destructure nested array' {
    should $let([x,[y]]=[1,[2,4],3], [z] = [4,5,6]){
      x+y+z
    } => 7
  }

recur

  fact 'recur function' {
    $defn f{(a,b){
      if(a>b) return a;
      recur(a++,b--)
    }}
    should f(1,36) => 19
  }
  fact 'looprecur' {
    $loop(a=1,b=36){
      if(a>b) return a;
      recur(a++,b--)
    } => 19
  }

core.async

var mori = require('con.js')
fact 'core.async' {
  it('async put and get channel a and b', function(done) {
    var channel1 = mori.async.chan();
    var channel2 = mori.async.chan();
    var data2 = [1,2,3];
    go {
      var a <! channel1;
      var b <! channel2;
      expect(a).toBe("data1");
      expect(b).toEqual([1,2,3]);
      done();
    };
    go {data2 >! channel2};
    go {'data1' >! channel1};
  })

  it('alts channel a and b', function(done) {
    var channela = mori.async.chan();
    var channelb = mori.async.chan();
    var data2 = [1,2,3];

    go {
      var anywho <!alts [channela, channelb];
      // vector.a(0) is equals to nth(vector, 0)
      expect(anywho.a(0)).toEqual([1,2,3]);
      expect(anywho.a(1)).toBe(channelb);
      done();
    };
    go {data2 >! channelb};
    go {'data1' >! channela};
  })
}

thread

should ('foo' ->> [mori.conj(mori.vector('bar')),
                   mori.map(function(x){return x.toUpperCase()})])
  .toString() => '("BAR" "FOO")'
}

Readtable

mori datastructure literals

#[bar, he] // => mori.vector(bar,he)
#{a: 1, b: 2} //=> mori.hashMap(:a, 1, :b, 2)
##{1, 2, 3} //=> mori.set([1,2,3])

入(rù) mori datastructure

  fact 'js to mori datastructure' {
  should mori.equals(
    $ru(map(inc, [0,1,2,3,4])),
    mori.vector(1,2,3,4,5)) => true
  }
  fact 'array' {
    should mori.equals(into([0],[1,2,3,4]), mori.vector(0,1,2,3,4)) => true
  }

出(chū) mori datastructure

  fact 'mori expression to js' {
    should $chu(map(inc, [0,1,2,3,4])).pop() => 5
  }

Checkout all Readable Specs for detail…