npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kansuu.js

v0.3.3

Published

Yet another functional programming library for node.js

Downloads

14

Readme

kansuu.js

Yet another functional programming library for node.js.

Please note that this module is in very experimental stage. It requires node v8.1 or above.

The 'motto' of this library is to have 'enough power with less magic'.

Usage

$ npm install kansuu.js

Testing

The files inside test can be run with:

$ git clone https://github.com/akimichi/kansuu.js.git
$ cd kansuu.js
kansuu.js$ nvm use
kansuu.js$ npm install
kansuu.js$ npm install -g mocha
kansuu.js$ npm test 
kansuu.js$ npm run test:watch

Examples

Prime numbers in stream

const math = require('kansuu.js').math,
 Stream = require('kansuu.js').stream,
 Pair = require('kansuu.js').pair,
 Maybe = require('kansuu.js').monad.maybe,
 List = require('kansuu.js').monad.list;

const primes = Stream.cons(2, (_) => {
  const stream = Stream.unfold(3)(n => {
    return Maybe.just(Pair.cons(n, n+1));
  });
  return Stream.filter(stream)(math.isPrime); 
});

expect(
  List.toArray(Stream.take(primes)(20))
).to.eql(
  [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71]
);

A monadic parser example: simple calculator

See Monadic Parser Combinators for more details.

const math = require('kansuu.js').math,
  List = require('kansuu.js').monad.list,
  Pair = require('kansuu.js').pair,
  Parser = require('kansuu.js').monad.parser;

const expr = (_) => {
  return Parser.chainl1(factor, operator);
};

const operator = (_) => {
  return Parser.append(
    Parser.flatMap(Parser.char('+'))(_ => {
      return Parser.unit(math.add);
    })
  )(
    Parser.flatMap(Parser.char('-'))(_ => {
      return Parser.unit(math.subtract);
    })
  );
};

const factor = (_) => {
  return Parser.append(
    Parser.nat()
  )(
    Parser.bracket(Parser.char("("), expr, Parser.char(")"))
  );
};

const calculator = (input) => {
  return Pair.left(List.head(
    Parser.parse(expr())(List.fromString(input))
  ))
};
describe("calculator", () => {
  it('can calculate an expression', (next) => {
    expect(
       calculator("(1+2)-3")
    ).to.eql(
      0 
    );
    next();
  });

Lambda calculus interpreter

const Env = {
  empty: (variable) => {
    return Maybe.nothing(variable);
  },
  lookup: (identifier, env) => {
    return env(identifier);
  },
  extend: (pair, oldEnv) => {
    return Pair.match(pair,{
      empty: (_) => {
        return Maybe.nothing(_);
      },
      cons: (identifier, value) => {
        return (queryIdentifier) => {
          if(identifier === queryIdentifier) {
            return Maybe.just(value);
          } else {
            return Env.lookup(queryIdentifier,oldEnv);
          }
        };
      }
    });
  }
};
const match = (exp, pattern) => {
  return exp(pattern);
};

const Exp = {
  number: (n) => {
    return (pattern) => {
      return pattern.number(n);
    };
  },
  variable: (name) => {
    return (pattern) => {
      return pattern.variable(name);
    };
  },
  lambda: (variable, exp) => {
    return (pattern) => {
      return pattern.lambda(variable, exp);
    };
  },
  apply: (rator,rand) => {
    return (pattern) => {
      return pattern.apply(rator, rand);
    };
  }
};
const evaluate = (expression) => {
  return (environment) => {
    return match(expression, {
      variable: (name) => {
        return Env.lookup(name, environment);
      },
      number: (n) => {
        return Maybe.just(n);
      },
      lambda: (variable, bodyExp) => {
        return match(variable,{ 
          variable: (name) => {
            return Maybe.just(
              (actualArg) => {
                const newEnv = Env.extend(Pair.cons(name, actualArg),environment);
                return evaluate(bodyExp)(newEnv);
              }
            );
          }
        });
      },
      apply: (lambdaExp, arg) => {
        return Maybe.flatMap(evaluate(lambdaExp)(environment))(closure => {
          return Maybe.flatMap(evaluate(arg)(environment))(actualArg => {
            return closure(actualArg);
          });
        });
      }
    });
  };
};
const lambdaExp = Exp.lambda(Exp.variable("x"), Exp.variable("x")),
  appExp = Exp.apply(lambdaExp, Exp.number(7));  

Maybe.flatMap(evaluate(appExp)(Env.empty))(answer => {
  expect(
    answer
  ).to.eql(
    7
  );
});

Docs

$ node_modules/docco/bin/docco lib/kansuu.js
$ open doc/kansuu.html