@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
56 lines (55 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.plookup = void 0;
var type_system_1 = require("../../../../type_system/index.js");
var tyArgs_1 = require("../../../../type_system/tyArgs/index.js");
var getDirectFstT_1 = require("../../../../type_system/tyArgs/getDirectFstT.js");
var getDirectSndT_1 = require("../../../../type_system/tyArgs/getDirectSndT.js");
var bool_1 = require("../../../builtins/bool/index.js");
var data_1 = require("../../../builtins/data/index.js");
var noUnwrap_1 = require("../../../builtins/pair/noUnwrap.js");
var pdelay_1 = require("../../../pdelay.js");
var pfn_1 = require("../../../pfn.js");
var phoist_1 = require("../../../phoist.js");
var plet_1 = require("../../../plet/index.js");
var PMaybe_1 = require("../../PMaybe/index.js");
var data_2 = require("../../data/index.js");
var precursiveList_1 = require("../precursiveList/index.js");
function plookup(kT, vT) {
kT = (0, tyArgs_1.unwrapAsData)(kT);
vT = (0, tyArgs_1.unwrapAsData)(vT);
var PMaybeVal = (0, PMaybe_1.PMaybe)(vT);
var elems_t = (0, type_system_1.pair)(kT, vT);
// pair call ususally adds `asData`
// taking args from pair type in case it ever changes
var fstT = (0, getDirectFstT_1.getDirectFstT)(elems_t);
var sndT = (0, getDirectSndT_1.getDirectSndT)(elems_t);
var pfst = (0, noUnwrap_1.pfstPairNoUnwrap)(fstT, sndT);
var psnd = (0, noUnwrap_1.psndPairNoUnwrap)(fstT, sndT);
var pvalueToData = (0, data_2.toData)(sndT);
var hoistedBody = (0, phoist_1.phoist)((0, pfn_1.pfn)([
(0, type_system_1.asData)(kT),
], (0, type_system_1.lam)((0, type_system_1.list)(elems_t), PMaybeVal.type))(function (searchElem) {
return (0, plet_1.plet)(data_1.peqData.$(searchElem)).in(function (isKey) {
return (0, precursiveList_1.precursiveList)(PMaybeVal.type, elems_t)
.$(function (_self) { return (0, pdelay_1.pdelay)(PMaybeVal.Nothing({})); })
.$(function (self, el, rest) {
return (0, bool_1.pif)(PMaybeVal.type)
.$(isKey.$(pfst.$(el)))
.then(PMaybeVal.Just({
val: pvalueToData(psnd.$(el))
}))
.else(self.$(rest));
});
});
}, "plookup"));
if ((0, type_system_1.typeExtends)(kT, type_system_1.data)) {
return hoistedBody;
}
return (0, phoist_1.phoist)((0, pfn_1.pfn)([
kT
], (0, type_system_1.lam)((0, type_system_1.list)(elems_t), PMaybeVal.type))(function (searchElem) {
return hoistedBody.$((0, data_2.toData)(kT)(searchElem));
}, "plookup::" + (0, type_system_1.termTypeToString)(kT)));
}
exports.plookup = plookup;