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

list-operations.mddocs/

List Operations

Comprehensive array manipulation functions with functional programming patterns. The List module provides 69 functions covering iteration, transformation, folding, sorting, searching, and mathematical operations on arrays.

Capabilities

Iteration and Transformation

Core functions for iterating over and transforming arrays.

/**
 * Applies function to each element, returns original array
 * @param {Function} fn - Function to apply to each element
 * @param {Array} array - Array to iterate over
 * @returns {Array} Original array (for chaining)
 */
function each(fn, array);

/**
 * Transforms each element using provided function
 * @param {Function} fn - Transformation function
 * @param {Array} array - Array to transform
 * @returns {Array} New array with transformed elements
 */
function map(fn, array);

/**
 * Removes falsy values from array
 * @param {Array} array - Array to compact
 * @returns {Array} Array without falsy values
 */
function compact(array);

/**
 * Keeps elements that match predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to filter
 * @returns {Array} Filtered array
 */
function filter(predicate, array);

/**
 * Removes elements that match predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to filter
 * @returns {Array} Array with rejected elements removed
 */
function reject(predicate, array);

/**
 * Removes first occurrence of element
 * @param {*} element - Element to remove
 * @param {Array} array - Array to modify
 * @returns {Array} Array with element removed
 */
function remove(element, array);

/**
 * Splits array into [passed, failed] based on predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to partition
 * @returns {Array} [passed, failed] arrays
 */
function partition(predicate, array);

/**
 * Finds first element matching predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to search
 * @returns {*} First matching element or undefined
 */
function find(predicate, array);

Array Access and Structure

Functions for accessing and manipulating array structure.

/**
 * Gets first element of array
 * @param {Array} array - Array to access
 * @returns {*} First element or undefined
 */
function head(array);

/**
 * Alias for head - gets first element
 * @param {Array} array - Array to access
 * @returns {*} First element or undefined
 */
function first(array);

/**
 * Gets all elements except first
 * @param {Array} array - Array to access
 * @returns {Array} Array without first element
 */
function tail(array);

/**
 * Gets last element of array
 * @param {Array} array - Array to access
 * @returns {*} Last element or undefined
 */
function last(array);

/**
 * Gets all elements except last
 * @param {Array} array - Array to access
 * @returns {Array} Array without last element
 */
function initial(array);

/**
 * Checks if array is empty
 * @param {Array} array - Array to check
 * @returns {boolean} True if array has no elements
 */
function empty(array);

/**
 * Reverses array (returns new array)
 * @param {Array} array - Array to reverse
 * @returns {Array} Reversed array
 */
function reverse(array);

Set Operations

Functions for set-like operations on arrays.

/**
 * Elements in first array not in others (variadic)
 * @param {...Array} arrays - Arrays to compare
 * @returns {Array} Elements unique to first array
 */
function difference(...arrays);

/**
 * Elements common to all arrays (variadic)
 * @param {...Array} arrays - Arrays to intersect
 * @returns {Array} Common elements
 */
function intersection(...arrays);

/**
 * All unique elements from all arrays (variadic)
 * @param {...Array} arrays - Arrays to unite
 * @returns {Array} Union of all arrays
 */
function union(...arrays);

/**
 * Removes duplicate elements
 * @param {Array} array - Array to process
 * @returns {Array} Array with unique elements
 */
function unique(array);

/**
 * Removes duplicates based on function result
 * @param {Function} fn - Function to determine uniqueness
 * @param {Array} array - Array to process
 * @returns {Array} Array with unique elements by function
 */
function uniqueBy(fn, array);

Folding and Reduction

Functions for reducing arrays to single values.

/**
 * Left fold with initial value (alias: foldl)
 * @param {Function} fn - Reducer function (acc, val) => newAcc
 * @param {*} initial - Initial accumulator value
 * @param {Array} array - Array to fold
 * @returns {*} Final accumulated value
 */
function fold(fn, initial, array);

/**
 * Alias for fold - left fold with initial value
 * @param {Function} fn - Reducer function
 * @param {*} initial - Initial accumulator value
 * @param {Array} array - Array to fold
 * @returns {*} Final accumulated value
 */
function foldl(fn, initial, array);

/**
 * Left fold using first element as initial (alias: foldl1)
 * @param {Function} fn - Reducer function
 * @param {Array} array - Non-empty array to fold
 * @returns {*} Final accumulated value
 */
function fold1(fn, array);

/**
 * Alias for fold1 - left fold using first element
 * @param {Function} fn - Reducer function
 * @param {Array} array - Non-empty array to fold
 * @returns {*} Final accumulated value
 */
function foldl1(fn, array);

/**
 * Right fold with initial value
 * @param {Function} fn - Reducer function (val, acc) => newAcc
 * @param {*} initial - Initial accumulator value
 * @param {Array} array - Array to fold
 * @returns {*} Final accumulated value
 */
function foldr(fn, initial, array);

/**
 * Right fold using last element as initial
 * @param {Function} fn - Reducer function
 * @param {Array} array - Non-empty array to fold
 * @returns {*} Final accumulated value
 */
function foldr1(fn, array);

/**
 * Unfolds array from seed value using function
 * @param {Function} fn - Function returning [value, newSeed] or null
 * @param {*} seed - Initial seed value
 * @returns {Array} Unfolded array
 */
function unfoldr(fn, seed);

Logical Operations

Functions for logical operations on arrays.

/**
 * Logical AND of all elements
 * @param {Array} array - Array of values
 * @returns {boolean} True if all elements are truthy
 */
function andList(array);

/**
 * Logical OR of all elements
 * @param {Array} array - Array of values
 * @returns {boolean} True if any element is truthy
 */
function orList(array);

/**
 * Tests if any element matches predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to test
 * @returns {boolean} True if any element matches
 */
function any(predicate, array);

/**
 * Tests if all elements match predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to test
 * @returns {boolean} True if all elements match
 */
function all(predicate, array);

Sorting and Ordering

Functions for sorting arrays.

/**
 * Sorts array using default comparison
 * @param {Array} array - Array to sort
 * @returns {Array} Sorted array
 */
function sort(array);

/**
 * Sorts using custom comparison function
 * @param {Function} compareFn - Comparison function (a, b) => number
 * @param {Array} array - Array to sort
 * @returns {Array} Sorted array
 */
function sortWith(compareFn, array);

/**
 * Sorts by result of function applied to elements
 * @param {Function} fn - Function to extract sort key
 * @param {Array} array - Array to sort
 * @returns {Array} Sorted array
 */
function sortBy(fn, array);

Mathematical Operations

Functions for mathematical operations on arrays of numbers.

/**
 * Sum of all numbers in array
 * @param {Array<number>} array - Array of numbers
 * @returns {number} Sum of all numbers
 */
function sum(array);

/**
 * Product of all numbers in array
 * @param {Array<number>} array - Array of numbers
 * @returns {number} Product of all numbers
 */
function product(array);

/**
 * Arithmetic mean of numbers (alias: average)
 * @param {Array<number>} array - Array of numbers
 * @returns {number} Arithmetic mean
 */
function mean(array);

/**
 * Alias for mean - arithmetic mean of numbers
 * @param {Array<number>} array - Array of numbers
 * @returns {number} Arithmetic mean
 */
function average(array);

/**
 * Largest element in array
 * @param {Array} array - Array to search
 * @returns {*} Maximum element
 */
function maximum(array);

/**
 * Smallest element in array
 * @param {Array} array - Array to search
 * @returns {*} Minimum element
 */
function minimum(array);

/**
 * Largest element by function result
 * @param {Function} fn - Function to extract comparison value
 * @param {Array} array - Array to search
 * @returns {*} Element with maximum function result
 */
function maximumBy(fn, array);

/**
 * Smallest element by function result
 * @param {Function} fn - Function to extract comparison value
 * @param {Array} array - Array to search
 * @returns {*} Element with minimum function result
 */
function minimumBy(fn, array);

Array Manipulation

Functions for combining and manipulating arrays.

/**
 * Concatenates array of arrays
 * @param {Array<Array>} arrays - Array containing arrays to concatenate
 * @returns {Array} Flattened array
 */
function concat(arrays);

/**
 * Maps function then concatenates results
 * @param {Function} fn - Function returning array
 * @param {Array} array - Array to map over
 * @returns {Array} Concatenated results
 */
function concatMap(fn, array);

/**
 * Recursively flattens nested arrays
 * @param {Array} array - Nested array structure
 * @returns {Array} Completely flattened array
 */
function flatten(array);

Scanning

Functions for intermediate results during folding.

/**
 * Left scan - returns intermediate fold results (alias: scanl)
 * @param {Function} fn - Reducer function
 * @param {*} initial - Initial value
 * @param {Array} array - Array to scan
 * @returns {Array} Array of intermediate results
 */
function scan(fn, initial, array);

/**
 * Alias for scan - left scan with initial value
 * @param {Function} fn - Reducer function
 * @param {*} initial - Initial value
 * @param {Array} array - Array to scan
 * @returns {Array} Array of intermediate results
 */
function scanl(fn, initial, array);

/**
 * Left scan using first element as initial (alias: scanl1)
 * @param {Function} fn - Reducer function
 * @param {Array} array - Non-empty array to scan
 * @returns {Array} Array of intermediate results
 */
function scan1(fn, array);

/**
 * Alias for scan1 - left scan using first element
 * @param {Function} fn - Reducer function
 * @param {Array} array - Non-empty array to scan
 * @returns {Array} Array of intermediate results
 */
function scanl1(fn, array);

/**
 * Right scan with initial value
 * @param {Function} fn - Reducer function
 * @param {*} initial - Initial value
 * @param {Array} array - Array to scan
 * @returns {Array} Array of intermediate results
 */
function scanr(fn, initial, array);

/**
 * Right scan using last element as initial
 * @param {Function} fn - Reducer function
 * @param {Array} array - Non-empty array to scan
 * @returns {Array} Array of intermediate results
 */
function scanr1(fn, array);

Slicing and Taking

Functions for extracting portions of arrays.

/**
 * Extracts section of array
 * @param {number} start - Start index (inclusive)
 * @param {number} end - End index (exclusive)
 * @param {Array} array - Array to slice
 * @returns {Array} Sliced portion
 */
function slice(start, end, array);

/**
 * Takes first n elements
 * @param {number} n - Number of elements to take
 * @param {Array} array - Array to take from
 * @returns {Array} First n elements
 */
function take(n, array);

/**
 * Drops first n elements
 * @param {number} n - Number of elements to drop
 * @param {Array} array - Array to drop from
 * @returns {Array} Array without first n elements
 */
function drop(n, array);

/**
 * Splits array at index into two arrays
 * @param {number} index - Index to split at
 * @param {Array} array - Array to split
 * @returns {Array} [firstPart, secondPart]
 */
function splitAt(index, array);

/**
 * Takes elements while predicate is true
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to take from
 * @returns {Array} Elements taken while predicate true
 */
function takeWhile(predicate, array);

/**
 * Drops elements while predicate is true
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to drop from
 * @returns {Array} Remaining elements after dropping
 */
function dropWhile(predicate, array);

/**
 * Splits into [takeWhile, dropWhile] results
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to split
 * @returns {Array} [taken, remaining]
 */
function span(predicate, array);

/**
 * Splits where predicate first becomes true
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to break
 * @returns {Array} [before, after] break point
 */
function breakList(predicate, array);

Grouping and Counting

Functions for grouping and counting elements.

/**
 * Counts elements by function result
 * @param {Function} fn - Function to group by
 * @param {Array} array - Array to count
 * @returns {Object} Object mapping keys to counts
 */
function countBy(fn, array);

/**
 * Groups elements by function result
 * @param {Function} fn - Function to group by
 * @param {Array} array - Array to group
 * @returns {Object} Object mapping keys to arrays of elements
 */
function groupBy(fn, array);

Zipping

Functions for combining multiple arrays.

/**
 * Zips two arrays into array of pairs
 * @param {Array} array1 - First array
 * @param {Array} array2 - Second array
 * @returns {Array} Array of [elem1, elem2] pairs
 */
function zip(array1, array2);

/**
 * Zips two arrays using function
 * @param {Function} fn - Function to combine elements
 * @param {Array} array1 - First array
 * @param {Array} array2 - Second array
 * @returns {Array} Array of combined elements
 */
function zipWith(fn, array1, array2);

/**
 * Zips multiple arrays (variadic)
 * @param {...Array} arrays - Arrays to zip
 * @returns {Array} Array of element arrays
 */
function zipAll(...arrays);

/**
 * Zips multiple arrays using function (variadic)
 * @param {Function} fn - Function to combine elements
 * @param {...Array} arrays - Arrays to zip
 * @returns {Array} Array of combined elements
 */
function zipAllWith(fn, ...arrays);

Indexing and Search

Functions for finding elements and indices.

/**
 * Gets element at index (supports negative indices)
 * @param {number} index - Index to access (negative for from end)
 * @param {Array} array - Array to access
 * @returns {*} Element at index or undefined
 */
function at(index, array);

/**
 * Finds index of element
 * @param {*} element - Element to find
 * @param {Array} array - Array to search
 * @returns {number} Index of element or -1
 */
function elemIndex(element, array);

/**
 * Finds all indices of element
 * @param {*} element - Element to find
 * @param {Array} array - Array to search
 * @returns {Array<number>} Array of indices
 */
function elemIndices(element, array);

/**
 * Finds index of first element matching predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to search
 * @returns {number} Index of first match or -1
 */
function findIndex(predicate, array);

/**
 * Finds all indices of elements matching predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Array} array - Array to search
 * @returns {Array<number>} Array of matching indices
 */
function findIndices(predicate, array);

Usage Examples

Basic Array Processing:

const { map, filter, fold } = require('prelude-ls');

const numbers = [1, 2, 3, 4, 5];
const doubled = map(x => x * 2, numbers);        // [2, 4, 6, 8, 10]
const evens = filter(x => x % 2 === 0, doubled); // [2, 4, 6, 8, 10]
const sum = fold((a, b) => a + b, 0, evens);     // 30

Curried Function Composition:

const { map, filter, fold } = require('prelude-ls');

// Create reusable functions
const double = map(x => x * 2);
const evens = filter(x => x % 2 === 0);
const sum = fold((a, b) => a + b, 0);

// Compose pipeline
const processNumbers = numbers => sum(evens(double(numbers)));
const result = processNumbers([1, 2, 3, 4, 5]); // 30

Complex Data Processing:

const { groupBy, map, keys, sortBy } = require('prelude-ls');

const students = [
  { name: 'Alice', grade: 'A', subject: 'Math' },
  { name: 'Bob', grade: 'B', subject: 'Math' },
  { name: 'Charlie', grade: 'A', subject: 'Science' }
];

const byGrade = groupBy(student => student.grade, students);
const gradeStats = map(group => group.length, byGrade);
const sortedGrades = sortBy(grade => grade, keys(gradeStats));

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