CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-solid-primitives--utils

A bunch of reactive utility types and functions, for building primitives with Solid.js

Pending
Overview
Eval results
Files

math-operations.mddocs/

Mathematical Operations

Immutable mathematical functions for basic arithmetic operations, designed to work well with functional programming patterns and signal-based reactive systems. These functions support both numeric and string operations where applicable.

Capabilities

Basic Arithmetic

Core mathematical operations for performing calculations without side effects.

/**
 * Addition: a + b + c + ...
 * Supports both numbers and strings
 */
function add(...a: number[]): number;
function add(...a: string[]): string;

/**
 * Subtraction: a - b - c - ...
 * Note: Function name is misspelled in source code as "substract"
 * @param a - Initial value
 * @param b - Values to subtract
 * @returns Result of subtraction
 */
function substract(a: number, ...b: number[]): number;

/**
 * Multiplication: a * b * c * ...
 * @param a - Initial value
 * @param b - Values to multiply by
 * @returns Result of multiplication
 */
function multiply(a: number, ...b: number[]): number;

/**
 * Division: a / b / c / ...
 * @param a - Initial value
 * @param b - Values to divide by
 * @returns Result of division
 */
function divide(a: number, ...b: number[]): number;

/**
 * Exponentiation: a ** b ** c ** ...
 * @param a - Base value
 * @param b - Exponent values
 * @returns Result of exponentiation
 */
function power(a: number, ...b: number[]): number;

Usage Examples:

import { createSignal } from "solid-js";
import { add, substract, multiply, divide, power } from "@solid-primitives/utils/immutable";

// Basic arithmetic operations
const sum = add(1, 2, 3, 4); // 10
const stringConcat = add("Hello", " ", "World"); // "Hello World"
const difference = substract(100, 10, 5); // 85
const product = multiply(2, 3, 4); // 24
const quotient = divide(100, 2, 5); // 10
const exponent = power(2, 3, 2); // 2 ** 3 ** 2 = 512

// With reactive signals
const [values, setValues] = createSignal([1, 2, 3, 4, 5]);
const [multiplier, setMultiplier] = createSignal(2);

// Calculate sum reactively
const total = () => add(...values());

// Calculate scaled values
const scaled = () => multiply(total(), multiplier());

Range and Constraint Operations

Functions for constraining values within specific ranges.

/**
 * Clamp a number value between two other values
 * @param n - Number to clamp
 * @param min - Minimum value
 * @param max - Maximum value
 * @returns Clamped value between min and max
 */
function clamp(n: number, min: number, max: number): number;

Usage Examples:

import { clamp } from "@solid-primitives/utils/immutable";
import { createSignal } from "solid-js";

// Basic clamping
const clamped = clamp(15, 0, 10); // 10
const inRange = clamp(5, 0, 10); // 5
const tooLow = clamp(-5, 0, 10); // 0

// Reactive clamping for user input
const [userInput, setUserInput] = createSignal(0);
const [minValue] = createSignal(0);
const [maxValue] = createSignal(100);

const clampedValue = () => clamp(userInput(), minValue(), maxValue());

// Usage in component
setUserInput(150); // Input is 150
console.log(clampedValue()); // 100 (clamped to max)

Complex Calculations

These functions can be combined for more complex mathematical operations:

Usage Examples:

import { add, multiply, divide, clamp } from "@solid-primitives/utils/immutable";
import { createSignal } from "solid-js";

// Calculate weighted average with clamping
const calculateWeightedAverage = (values: number[], weights: number[]) => {
  const weightedSum = values.reduce((sum, val, i) => 
    add(sum, multiply(val, weights[i] || 1)), 0
  );
  const totalWeight = add(...weights);
  const average = divide(weightedSum, totalWeight);
  return clamp(average, 0, 100); // Clamp percentage to 0-100
};

// Reactive calculation example
const [scores, setScores] = createSignal([85, 92, 78, 95]);
const [weights, setWeights] = createSignal([0.2, 0.3, 0.25, 0.25]);

const finalGrade = () => calculateWeightedAverage(scores(), weights());

// Complex formula with signal updates
const [principal, setPrincipal] = createSignal(1000);
const [rate, setRate] = createSignal(0.05);
const [time, setTime] = createSignal(3);

// Compound interest calculation: A = P(1 + r)^t
const compoundInterest = () => {
  const baseAmount = add(1, rate());
  const finalAmount = multiply(principal(), power(baseAmount, time()));
  return finalAmount;
};

Signal Integration

These mathematical operations work seamlessly with Solid.js signals for reactive calculations:

Usage Examples:

import { createSignal, createMemo } from "solid-js";
import { add, multiply, clamp } from "@solid-primitives/utils/immutable";

// Reactive shopping cart total
const [items, setItems] = createSignal([
  { price: 10.99, quantity: 2 },
  { price: 25.50, quantity: 1 },
  { price: 8.75, quantity: 3 }
]);

const subtotal = createMemo(() => 
  items().reduce((total, item) => 
    add(total, multiply(item.price, item.quantity)), 0
  )
);

const [taxRate] = createSignal(0.08);
const tax = createMemo(() => multiply(subtotal(), taxRate()));
const total = createMemo(() => add(subtotal(), tax()));

// Reactive progress calculation with clamping
const [currentStep, setCurrentStep] = createSignal(0);
const [totalSteps] = createSignal(10);

const progress = createMemo(() => 
  clamp(
    multiply(divide(currentStep(), totalSteps()), 100),
    0,
    100
  )
);

Type Definitions

The mathematical operations maintain type safety while supporting multiple input types where appropriate:

// add function supports both number and string arrays
function add(...a: number[]): number;
function add(...a: string[]): string;

// Other functions work with numbers  
function substract(a: number, ...b: number[]): number; // Note: misspelled in source
function multiply(a: number, ...b: number[]): number;
function divide(a: number, ...b: number[]): number;
function power(a: number, ...b: number[]): number;
function clamp(n: number, min: number, max: number): number;

Note: The add function is overloaded to work with both numbers (performing addition) and strings (performing concatenation), making it versatile for different use cases in reactive applications.

Install with Tessl CLI

npx tessl i tessl/npm-solid-primitives--utils

docs

environment-utilities.md

immutable-arrays.md

immutable-objects.md

index.md

math-operations.md

reactive-utilities.md

type-definitions.md

tile.json