Install via yarn add locutus and import:
import { prod } from 'locutus/python/math/prod'.
Or with CommonJS: const { prod } = require('locutus/python/math/prod')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
prod([2, 3, 4])
24
2
prod([], 5)
5
3
prod([1.5, 2, 3], 2)
18
Notes
Multiplies all iterable values together and applies the optional start value first.
Here's what our current TypeScript equivalent to Python's math.prod looks like.
typeProdValue = number | string
exportfunctionprod(values: ProdValue[] | unknown, start: ProdValue = 1): ProdValue { // discuss at: https://locutus.io/python/prod/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Multiplies all iterable values together and applies the optional start value first. // example 1: prod([2, 3, 4]) // returns 1: 24 // example 2: prod([], 5) // returns 2: 5 // example 3: prod([1.5, 2, 3], 2) // returns 3: 18
if (!Array.isArray(values)) { thrownewTypeError('prod(): iterable must be an array') }
letresult: ProdValue = start for (const value of values) { result = multiplyProdValues(result, value) }
return result }
functionmultiplyProdValues(left: ProdValue, right: ProdValue): ProdValue { if (typeof left === 'string' && typeof right !== 'string') { returnrepeatString(left, right) }
if (typeof right === 'string' && typeof left !== 'string') { returnrepeatString(right, left) }
if (typeof left !== 'number' || typeof right !== 'number') { thrownewTypeError('prod(): values must be numbers or repeatable strings') }
const leftNumber = left const rightNumber = right
if (Number.isInteger(leftNumber) && Number.isInteger(rightNumber)) { if (leftNumber === 0 || rightNumber === 0) { return0 } if (leftNumber === 1) { return rightNumber } if (rightNumber === 1) { return leftNumber } if (leftNumber === -1) { return -rightNumber } if (rightNumber === -1) { return -leftNumber }
if (!Number.isSafeInteger(leftNumber) || !Number.isSafeInteger(rightNumber)) { thrownewRangeError('prod(): integer multiplication only supports JS safe integers') }
const result = leftNumber * rightNumber if (!Number.isSafeInteger(result)) { thrownewRangeError('prod(): integer multiplication only supports JS safe integers') }
const count = multiplier if (!Number.isInteger(count) || !Number.isSafeInteger(count)) { thrownewTypeError('prod(): string repetition requires a safe integer multiplier') }
return count > 0 ? value.repeat(count) : '' }
exportfunctionprod(values, start = 1) { // discuss at: https://locutus.io/python/prod/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Multiplies all iterable values together and applies the optional start value first. // example 1: prod([2, 3, 4]) // returns 1: 24 // example 2: prod([], 5) // returns 2: 5 // example 3: prod([1.5, 2, 3], 2) // returns 3: 18
if (!Array.isArray(values)) { thrownewTypeError('prod(): iterable must be an array') }
let result = start for (const value of values) { result = multiplyProdValues(result, value) }
return result }
functionmultiplyProdValues(left, right) { if (typeof left === 'string' && typeof right !== 'string') { returnrepeatString(left, right) }
if (typeof right === 'string' && typeof left !== 'string') { returnrepeatString(right, left) }
if (typeof left !== 'number' || typeof right !== 'number') { thrownewTypeError('prod(): values must be numbers or repeatable strings') }
const leftNumber = left const rightNumber = right
if (Number.isInteger(leftNumber) && Number.isInteger(rightNumber)) { if (leftNumber === 0 || rightNumber === 0) { return0 } if (leftNumber === 1) { return rightNumber } if (rightNumber === 1) { return leftNumber } if (leftNumber === -1) { return -rightNumber } if (rightNumber === -1) { return -leftNumber }
if (!Number.isSafeInteger(leftNumber) || !Number.isSafeInteger(rightNumber)) { thrownewRangeError('prod(): integer multiplication only supports JS safe integers') }
const result = leftNumber * rightNumber if (!Number.isSafeInteger(result)) { thrownewRangeError('prod(): integer multiplication only supports JS safe integers') }
return result }
return leftNumber * rightNumber }
functionrepeatString(value, multiplier) { if (typeof multiplier !== 'number') { thrownewTypeError('prod(): string repetition requires a safe integer multiplier') }
const count = multiplier if (!Number.isInteger(count) || !Number.isSafeInteger(count)) { thrownewTypeError('prod(): string repetition requires a safe integer multiplier') }
return count > 0 ? value.repeat(count) : '' }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.