CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-prelude-ls

A functionally oriented utility library for LiveScript with curried functions for lists, objects, strings, numbers, and function composition.

89

1.53x
Overview
Eval results
Files

mathematical-operations.mddocs/

Mathematical Operations

Comprehensive mathematical functions including basic arithmetic, trigonometry, logarithmic operations, and number property testing. The Number module provides 29 functions covering all essential mathematical operations.

Capabilities

Basic Mathematical Operations

Core arithmetic and comparison functions.

/**
 * Returns larger of two numbers
 * @param {number} a - First number
 * @param {number} b - Second number
 * @returns {number} Maximum of a and b
 */
function max(a, b);

/**
 * Returns smaller of two numbers
 * @param {number} a - First number
 * @param {number} b - Second number
 * @returns {number} Minimum of a and b
 */
function min(a, b);

/**
 * Negates number
 * @param {number} n - Number to negate
 * @returns {number} Negative of n
 */
function negate(n);

/**
 * Absolute value (Math.abs)
 * @param {number} n - Number to get absolute value of
 * @returns {number} Absolute value of n
 */
function abs(n);

/**
 * Sign function (-1, 0, or 1)
 * @param {number} n - Number to get sign of
 * @returns {number} -1 if negative, 0 if zero, 1 if positive
 */
function signum(n);

Division Operations

Specialized division functions for different mathematical needs.

/**
 * Integer division (truncates toward zero)
 * @param {number} a - Dividend
 * @param {number} b - Divisor
 * @returns {number} Integer quotient truncated toward zero
 */
function quot(a, b);

/**
 * Remainder operation (sign follows dividend)
 * @param {number} a - Dividend
 * @param {number} b - Divisor
 * @returns {number} Remainder of a/b
 */
function rem(a, b);

/**
 * Integer division (floors result)
 * @param {number} a - Dividend
 * @param {number} b - Divisor
 * @returns {number} Integer quotient floored
 */
function div(a, b);

/**
 * Modulo operation (always positive result)
 * @param {number} a - Dividend
 * @param {number} b - Divisor
 * @returns {number} Positive remainder of a/b
 */
function mod(a, b);

/**
 * Reciprocal (1/x)
 * @param {number} x - Number to get reciprocal of
 * @returns {number} 1/x
 */
function recip(x);

Mathematical Constants

Important mathematical constants.

/**
 * Math.PI constant (π ≈ 3.14159)
 * @type {number}
 */
const pi = Math.PI;

/**
 * Tau constant (2π ≈ 6.28318)
 * @type {number}
 */
const tau = 2 * Math.PI;

Exponential and Logarithmic Functions

Power, exponential, and logarithmic operations.

/**
 * e^x (Math.exp)
 * @param {number} x - Exponent
 * @returns {number} e raised to power x
 */
function exp(x);

/**
 * Square root (Math.sqrt)
 * @param {number} x - Number to get square root of
 * @returns {number} Square root of x
 */
function sqrt(x);

/**
 * Natural logarithm (Math.log)
 * @param {number} x - Number to get natural log of
 * @returns {number} Natural logarithm of x
 */
function ln(x);

/**
 * Exponentiation (Math.pow)
 * @param {number} base - Base number
 * @param {number} exponent - Exponent
 * @returns {number} Base raised to exponent
 */
function pow(base, exponent);

Trigonometric Functions

Complete set of trigonometric and inverse trigonometric functions.

/**
 * Sine (Math.sin)
 * @param {number} x - Angle in radians
 * @returns {number} Sine of x
 */
function sin(x);

/**
 * Tangent (Math.tan)
 * @param {number} x - Angle in radians
 * @returns {number} Tangent of x
 */
function tan(x);

/**
 * Cosine (Math.cos)
 * @param {number} x - Angle in radians
 * @returns {number} Cosine of x
 */
function cos(x);

/**
 * Arcsine (Math.asin)
 * @param {number} x - Value between -1 and 1
 * @returns {number} Arcsine of x in radians
 */
function asin(x);

/**
 * Arccosine (Math.acos)
 * @param {number} x - Value between -1 and 1
 * @returns {number} Arccosine of x in radians
 */
function acos(x);

/**
 * Arctangent (Math.atan)
 * @param {number} x - Any number
 * @returns {number} Arctangent of x in radians
 */
function atan(x);

/**
 * Two-argument arctangent (Math.atan2)
 * @param {number} y - Y coordinate
 * @param {number} x - X coordinate
 * @returns {number} Angle from x-axis to point (x,y) in radians
 */
function atan2(y, x);

Rounding Functions

Functions for different types of number rounding.

/**
 * Truncates to integer (removes decimal part)
 * @param {number} n - Number to truncate
 * @returns {number} Integer part of n
 */
function truncate(n);

/**
 * Rounds to nearest integer (Math.round)
 * @param {number} n - Number to round
 * @returns {number} Nearest integer to n
 */
function round(n);

/**
 * Rounds up to next integer (Math.ceil)
 * @param {number} n - Number to round up
 * @returns {number} Smallest integer ≥ n
 */
function ceiling(n);

/**
 * Rounds down to previous integer (Math.floor)
 * @param {number} n - Number to round down
 * @returns {number} Largest integer ≤ n
 */
function floor(n);

Number Properties and Testing

Functions for testing number properties.

/**
 * Checks if value is NaN
 * @param {*} value - Value to test
 * @returns {boolean} True if value is NaN
 */
function isItNaN(value);

/**
 * Tests if number is even
 * @param {number} n - Integer to test
 * @returns {boolean} True if n is even
 */
function even(n);

/**
 * Tests if number is odd
 * @param {number} n - Integer to test
 * @returns {boolean} True if n is odd
 */
function odd(n);

/**
 * Greatest common divisor
 * @param {number} a - First integer
 * @param {number} b - Second integer
 * @returns {number} Greatest common divisor of a and b
 */
function gcd(a, b);

/**
 * Least common multiple
 * @param {number} a - First integer
 * @param {number} b - Second integer
 * @returns {number} Least common multiple of a and b
 */
function lcm(a, b);

Usage Examples

Basic Arithmetic:

const { max, min, abs, signum } = require('prelude-ls');

const numbers = [5, -3, 8, -12, 0];

const maximum = max(5, 8);                // 8
const minimum = min(-3, -12);             // -12

const absolute = abs(-12);                // 12
const sign = signum(-5);                  // -1

// Finding max/min in arrays (with apply or reduce)
const arrayMax = numbers.reduce(max);     // 8
const arrayMin = numbers.reduce(min);     // -12

Division Operations:

const { quot, rem, div, mod } = require('prelude-ls');

const a = 17, b = 5;

const quotient = quot(a, b);              // 3 (17 ÷ 5, truncated)
const remainder = rem(a, b);              // 2 (17 % 5)

const division = div(a, b);               // 3 (17 ÷ 5, floored)
const modulo = mod(a, b);                 // 2 (17 mod 5)

// Difference with negative numbers
const negA = -17, negB = 5;
const quotNeg = quot(negA, negB);         // -3 (truncated toward zero)
const divNeg = div(negA, negB);           // -4 (floored)
const remNeg = rem(negA, negB);           // -2 (sign follows dividend)
const modNeg = mod(negA, negB);           // 3 (always positive)

Trigonometry:

const { sin, cos, tan, pi, tau } = require('prelude-ls');

// Basic trigonometric functions
const angle = pi / 4;                     // 45 degrees in radians

const sine = sin(angle);                  // √2/2 ≈ 0.707
const cosine = cos(angle);                // √2/2 ≈ 0.707
const tangent = tan(angle);               // 1

// Full circle calculations
const fullCircle = tau;                   // 2π ≈ 6.283
const halfCircle = pi;                    // π ≈ 3.142

// Common angles
const angles = [0, pi/6, pi/4, pi/3, pi/2];
const sines = angles.map(sin);
const cosines = angles.map(cos);

Exponential and Logarithmic:

const { exp, ln, sqrt, pow } = require('prelude-ls');

const base = 2;
const exponent = 3;

const power = pow(base, exponent);        // 8 (2³)
const squareRoot = sqrt(16);              // 4
const naturalExp = exp(1);                // e ≈ 2.718
const naturalLog = ln(naturalExp);        // 1

// Compound calculations
const compound = exp(ln(5) * 2);          // 25 (equivalent to 5²)
const logBase = (base, n) => ln(n) / ln(base);
const log2of8 = logBase(2, 8);            // 3

Rounding Operations:

const { round, floor, ceiling, truncate } = require('prelude-ls');

const number = 3.7;
const negative = -3.7;

const rounded = round(number);            // 4
const floored = floor(number);            // 3
const ceiled = ceiling(number);           // 4
const truncated = truncate(number);       // 3

// With negative numbers
const roundedNeg = round(negative);       // -4
const flooredNeg = floor(negative);       // -4
const ceiledNeg = ceiling(negative);      // -3
const truncatedNeg = truncate(negative);  // -3

Number Properties:

const { even, odd, gcd, lcm, isItNaN } = require('prelude-ls');

const numbers = [12, 15, 18, 21];

const evens = numbers.filter(even);       // [12, 18]
const odds = numbers.filter(odd);         // [15, 21]

const greatestCommon = gcd(12, 18);       // 6
const leastCommon = lcm(12, 18);          // 36

// NaN checking
const validNumbers = [1, 2, NaN, 4].filter(n => !isItNaN(n)); // [1, 2, 4]

Curried Mathematical Operations:

const { max, min, pow, mod } = require('prelude-ls');

// Create reusable functions
const atLeast = min(0);                   // Ensures non-negative
const atMost = max(100);                  // Caps at 100
const square = pow(2);                    // Square function (note: arguments flipped)
const isMultipleOf5 = n => mod(n, 5) === 0;

const numbers = [-5, 25, 150, 200];
const clamped = numbers.map(n => atMost(atLeast(n))); // [0, 25, 100, 100]
const multiples = numbers.filter(isMultipleOf5);     // [25, 150, 200]

Advanced Mathematical Computations:

const { sin, cos, pow, sqrt, pi } = require('prelude-ls');

// Distance formula
const distance = (x1, y1, x2, y2) => 
  sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

const dist = distance(0, 0, 3, 4);        // 5

// Polar to Cartesian conversion
const polarToCartesian = (r, theta) => ({
  x: r * cos(theta),
  y: r * sin(theta)
});

const point = polarToCartesian(5, pi / 3); // {x: 2.5, y: ≈4.33}

Statistical Calculations:

const { sqrt, pow, abs } = require('prelude-ls');

const standardDeviation = (numbers) => {
  const mean = numbers.reduce((a, b) => a + b) / numbers.length;
  const variance = numbers
    .map(n => pow(n - mean, 2))
    .reduce((a, b) => a + b) / numbers.length;
  return sqrt(variance);
};

const data = [10, 12, 23, 23, 16, 23, 21, 16];
const stdDev = standardDeviation(data);    // ≈5.237

Install with Tessl CLI

npx tessl i tessl/npm-prelude-ls

docs

function-utilities.md

index.md

list-operations.md

mathematical-operations.md

object-operations.md

string-processing.md

tile.json