Skip to content

Commit

Permalink
Added map/flatmap for rxjs/immutablejs/maybe/array
Browse files Browse the repository at this point in the history
  • Loading branch information
chorobinaccess committed Jun 18, 2018
0 parents commit b1fccea
Show file tree
Hide file tree
Showing 33 changed files with 5,375 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 2 additions & 0 deletions dist/flat-map/flat-map.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Monad, MonadType } from '../monad';
export declare const flatMap: <T, U, UMonad extends Monad<U>>(f: (val: T) => UMonad) => (monad: MonadType<UMonad, T>) => UMonad;
22 changes: 22 additions & 0 deletions dist/flat-map/flat-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var immutable_1 = require("immutable");
var flatMapArray = function (f) { return function (arr) { return arr.reduce(function (acc, current) { return acc.concat(f(current)); }, []); }; };
exports.flatMap = function (f) { return function (monad) {
var result;
if (rxjs_1.isObservable(monad)) {
result = operators_1.flatMap(f)(monad);
}
else if (immutable_1.Iterable.isIterable(monad)) {
result = monad.flatMap(f);
}
else if (Array.isArray(monad)) {
result = flatMapArray(f)(monad);
}
else {
throw new Error('Invalid type');
}
return result;
}; };
1 change: 1 addition & 0 deletions dist/flat-map/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './flat-map';
6 changes: 6 additions & 0 deletions dist/flat-map/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./flat-map"));
Empty file added dist/index.d.ts
Empty file.
1 change: 1 addition & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";
1 change: 1 addition & 0 deletions dist/map/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './map';
6 changes: 6 additions & 0 deletions dist/map/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./map"));
2 changes: 2 additions & 0 deletions dist/map/map.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Monad, MonadType } from '../monad';
export declare const map: <T, U>(f: (val: T) => U) => <TMonad extends Monad<T>>(monad: TMonad) => MonadType<TMonad, U>;
25 changes: 25 additions & 0 deletions dist/map/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var immutable_1 = require("immutable");
var operators_1 = require("rxjs/operators");
var maybe_1 = require("../maybe/maybe");
exports.map = function (f) { return function (monad) {
var result;
if (rxjs_1.isObservable(monad)) {
result = operators_1.map(f)(monad);
}
else if (immutable_1.Iterable.isIterable(monad)) {
result = monad.map(f);
}
else if (maybe_1.isMaybe(monad)) {
result = maybe_1.map(f)(monad);
}
else if (Array.isArray(monad)) {
result = monad.map(f);
}
else {
throw new Error('Invalid type');
}
return result;
}; };
4 changes: 4 additions & 0 deletions dist/maybe/just.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare type Just<T> = {
readonly get: NonNullable<T>;
readonly isNothing: boolean;
};
2 changes: 2 additions & 0 deletions dist/maybe/just.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
9 changes: 9 additions & 0 deletions dist/maybe/maybe.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Just } from './just';
import { Nothing } from './nothing';
export declare type Maybe<T> = Just<T> | Nothing;
export declare const isMaybe: <T>(couldBeMaybe: {}) => boolean;
export declare const isNothing: <T>(maybe: Maybe<T>) => boolean;
export declare const maybe: <T>(get: T) => Nothing;
export declare const fold: <T, U>(ifJust: (get: T) => U, ifNothing: U) => (maybe: Maybe<T>) => U;
export declare const map: <T, U>(f: (get: T) => U) => (maybe: Maybe<T>) => Nothing;
export declare const flatMap: <T, U>(f: (get: T) => Maybe<U>) => (maybe: Maybe<T>) => Nothing;
11 changes: 11 additions & 0 deletions dist/maybe/maybe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var nothing_1 = require("./nothing");
exports.isMaybe = function (couldBeMaybe) { return couldBeMaybe.isNothing !== undefined; };
exports.isNothing = function (maybe) { return maybe === nothing_1.nothing; };
exports.maybe = function (get) { return get ? { get: get } : nothing_1.nothing; };
exports.fold = function (ifJust, ifNothing) { return function (maybe) {
return exports.isNothing(maybe) ? ifNothing : ifJust(maybe.get);
}; };
exports.map = function (f) { return exports.fold(function (get) { return exports.maybe(f(get)); }, nothing_1.nothing); };
exports.flatMap = function (f) { return exports.fold(f, nothing_1.nothing); };
4 changes: 4 additions & 0 deletions dist/maybe/nothing.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare type Nothing = {
readonly isNothing: boolean;
};
export declare const nothing: Nothing;
5 changes: 5 additions & 0 deletions dist/maybe/nothing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.nothing = {
isNothing: true
};
5 changes: 5 additions & 0 deletions dist/monad.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Observable } from 'rxjs';
import { List } from 'immutable';
import { Maybe } from './maybe/maybe';
export declare type MonadType<TMonad, U> = TMonad extends Observable<infer T> ? Observable<U> : TMonad extends Array<infer T> ? Array<U> : TMonad extends List<infer T> ? List<U> : TMonad extends Maybe<infer T> ? Maybe<U> : never;
export declare type Monad<T> = List<T> | Observable<T> | Array<T> | Maybe<T>;
2 changes: 2 additions & 0 deletions dist/monad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Loading

0 comments on commit b1fccea

Please sign in to comment.