-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added map/flatmap for rxjs/immutablejs/maybe/array
- Loading branch information
0 parents
commit b1fccea
Showing
33 changed files
with
5,375 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './flat-map'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"use strict"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './map'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
Oops, something went wrong.