Skip to content

Commit

Permalink
Initial commit. Setup config and copy paste from Jabz
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed May 20, 2017
0 parents commit 6900f1a
Show file tree
Hide file tree
Showing 11 changed files with 756 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
*.log
bundle.js
*.map
node_modules/
*~
typings
.tern-port
.#*
dist/
benchmark/hareactive-old
.nyc_output/
coverage/
src/**/*.js
test/**/*.js
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "6"
- "6.1"

after_success:
- "npm run codecov"
36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
// This configurtion makes it possible to debug the Mocha tests with
// break points.
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Tests 2",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"protocol": "inspector",
"args": [
"--no-timeouts",
"-r",
"ts-node/register",
"--colors",
"${workspaceRoot}/test/**/*.ts"
],
"outFiles": [
"${workspaceRoot}/dist"
],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"runtimeExecutable": null,
"internalConsoleOptions": "openOnSessionStart",
"stopOnEntry": false,
"env": {
"NODE_ENV": "testing"
},
"skipFiles": [
"node_modules/**/*.js",
"<node_internals>/**/*.js"
]
},
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Simon Friis Vindum

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
81 changes: 81 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"name": "@funkia/io",
"version": "0.0.1",
"description": "A library that turns impure code pure.",
"main": "dist/index.js",
"module": "dist/es/index.js",
"types": "dist/defs/index.d.ts",
"directories": {
"test": "test",
"dist": "dist"
},
"scripts": {
"build": "npm run build-es6; npm run build-cmjs",
"build-es6": "tsc -P ./tsconfig-release.json --outDir 'dist/es' --target es2015 --module es2015",
"build-cmjs": "tsc -P ./tsconfig-release.json",
"prepare": "npm run clean; npm run build",
"clean": "rm -rf dist coverage .nyc_output",
"test": "nyc mocha --recursive test/**/*.ts",
"test-watch": "mocha -R progress --watch --compilers ts:ts-node/register test/**/*.ts",
"codecov": "codecov -f coverage/coverage-final.json",
"release-major": "xyz --repo git@github.com:funkia/io.git --increment major",
"release-minor": "xyz --repo git@github.com:funkia/io.git --increment minor",
"release-patch": "xyz --repo git@github.com:funkia/io.git --increment patch"
},
"repository": {
"type": "git",
"url": "git+https://github.com/funkia/io.git"
},
"keywords": [
"frp",
"functional reactive programming",
"pure",
"funkia"
],
"author": "Funkia",
"license": "MIT",
"bugs": {
"url": "https://github.com/funkia/io/issues"
},
"homepage": "https://github.com/funkia/io#readme",
"dependencies": {
"@funkia/jabz": "0.0.21",
"tslib": "^1.7.1"
},
"devDependencies": {
"@types/benchmark": "^1.0.30",
"@types/chai": "^3.5.2",
"@types/mocha": "^2.2.41",
"@types/sinon": "^1.16.36",
"benchmark": "^2.1.4",
"browser-env": "^2.0.31",
"browserify": "^14.3.0",
"browserify-istanbul": "^2.0.0",
"chai": "^3.5.0",
"codecov": "^2.1.0",
"mocha": "^3.3.0",
"nyc": "^10.3.2",
"sinon": "^2.2.0",
"source-map-support": "^0.4.15",
"ts-node": "^3.0.3",
"tsify": "^3.0.1",
"typescript": "^2.3.2",
"watchify": "^3.9.0",
"webpack": "^2.5.0",
"xyz": "2.1.0"
},
"nyc": {
"extension": [
".ts"
],
"require": [
"ts-node/register",
"source-map-support/register"
],
"reporter": [
"json",
"html",
"text"
]
}
}
60 changes: 60 additions & 0 deletions src/freer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {AbstractMonad, Monad, monad} from "@funkia/jabz";

export type FreerMatch<F, A, K> = {
pure: (a: A) => K
bind: (u: F, k: (a: any) => Freer<F, A>) => K
};

export abstract class Freer<F, A> extends AbstractMonad<A> {
static of<B>(b: B): Freer<any, B> {
return new Pure(b);
}
of<B>(b: B): Freer<F, B> {
return new Pure(b);
}
abstract match<K>(m: FreerMatch<F, A, K>): K;
abstract map<B>(f: (a: A) => B): Freer<F, B>;
multi: false;
static multi = false;
abstract chain<B>(f: (a: A) => Freer<F, B>): Freer<F, B>;
}

@monad
export class Pure<F, A> extends Freer<F, A> {
constructor(private a: A) {
super();
}
match<K>(m: FreerMatch<F, A, K>): K {
return m.pure(this.a);
}
map<B>(f: (a: A) => B): Freer<F, B> {
return new Pure(f(this.a));
}
chain<B>(f: (a: A) => Freer<F, B>): Freer<F, B> {
return f(this.a);
}
}

function pure<F, A>(a: A): Freer<F, A> {
return new Pure(a);
}

@monad
export class Bind<F, A> extends Freer<F, A> {
constructor(public val: any, public f: (a: any) => Freer<F, A>) {
super();
}
match<K>(m: FreerMatch<F, A, K>): K {
return m.bind(this.val, this.f);
}
map<B>(f: (a: A) => B): Freer<F, B> {
return new Bind(this.val, (a: any) => this.f(a).map(f));
}
chain<B>(f: (a: A) => Freer<F, B>): Freer<F, B> {
return new Bind(this.val, (a: any) => this.f(a).chain(f));
}
}

export function liftF<F, A>(fa: any): Freer<F, A> {
return new Bind(fa, pure);
}
153 changes: 153 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { Freer, liftF } from "./freer";
import { Monad, AbstractMonad } from "@funkia/jabz";

function deepEqual(a: any, b: any): boolean {
if (typeof a === "object" && typeof b === "object") {
const aKeys = Object.keys(a);
for (const key of aKeys) {
if (!deepEqual(a[key], b[key])) {
return false;
}
}
return true;
} else {
return a === b;
}
}

export type F0<Z> =
() => Z;
export type F1<A, Z> =
(a: A) => Z;
export type F2<A, B, Z> =
(a: A, b: B) => Z;
export type F3<A, B, C, Z> =
(a: A, b: B, c: C) => Z;
export type F4<A, B, C, D, Z> =
(a: A, b: B, c: C, d: D) => Z;
export type F5<A, B, C, D, E, Z> =
(a: A, b: B, c: C, d: D, e: E) => Z;

export type IOValue<A> = Call | CallP | ThrowE | CatchE;

export class Call {
type: "call" = "call";
constructor(public fn: Function, public args: any[]) { }
}

export class CallP {
type: "callP" = "callP";
constructor(public fn: Function, public args: any[]) { }
}

export class ThrowE {
type: "throwE" = "throwE";
constructor(public error: any) { }
}

export class CatchE {
type: "catchE" = "catchE";
constructor(public handler: (error: any) => IO<any>, public io: IO<any>) { }
}

export type IO<A> = Freer<IOValue<any>, A>;

export const IO = Freer;

// in the IO monad
export function withEffects<A, Z>(f: F1<A, Z>): (a: A) => IO<Z>;
export function withEffects<A, B, Z>(f: F2<A, B, Z>): (a: A, b: B) => IO<Z>;
export function withEffects<A, B, C, Z>(f: F3<A, B, C, Z>): (a: A, b: B, c: C) => IO<Z>;
export function withEffects<A, B, C, D, Z>(f: F4<A, B, C, D, Z>): (a: A, b: B, c: C, d: D) => IO<Z>;
export function withEffects<A, B, C, D, E, Z>(f: F5<A, B, C, D, E, Z>): (a: A, b: B, c: C, d: D, e: E) => IO<Z>;
export function withEffects<A>(fn: any): (...as: any[]) => IO<A> {
return (...args: any[]) => liftF(new Call(fn, args));
}

export function withEffectsP<A, Z>(f: F1<A, Promise<Z>>): (a: A) => IO<Z>;
export function withEffectsP<A, B, Z>(f: F2<A, B, Promise<Z>>): (a: A, b: B) => IO<Z>;
export function withEffectsP<A, B, C, Z>(f: F3<A, B, C, Promise<Z>>): (a: A, b: B, c: C) => IO<Z>;
export function withEffectsP<A, B, C, D, Z>(f: F4<A, B, C, D, Promise<Z>>): (a: A, b: B, c: C, d: D) => IO<Z>;
export function withEffectsP<A, B, C, D, E, Z>(f: F5<A, B, C, D, E, Promise<Z>>): (a: A, b: B, c: C, d: D, e: E) => IO<Z>;
export function withEffectsP<A>(fn: (...as: any[]) => Promise<A>): (...a: any[]) => IO<A> {
return (...args: any[]) => liftF(new CallP(fn, args));
}

export function call<Z>(f: F0<Z>): IO<Z>;
export function call<A, Z>(f: F1<A, Z>, a: A): IO<Z>;
export function call<A, B, Z>(f: F2<A, B, Z>, a: A, b: B): IO<Z>;
export function call<A, B, C, Z>(f: F3<A, B, C, Z>, a: A, b: B, c: C): IO<Z>;
export function call<A, B, C, D, Z>(f: F4<A, B, C, D, Z>, a: A, b: B, c: C, d: D): IO<Z>;
export function call<A, B, C, D, E, Z>(f: F5<A, B, C, D, E, Z>, a: A, b: B, c: C, d: D, e: E): IO<Z>;
export function call(fn: Function, ...args: any[]): IO<any> {
return liftF(new Call(fn, args));
}

export function callP<Z>(f: F0<Z>): IO<Z>;
export function callP<A, Z>(f: F1<A, Promise<Z>>, a: A): IO<Z>;
export function callP<A, B, Z>(f: F2<A, B, Promise<Z>>, a: A, b: B): IO<Z>;
export function callP<A, B, C, Z>(f: F3<A, B, C, Promise<Z>>, a: A, b: B, c: C): IO<Z>;
export function callP<A, B, C, D, Z>(f: F4<A, B, C, D, Promise<Z>>, a: A, b: B, c: C, d: D): IO<Z>;
export function callP<A, B, C, D, E, Z>(f: F5<A, B, C, D, E, Promise<Z>>, a: A, b: B, c: C, d: D, e: E): IO<Z>;
export function callP(fn: Function, ...args: any[]): IO<any> {
return liftF(new CallP(fn, args));
}

export function throwE(error: any): IO<any> {
return liftF(new ThrowE(error));
}

export function catchE(
errorHandler: (error: any) => IO<any>, io: IO<any>
): IO<any> {
return liftF(new CatchE(errorHandler, io));
}

export function doRunIO<A>(e: IO<A>): Promise<A> {
return e.match<Promise<A>>({
pure: (a) => Promise.resolve(a),
bind: (io, cont) => {
switch (io.type) {
case "call":
return runIO(cont(io.fn(...io.args)));
case "callP":
return io.fn(...io.args)
.then((a: A) => runIO(cont(a)));
case "catchE":
return doRunIO(io.io)
.then((a: A) => runIO(cont(a)))
.catch((err: any) => doRunIO(io.handler(err)));
case "throwE":
return Promise.reject(io.error);
}
}
});
}

export function runIO<A>(e: IO<A>): Promise<A> {
return doRunIO(e);
}

function doTestIO<A>(e: IO<A>, arr: any[], ending: A, idx: number): void {
e.match({
pure: (a2) => {
if (ending !== a2) {
throw new Error(
`Pure value invalid, expected ${ending} but saw ${a2}`
);
}
},
bind: (io, cont) => {
const [{ val: io2 }, a] = arr[idx];
if (!deepEqual(io, io2)) {
throw new Error(`Value invalid, expected ${io2} but saw ${io}`);
} else {
doTestIO(cont(a), arr, ending, idx + 1);
}
}
});
}

export function testIO<A>(e: IO<A>, arr: any[], a: A): void {
doTestIO(e, arr, a, 0);
}
Loading

0 comments on commit 6900f1a

Please sign in to comment.