lambda-math
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "lambda-math", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Pseudo lambda expressions for JS arbitrary-precision arithmetic operations.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -68,3 +68,3 @@ # Lambda math | ||
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: | ||
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: | ||
@@ -86,2 +86,4 @@ ``` | ||
The function `Ω` can be used to easily index any of the results produced by the `λ` function. This is helpful when the number of invocation of `λ` is dynamic and you don't want to keep track of the index of last result. | ||
Some examples follow below to better demonstrate these concepts. | ||
@@ -188,2 +190,51 @@ | ||
If you want to quickly reference some of the latest `λ` results, but don't want to store the invocation number in a variable, `lambda-math` provides the `Ω` function. This function has two goals. | ||
Firstly, if you call it without any parameter, it will return the number of times `λ` has been called up to this point. I.e. the number of results available. For example: | ||
``` | ||
λ(add, [1, 2]) | ||
(add, [3, 4]) | ||
(add, [5, 6]) | ||
(add, [7, 8]); | ||
console.log(Ω()); // 4 | ||
``` | ||
Secondly, if you pass an integer, you will get the `Nth` result from the start or from the end, depending on the sign of the integer passed. For example: | ||
``` | ||
λ( add, [1, 2] ) | ||
( add, [3, 4] ) | ||
( add, [5, 6] ) | ||
( add, [7, 8] ) | ||
( add, [9, 10] ); | ||
console.log(Ω(1).number); // 19 | ||
console.log(Ω(2).number); // 15 | ||
console.log(Ω(3).number); // 11 | ||
console.log(Ω(4).number); // 7 | ||
console.log(Ω(5).number); // 3 | ||
``` | ||
And if you pass negative integers: | ||
``` | ||
λ( add, [1, 2] ) | ||
( add, [3, 4] ) | ||
( add, [5, 6] ) | ||
( add, [7, 8] ) | ||
( add, [9, 10] ); | ||
console.log(Ω(-1).number); // 3 | ||
console.log(Ω(-2).number); // 7 | ||
console.log(Ω(-3).number); // 11 | ||
console.log(Ω(-4).number); // 15 | ||
console.log(Ω(-5).number); // 19 | ||
``` | ||
Please note, passing `0` as a parameter to `Ω` function is undefined behavior, and the library will throw an error. | ||
### Example 9 | ||
Last, but not least, `λ.reset()` is available to clear all `lambda-math` state, and reset the results stack to zero. | ||
@@ -190,0 +241,0 @@ |
@@ -1,5 +0,5 @@ | ||
const { λ } = require('./lambda'); | ||
const { λ, Ω } = require('./lambda'); | ||
const { Σ } = require('./sigma'); | ||
const { add, sub, mul, div } = require('./math'); | ||
module.exports = { λ, Σ, add, sub, mul, div }; | ||
module.exports = { λ, Ω, Σ, add, sub, mul, div }; |
@@ -5,2 +5,3 @@ const BigNumber = require('bignumber.js'); | ||
const { add, sub, mul, div } = require('./math'); | ||
const { isInteger } = require('./utils'); | ||
@@ -119,2 +120,26 @@ let λCallCount = -1; | ||
module.exports = { λ }; | ||
function Ω(idx) { | ||
if (typeof idx === 'undefined') { | ||
return λCallCount + 1; | ||
} else if (typeof idx === 'number') { | ||
if (idx === 0) { | ||
throw new Error('Parameter to Ω() can not be 0.'); | ||
} | ||
if (!isInteger(idx)) { | ||
throw new Error('Parameter to Ω() must be an integer.'); | ||
} | ||
if ((idx < 0) && (Math.abs(idx) <= λCallCount + 1)) { | ||
return λ[Math.abs(idx) - 1]; | ||
} else if ((idx > 0) && (idx <= λCallCount + 1)) { | ||
return λ[λCallCount - (idx - 1)]; | ||
} else { | ||
throw new Error('Parameter to Ω() is out of bounds.'); | ||
} | ||
} else { | ||
throw new TypeError('Parameter to Ω() should be either a number or undefined.'); | ||
} | ||
} | ||
module.exports = { λ, Ω }; |
@@ -28,2 +28,6 @@ const BigNumber = require('bignumber.js'); | ||
module.exports = { verifyFuncParams, convertFuncParams }; | ||
function isInteger(n) { | ||
return n % 1 === 0; | ||
} | ||
module.exports = { verifyFuncParams, convertFuncParams, isInteger }; |
Sorry, the diff of this file is too big to display
90003
2027
276