本文整理汇总了TypeScript中lodash.sum函数的典型用法代码示例。如果您正苦于以下问题:TypeScript sum函数的具体用法?TypeScript sum怎么用?TypeScript sum使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sum函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: linear
export function linear(...as: [number, number][]): LinearResult {
if (!as.length) {
return {
b: undefined,
m: undefined,
fn: () => undefined,
r2: undefined
}
}
const [xs, ys] = unzip(as)
const [sum_x, sum_y] = [xs, ys].map(sum)
const sum_x2 = sum(xs.map(_ => _*_))
const sum_xy = sum(as.map(([a, b]) => a * b))
const n = as.length
const mean_y = mean(ys)
const m = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x)
const b = (sum_y - m * sum_x) / n
const fn = (x: number) => m*x + b
// @see https://en.wikipedia.org/wiki/Coefficient_of_determination
const ss_res = sum(as.map(([x, y]) => square(y - fn(x))))
const ss_tot = sum(ys.map(y => square(y - mean_y)))
const r2 = 1 - ss_res/ss_tot
return {b, m, fn, r2}
}
示例2:
Array.from(ratingMap.values()).filter(mappedRatings => mappedRatings.length > 1).forEach(dupeRatings => {
// update first
const first = dupeRatings.shift();
queries.push(first.update({ value: Math.round(sum(dupeRatings.map((r: RatingDocument) => r.value)) / dupeRatings.length) }));
// delete the rest
dupeRatings.forEach((r: RatingDocument) => queries.push(r.remove()));
});
示例3: del
/**
* Deletes a game.
*
* @see DELETE /v1/games/:id
* @param {Context} ctx Koa context
*/
public async del(ctx: Context) {
const game = await state.models.Game.findOne({ id: sanitize(ctx.params.id) })
.populate({ path: '_backglass' })
.populate({ path: '_logo' })
.exec();
if (!game) {
throw new ApiError('No such game with ID "%s".', ctx.params.id).status(404);
}
// check for release and backglass reference and fail if there are
const refs: { [key: string]: number } = {
releases: await state.models.Release.countDocuments({ _game: game._id }).exec(),
backglasses: await state.models.Backglass.countDocuments({ _game: game._id }).exec(),
};
if (sum(values(refs)) > 0) {
throw new ApiError('Cannot delete game because it is referenced by %s.', Object.keys(refs).map(f => `${refs[f]} ${f}`).join(' and '))
.status(400).warn();
}
await game.remove();
logger.info(ctx.state, '[GameApi.del] Game "%s" (%s) successfully deleted.', game.title, game.id);
await apiCache.invalidateDeletedGame(ctx.state, game);
// log event
await LogEventUtil.log(ctx, 'delete_game', false, { game: omit(state.serializers.Game.simple(ctx, game), ['rating', 'counter']) }, { game: game._id });
this.success(ctx, null, 204);
}
示例4: evaluate
export default function evaluate(situation: Situation, camp: Camp): number {
return _.sum(situation.getSlots().map( (chess, index) => {
if (chess && chess.camp == camp) {
return rules.of(chess).getScore(situation, index);
} else {
return 0;
}
}));
}
示例5: function
this.getActiveAddresses(wallet, inGap, reportFn, function(err, addresses) {
reportFn("Active addresses:" + JSON.stringify(addresses));
if (err) return cb(err);
var utxos = _.map(_.flatten(_.map(addresses, "utxo")),"amount");
var result = {
addresses: addresses,
balance: _.sum(utxos),
}
return cb(null, result);
});
示例6: refresh
refresh() {
this.playerCardsStrengths = this.ChanceCalculations.computeStrengths(this.currentDeck);
this.strengthPercentageValues = this.playerCardsStrengths.map(strength => this.ChanceCalculations.computeStrengthPercentageValue(strength, this.Game.stack));
let nonZeroChances = _.compact(this.strengthPercentageValues).length;
if (nonZeroChances !== 0) {
this.totalChance = _.sum(this.strengthPercentageValues) / nonZeroChances;
} else {
this.totalChance = 0;
}
}
示例7: Date
return customers.map(customer => {
const metadata = (customer.charges[0] && customer.charges[0].metadata) || customer.subscriptions.data[0] && customer.subscriptions.data[0].metadata
return {
email: customer.email,
name: metadata && metadata.name,
showOnList: metadata && metadata.showOnList,
isMonthly: customer.subscriptions.data.length > 0,
created: (new Date(customer.created * 1000)).toISOString(),
total: sum(customer.charges.map(charge => charge.amount)) / 100
}
})
示例8: runPing
async function runPing(url: string): Promise<number> {
const pingUrl = async () => {
const start = Date.now()
await fetch(url)
return Date.now() - start
}
const pings = await Promise.all([0,0].map(pingUrl))
return sum(pings) / pings.length
}
示例9: recalculateStats
recalculateStats() {
this.maxMembers = 10 + (this.buildings.levels.Academy || 0);
const numBuildings = _.size(Buildings);
const totalLevel = _.sum(_.values(this.buildings.levels));
this.level = Math.max(1, Math.floor(totalLevel / numBuildings));
this.$statBoosts = {};
if(this.$buildingInstances.GardenSmall) {
const smallGardenLevel = this.buildings.levels.GardenSmall;
const smallGardenBoost1 = this.getProperty('GardenSmall', 'StatBoost1');
this.$statBoosts[smallGardenBoost1] = smallGardenLevel;
}
if(this.$buildingInstances.GardenMedium) {
const mediumGardenLevel = this.buildings.levels.GardenMedium;
const mediumGardenBoost1 = this.getProperty('GardenMedium', 'StatBoost1');
const mediumGardenBoost2 = this.getProperty('GardenMedium', 'StatBoost2');
this.$statBoosts[mediumGardenBoost1] = mediumGardenLevel * 20;
let val = 0;
switch(mediumGardenBoost2) {
case 'gold': val = 10; break;
case 'xp': val = 2; break;
case 'itemFindRangeMultiplier': val = 0.05; break;
case 'salvage': val = 1; break;
}
this.$statBoosts[mediumGardenBoost2] = mediumGardenLevel * val;
}
if(this.$buildingInstances.GardenLarge) {
const largeGardenLevel = this.buildings.levels.GardenLarge;
const largeGardenBoost1 = this.getProperty('GardenLarge', 'StatBoost1');
const largeGardenBoost2 = this.getProperty('GardenLarge', 'StatBoost2');
const largeGardenBoost3 = this.getProperty('GardenLarge', 'StatBoost3');
this.$statBoosts[largeGardenBoost1] = largeGardenLevel;
this.$statBoosts[largeGardenBoost2] = largeGardenLevel;
let val = 0;
switch(largeGardenBoost3) {
case 'hp': case 'mp': val = 1000; break;
case 'hpregen': case 'mpregen': val = 200; break;
case 'damageReduction': val = 100; break;
}
this.$statBoosts[largeGardenBoost3] = largeGardenLevel * val;
}
}
示例10: function
return function (engine, locale) {
function pick(n: number, [[k, x], ...xs]): any {
return (n <= k) ? x : pick(n - k, xs);
}
const total = sum(pool.map(e => e[0]));
const head = integer(1, total)(engine);
const result = pick(head, pool);
if (result instanceof Arbitrary) {
return result.makeGenerator()(engine, locale);
}
else {
return result;
};
};