New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

lambda-math

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lambda-math - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

src/sigma.js

2

package-lock.json
{
"name": "lambda-math",
"version": "0.0.7",
"version": "0.0.8",
"lockfileVersion": 1,

@@ -5,0 +5,0 @@ "requires": true,

{
"name": "lambda-math",
"version": "0.0.7",
"version": "0.0.8",
"description": "Pseudo lambda expressions for JS arbitrary-precision arithmetic operations.",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -19,6 +19,6 @@ # Lambda math

```
const { div, add, λ, _ } = require('lambda-math');
const { div, add, λ, Σ } = require('lambda-math');
λ( div, [300, 293] )
( add, [λ[0], λ[0]], [_, λ[0]], 70 );
( add, [λ[0], λ[0]], [Σ, λ[0]], 70 );

@@ -57,4 +57,152 @@ console.log(λ[1].number); // 73.72013651877133

## Library API
The library `lambda-math` exports the symbols `λ` and `Σ`, along with a number of mathematical functions. At the moment there are just 4 arithmetic functions available. Addition, subtraction, multiplication, and division:
```
c = add(a, b) // same as: a + b
c = sub(a, b) // same as: a - b
c = mul(a, b) // same as: a * b
c = div(a, b) // same as: a / b
```
These 4 functions accept either JavaScript `number` or a `BigNumber` as parameters (can mix either way).
While you can use these functions directly, what you want to do is use them via the `λ` function. The `λ` function expects several parameters. The first parameter is the math function to be applied. It can be one of the above 4 functions. The 2nd, 3rd, etc. params must be arrays containing the numbers that will be passed to each subsequent call of the math function.
Optionally, you can pass to `λ` a simple JavaScript number as the last param. It will indicate how many times the last math function needs to be called with the last set of params. You should think of this as a `for loop`.
Additionally, besides numbers, any of the parameter arrays can contain the symbol `Σ`. You can use the symbol `Σ` to tell `lambda-math` to substitute the result of the last operation as a param to a math function call. You should think of this as a `variable`.
Some examples to better demonstrate these concepts:
### Example 1
```
λ(add, [1, 2]);
console.log(λ[0].number); // 3
// The same as:
let c = add(1, 2);
console.log(c.toNumber()); // 3
```
### Example 2
```
λ(add, [3, 4], [5, 6]);
console.log(λ[0].number); // 11
// Is the same as:
let c = add(3, 4);
c = add(5, 6);
console.log(c.toNumber()); // 11
```
### Example 3
```
λ(add, [3, 4], [Σ, 6]);
console.log(λ[0].number); // 13
// Is the same as:
let c = add(3, 4);
c = add(c, 6);
console.log(c.toNumber()); // 13
```
### Example 4
```
λ(add, [3, 4], 10);
console.log(λ[0].number); // 7
// Is the same as:
let c;
for (let i = 0; i < 10; i += 1) {
c = add(3, 4);
}
console.log(c.toNumber()); // 7
```
### Example 5
```
λ(add, [3, 4], [Σ, 1], 10);
console.log(λ[0].number); // 17
// Is the same as:
let c = add(3, 4);
for (let i = 0; i < 10; i += 1) {
c = add(c, 1);
}
console.log(c.toNumber()); // 17
```
Besides using `λ` as a function, you can also access the results of each invocation of the function via the array index, starting from 0. So first invocation of `λ` will store the result as `λ[0]`, second invocation as `λ[1]`, and so on. For convenience, `λ[i].number` will contain the JavaScript `number` result value, and `λ[i]` will contain the `BigNumber` result value.
### Example 6
```
λ(add, [1, 2]);
λ(add, [3, 4]);
λ(add, [5, 6]);
console.log(λ[0].number); // 3
console.log(λ[1].number); // 7
console.log(λ[2].number); // 11
```
You can also chain any number of calls to `λ`, and this will not have any affect on your program:
### Example 7
```
λ(add, [1, 2])
(add, [3, 4])
(add, [5, 6]);
console.log(λ[0].number); // 3
console.log(λ[1].number); // 7
console.log(λ[2].number); // 11
```
This is possible due to the fact that an invocation of `λ` returns an instance of itself ;)
Last, but not least, `λ.reset()` is available to clear all `lambda-math` state, and reset the results stack to zero.
### Example 8
```
λ(add, [1, 2])
(add, [3, 4])
(add, [5, 6]);
console.log(λ[0].number); // 3
console.log(λ[1].number); // 7
console.log(λ[2].number); // 11
console.log(λ[3]); // undefined
console.log(λ[4]); // undefined
console.log(λ[5]); // undefined
λ.reset();
λ(add, [10, 20])
(add, [30, 40])
(add, [50, 60]);
console.log(λ[0].number); // 30
console.log(λ[1].number); // 70
console.log(λ[2].number); // 110
console.log(λ[3]); // undefined
console.log(λ[4]); // undefined
console.log(λ[5]); // undefined
```
## Running tests
Clone this repo, do `npm install`, followed by `npm run test`.
## License
See [LICENSE](LICENSE) for more details.
const { λ } = require('./lambda');
const { _ } = require('./underscore');
const { Σ } = require('./sigma');
const { add, sub, mul, div } = require('./math');
module.exports = { λ, _, add, sub, mul, div };
module.exports = { λ, Σ, add, sub, mul, div };
const BigNumber = require('bignumber.js');
const { _ } = require('./underscore');
const { Σ } = require('./sigma');
const { add, sub, mul, div } = require('./math');

@@ -65,5 +65,5 @@

(typeof param === 'undefined' || param === null || Number.isNaN(param) === true) ||
(typeof param !== 'number' && param.constructor !== BigNumber && param !== _)
(typeof param !== 'number' && param.constructor !== BigNumber && param !== Σ)
) {
throw new TypeError(`Error! Array item must be a number, a BigNumber, or "_".`);
throw new TypeError(`Error! Array item must be a number, a BigNumber, or "Σ".`);
}

@@ -79,3 +79,3 @@ });

args.forEach(function(arg, idx) {
if (args[idx] === _) {
if (args[idx] === Σ) {
args[idx] = λ[λ_call_count];

@@ -96,3 +96,3 @@ }

args.forEach(function(arg, idx) {
if (args[idx] === _) {
if (args[idx] === Σ) {
args[idx] = λ[λ_call_count];

@@ -99,0 +99,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc