add tsmonad

This commit is contained in:
ValentinTrinque
2015-06-19 18:37:18 +02:00
parent 6594eb9945
commit b43985c211
5 changed files with 76 additions and 1 deletions
+45
View File
@@ -0,0 +1,45 @@
/// <reference path="../tsmonad.d.ts" />
class User {
private age: number;
constructor(age?: number) {
this.age = age ? age : 0;
}
public getAge(): TsMonad.Either<string, number> {
if (this.age > 0) {
return TsMonad.Either.right<string, number>(this.age);
} else {
return TsMonad.Either.left<string, number>('Information withheld');
}
}
}
module Station {
export class BusPass {
public isValidForRoute(route: string): boolean {
return true;
}
}
export function getBusPass(age: number): TsMonad.Either<string, BusPass> {
if (age > 18) {
return TsMonad.Either.right<string, BusPass>(new BusPass());
} else {
return TsMonad.Either.left<string, BusPass>('Too young for a bus pass');
}
}
}
var user = new User(42)
var canRideForFree = user.getAge()
.bind(age => Station.getBusPass(age))
.caseOf({
right: busPass => busPass.isValidForRoute('Weston'),
left: errorMessage => { console.log(errorMessage); return false; }
});
+20
View File
@@ -0,0 +1,20 @@
/// <reference path="../tsmonad.d.ts" />
var turns_out_to_be_100 = TsMonad.Maybe.just(10)
.caseOf({
just: n => n * n,
nothing: () => -1
});
var turns_out_to_be_nothing = TsMonad.Maybe.nothing<number>()
.caseOf({
just: n => n * n,
nothing: () => -1
});
var turns_out_to_be_true = TsMonad.Maybe.just(123)
.lift(n => n * 2)
.caseOf({
just: n => n === 246,
nothing: () => false
});
+8
View File
@@ -0,0 +1,8 @@
/// <reference path="../tsmonad.d.ts" />
var is_true = TsMonad.Writer.writer(['Started with 0'], 0)
.bind(x => TsMonad.Writer.writer(['+ 8'], x + 8))
.bind(x => TsMonad.Writer.writer(['- 6', '* 8'], 8 * (x - 6)))
.caseOf({
writer: (s, v) => v === 16 && s.join(', ') === 'Started with 0, + 8, - 6, * 8'
});
+3
View File
@@ -0,0 +1,3 @@
/// <reference path="tests/maybe-tests.ts" />
/// <reference path="tests/writer-tests.ts" />
/// <reference path="tests/either-tests.ts" />
-1
View File
@@ -1,7 +1,6 @@
// Type definitions for TsMonad
// Project: https://github.com/cbowdon/TsMonad
// Definitions by: Chris Bowdon <https://github.com/cbowdon>
// Definitions by: Valentin Trinque <https://github.com/ValentinTrinque>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module TsMonad {