fp-ts-contrib
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -16,2 +16,7 @@ # Changelog | ||
# 0.0.5 | ||
- **New Feature** | ||
- (`Do` notation) add support for `sequenceS` (@gcanti) | ||
# 0.0.4 | ||
@@ -18,0 +23,0 @@ |
@@ -6,2 +6,3 @@ /** | ||
import { Monad, Monad1, Monad2, Monad2C, Monad3, Monad3C } from 'fp-ts/lib/Monad'; | ||
declare type EnforceNonEmptyRecord<R> = keyof R extends never ? never : R; | ||
export interface Do3<M extends URIS3, S extends object> { | ||
@@ -16,2 +17,12 @@ do: <U, L>(ma: Type3<M, U, L, unknown>) => Do3C<M, S, U, L>; | ||
}, U, L>; | ||
sequenceS: <U, L, R extends Record<string, Type3<M, U, L, any>>>(r: EnforceNonEmptyRecord<R> & Record<string, Type3<M, U, L, any>> & { | ||
[K in keyof S]?: never; | ||
}) => Do3C<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type3<M, any, any, infer A>] ? A : never; | ||
}, U, L>; | ||
sequenceSL: <U, L, R extends Record<string, Type3<M, U, L, any>>>(f: (s: S) => EnforceNonEmptyRecord<R> & Record<string, Type3<M, U, L, any>> & { | ||
[K in keyof S]?: never; | ||
}) => Do3C<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type3<M, any, any, infer A>] ? A : never; | ||
}, U, L>; | ||
return: <A, U, L>(f: (s: S) => A) => Type3<M, U, L, A>; | ||
@@ -29,2 +40,12 @@ done: <U, L>() => Type3<M, U, L, S>; | ||
}, U, L>; | ||
sequenceS: <R extends Record<string, Type3<M, U, L, any>>>(r: EnforceNonEmptyRecord<R> & { | ||
[K in keyof S]?: never; | ||
}) => Do3C<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type3<M, any, any, infer A>] ? A : never; | ||
}, U, L>; | ||
sequenceSL: <R extends Record<string, Type3<M, U, L, any>>>(f: (s: S) => EnforceNonEmptyRecord<R> & { | ||
[K in keyof S]?: never; | ||
}) => Do3C<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type3<M, any, any, infer A>] ? A : never; | ||
}, U, L>; | ||
return: <A>(f: (s: S) => A) => Type3<M, U, L, A>; | ||
@@ -42,2 +63,12 @@ done: () => Type3<M, U, L, S>; | ||
}, L>; | ||
sequenceS: <L, R extends Record<string, Type2<M, L, any>>>(r: EnforceNonEmptyRecord<R> & Record<string, Type2<M, L, any>> & { | ||
[K in keyof S]?: never; | ||
}) => Do2C<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type2<M, any, infer A>] ? A : never; | ||
}, L>; | ||
sequenceSL: <L, R extends Record<string, Type2<M, L, any>>>(f: (s: S) => EnforceNonEmptyRecord<R> & Record<string, Type2<M, L, any>> & { | ||
[K in keyof S]?: never; | ||
}) => Do2C<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type2<M, any, infer A>] ? A : never; | ||
}, L>; | ||
return: <A, L>(f: (s: S) => A) => Type2<M, L, A>; | ||
@@ -55,2 +86,12 @@ done: <L>() => Type2<M, L, S>; | ||
}, L>; | ||
sequenceS: <R extends Record<string, Type2<M, L, any>>>(r: EnforceNonEmptyRecord<R> & { | ||
[K in keyof S]?: never; | ||
}) => Do2C<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type2<M, any, infer A>] ? A : never; | ||
}, L>; | ||
sequenceSL: <R extends Record<string, Type2<M, L, any>>>(f: (s: S) => EnforceNonEmptyRecord<R> & { | ||
[K in keyof S]?: never; | ||
}) => Do2C<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type2<M, any, infer A>] ? A : never; | ||
}, L>; | ||
return: <A>(f: (s: S) => A) => Type2<M, L, A>; | ||
@@ -68,2 +109,12 @@ done: () => Type2<M, L, S>; | ||
}>; | ||
sequenceS: <R extends Record<string, Type<M, any>>>(r: EnforceNonEmptyRecord<R> & { | ||
[K in keyof S]?: never; | ||
}) => Do1<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type<M, infer A>] ? A : never; | ||
}>; | ||
sequenceSL: <R extends Record<string, Type<M, any>>>(f: (s: S) => EnforceNonEmptyRecord<R> & { | ||
[K in keyof S]?: never; | ||
}) => Do1<M, S & { | ||
[K in keyof R]: [R[K]] extends [Type<M, infer A>] ? A : never; | ||
}>; | ||
return: <A>(f: (s: S) => A) => Type<M, A>; | ||
@@ -81,2 +132,12 @@ done: () => Type<M, S>; | ||
}>; | ||
sequenceS: <R extends Record<string, HKT<M, unknown>>>(r: EnforceNonEmptyRecord<R> & { | ||
[K in keyof S]?: never; | ||
}) => Do0<M, S & { | ||
[K in keyof R]: [R[K]] extends [HKT<M, infer A>] ? A : never; | ||
}>; | ||
sequenceSL: <R extends Record<string, HKT<M, unknown>>>(f: (s: S) => EnforceNonEmptyRecord<R> & { | ||
[K in keyof S]?: never; | ||
}) => Do0<M, S & { | ||
[K in keyof R]: [R[K]] extends [HKT<M, infer A>] ? A : never; | ||
}>; | ||
return: <A>(f: (s: S) => A) => HKT<M, A>; | ||
@@ -111,1 +172,2 @@ done: () => HKT<M, S>; | ||
export declare function Do<M>(M: Monad<M>): Do0<M, {}>; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Apply_1 = require("fp-ts/lib/Apply"); | ||
var nil = { type: 'Nil', length: 0 }; | ||
@@ -26,2 +27,3 @@ var cons = function (head, tail) { return ({ | ||
this.actions = actions; | ||
this._sequenceS = undefined; | ||
} | ||
@@ -40,2 +42,8 @@ DoClass.prototype.do = function (action) { | ||
}; | ||
DoClass.prototype.sequenceS = function (r) { | ||
return new DoClass(this.M, cons({ type: 'sequenceS', r: r }, this.actions)); | ||
}; | ||
DoClass.prototype.sequenceSL = function (f) { | ||
return new DoClass(this.M, cons({ type: 'sequenceSL', f: f }, this.actions)); | ||
}; | ||
DoClass.prototype.return = function (f) { | ||
@@ -75,4 +83,13 @@ return this.M.map(this.done(), f); | ||
break; | ||
case 'sequenceS': | ||
var _sequenceS_1 = this_1._sequenceS === undefined ? (this_1._sequenceS = Apply_1.sequenceS(M)) : this_1._sequenceS; | ||
result = M.chain(result, function (s) { return M.map(_sequenceS_1(a.r), function (r) { return Object.assign(s, r); }); }); | ||
break; | ||
case 'sequenceSL': | ||
var _sequenceSL_1 = this_1._sequenceS === undefined ? (this_1._sequenceS = Apply_1.sequenceS(M)) : this_1._sequenceS; | ||
result = M.chain(result, function (s) { return M.map(_sequenceSL_1(a.f(s)), function (r) { return Object.assign(s, r); }); }); | ||
break; | ||
} | ||
}; | ||
var this_1 = this; | ||
for (var i = 0; i < len; i++) { | ||
@@ -79,0 +96,0 @@ _loop_1(i); |
@@ -0,5 +1,5 @@ | ||
import { Endomorphism } from 'fp-ts/lib/function'; | ||
import { IO } from 'fp-ts/lib/IO'; | ||
import { Monad2 } from 'fp-ts/lib/Monad'; | ||
import { State } from 'fp-ts/lib/State'; | ||
import { Endomorphism } from 'fp-ts/lib/function'; | ||
declare module 'fp-ts/lib/HKT' { | ||
@@ -6,0 +6,0 @@ interface URI2HKT2<L, A> { |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var array = require("fp-ts/lib/Array"); | ||
var Console_1 = require("fp-ts/lib/Console"); | ||
var function_1 = require("fp-ts/lib/function"); | ||
var IO_1 = require("fp-ts/lib/IO"); | ||
var Ord_1 = require("fp-ts/lib/Ord"); | ||
var Random_1 = require("fp-ts/lib/Random"); | ||
var stateT = require("fp-ts/lib/StateT"); | ||
var function_1 = require("fp-ts/lib/function"); | ||
var stateTIO = stateT.getStateT2v(IO_1.io); | ||
@@ -93,58 +78,1 @@ exports.URI = 'StateIO'; | ||
}; | ||
// | ||
// Usage (adapted from https://wiki.haskell.org/Simple_StateT_use) | ||
// | ||
// Example 1 | ||
/** pop the next unique off the stack */ | ||
var pop = exports.get().chain(function (ns) { | ||
return array.foldL(ns, function () { return of(0); }, function (h, t) { return exports.put(t).chain(function () { return of(h); }); }); | ||
}); | ||
var program1 = pop | ||
.chain(function (x) { return exports.fromIO(Console_1.log(x)); }) | ||
.chain(function () { return pop; }) | ||
.chain(function (y) { return exports.fromIO(Console_1.log(y)); }) | ||
.chain(function () { return of(undefined); }); | ||
program1.run([1, 2, 3]); | ||
// => 1 | ||
// => 2 | ||
// Example 2: a guessing game | ||
function readLine(s) { | ||
return new IO_1.IO(function () { return require('readline-sync').question(s); }); | ||
} | ||
function guessSession(answer) { | ||
return exports.fromIO(readLine('')).chain(function (gs) { | ||
var g = parseInt(gs, 10); | ||
return exports.modify(function (s) { return s + 1; }).chain(function () { | ||
switch (Ord_1.ordNumber.compare(g, answer)) { | ||
case -1: | ||
return exports.fromIO(Console_1.log('Too low')).chain(function () { return guessSession(answer); }); | ||
case 1: | ||
return exports.fromIO(Console_1.log('Too high')).chain(function () { return guessSession(answer); }); | ||
case 0: | ||
return exports.fromIO(Console_1.log('Got it!')); | ||
} | ||
}); | ||
}); | ||
} | ||
var program2 = Random_1.randomInt(1, 100).chain(function (answer) { | ||
return Console_1.log("I'm thinking of a number between 1 and 100, can you guess it? ").chain(function () { | ||
var guesses = guessSession(answer).exec(0); | ||
return Console_1.log("Success in " + guesses + " tries."); | ||
}); | ||
}); | ||
program2.run(); | ||
var s1 = [exports.gets(function (s) { return s.var1; }), function (var1) { return exports.modify(function (vars) { return (__assign({}, vars, { var1: var1 })); }); }]; | ||
var s2 = [exports.gets(function (s) { return s.var2; }), function (var2) { return exports.modify(function (vars) { return (__assign({}, vars, { var2: var2 })); }); }]; | ||
function sel(selector) { | ||
return selector[0]; | ||
} | ||
function mods(_a, mfun) { | ||
var gf = _a[0], uf = _a[1]; | ||
return gf.chain(function (st) { return uf(mfun(st)); }); | ||
} | ||
var program3 = sel(s1) | ||
.chain(function (a) { return mods(s2, function (n) { return n * a; }); }) | ||
.chain(function () { return sel(s2); }) | ||
.chain(function (b) { return exports.fromIO(Console_1.log(b)); }); | ||
program3.run({ var1: 2, var2: 1.3 }); | ||
// => 2.6 |
{ | ||
"name": "fp-ts-contrib", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A community driven utility package for fp-ts", | ||
@@ -33,4 +33,4 @@ "files": [ | ||
"homepage": "https://github.com/gcanti/fp-ts-contrib", | ||
"dependencies": { | ||
"fp-ts": "^1.14.3" | ||
"peerDependencies": { | ||
"fp-ts": "^1.15.1" | ||
}, | ||
@@ -45,2 +45,3 @@ "devDependencies": { | ||
"dtslint": "github:gcanti/dtslint", | ||
"fp-ts": "^1.17.2", | ||
"jest": "^23.6.0", | ||
@@ -47,0 +48,0 @@ "mocha": "^5.2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76719
1781
17
- Removedfp-ts@^1.14.3