import { tuple } from './function'; import * as _ from './internal'; export function ap(F, G) { return function (fa) { return function (fab) { return F.ap(F.map(fab, function (gab) { return function (ga) { return G.ap(gab, ga); }; }), fa); }; }; } export function apFirst(A) { return function (second) { return function (first) { return A.ap(A.map(first, function (a) { return function () { return a; }; }), second); }; }; } export function apSecond(A) { return function (second) { return function (first) { return A.ap(A.map(first, function () { return function (b) { return b; }; }), second); }; }; } export function apS(F) { return function (name, fb) { return function (fa) { return F.ap(F.map(fa, function (a) { return function (b) { var _a; return Object.assign({}, a, (_a = {}, _a[name] = b, _a)); }; }), fb); }; }; } export function getApplySemigroup(F) { return function (S) { return ({ concat: function (first, second) { return F.ap(F.map(first, function (x) { return function (y) { return S.concat(x, y); }; }), second); } }); }; } function curried(f, n, acc) { return function (x) { var combined = Array(acc.length + 1); for (var i = 0; i < acc.length; i++) { combined[i] = acc[i]; } combined[acc.length] = x; return n === 0 ? f.apply(null, combined) : curried(f, n - 1, combined); }; } var tupleConstructors = { 1: function (a) { return [a]; }, 2: function (a) { return function (b) { return [a, b]; }; }, 3: function (a) { return function (b) { return function (c) { return [a, b, c]; }; }; }, 4: function (a) { return function (b) { return function (c) { return function (d) { return [a, b, c, d]; }; }; }; }, 5: function (a) { return function (b) { return function (c) { return function (d) { return function (e) { return [a, b, c, d, e]; }; }; }; }; } }; function getTupleConstructor(len) { if (!_.has.call(tupleConstructors, len)) { tupleConstructors[len] = curried(tuple, len - 1, []); } return tupleConstructors[len]; } export function sequenceT(F) { return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var len = args.length; var f = getTupleConstructor(len); var fas = F.map(args[0], f); for (var i = 1; i < len; i++) { fas = F.ap(fas, args[i]); } return fas; }; } function getRecordConstructor(keys) { var len = keys.length; switch (len) { case 1: return function (a) { var _a; return (_a = {}, _a[keys[0]] = a, _a); }; case 2: return function (a) { return function (b) { var _a; return (_a = {}, _a[keys[0]] = a, _a[keys[1]] = b, _a); }; }; case 3: return function (a) { return function (b) { return function (c) { var _a; return (_a = {}, _a[keys[0]] = a, _a[keys[1]] = b, _a[keys[2]] = c, _a); }; }; }; case 4: return function (a) { return function (b) { return function (c) { return function (d) { var _a; return (_a = {}, _a[keys[0]] = a, _a[keys[1]] = b, _a[keys[2]] = c, _a[keys[3]] = d, _a); }; }; }; }; case 5: return function (a) { return function (b) { return function (c) { return function (d) { return function (e) { var _a; return (_a = {}, _a[keys[0]] = a, _a[keys[1]] = b, _a[keys[2]] = c, _a[keys[3]] = d, _a[keys[4]] = e, _a); }; }; }; }; }; default: return curried(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var r = {}; for (var i = 0; i < len; i++) { r[keys[i]] = args[i]; } return r; }, len - 1, []); } } export function sequenceS(F) { return function (r) { var keys = Object.keys(r); var len = keys.length; var f = getRecordConstructor(keys); var fr = F.map(r[keys[0]], f); for (var i = 1; i < len; i++) { fr = F.ap(fr, r[keys[i]]); } return fr; }; }