Prelude.ls Functional Utility Library
prelude.ls is a functionally oriented utility library designed to simplify common programming tasks by providing a rich set of curried functions. It is primarily written in LiveScript and serves as the recommended base library for LiveScript projects, although it is fully usable within standard JavaScript environments. The library offers a wide array of utilities for lists, objects, strings, functions, and numbers, emphasizing a functional programming paradigm with functions like `map`, `filter`, and `fold` readily available. The current stable version is 1.2.1. Given its last update approximately six years ago (around April 2020), its release cadence is effectively inactive, indicating it is no longer actively maintained. A key differentiator is its deep integration with and recommendation for the LiveScript ecosystem, providing a Haskell-like prelude module, and its consistent use of currying across most functions. It predates widespread modern JavaScript module systems and exclusively functions via CommonJS modules.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require('prelude-ls')` in a browser environment without a CommonJS bundler (like Webpack or Browserify) or trying to use `import` in a pure CommonJS Node.js project.fixFor Node.js, ensure your project is configured for CommonJS modules. For browser usage, use a bundler that can resolve CommonJS modules, or include the `prelude-browser.js` file directly from the package's `browser` directory via a `<script>` tag. -
Error: Cannot find module 'prelude-ls'
cause The package `prelude-ls` is not installed or the module resolution path is incorrect, or an ES module `import` statement is used in a CommonJS-only context that doesn't transpile it.fixFirst, ensure the package is installed: `npm install prelude-ls`. If in Node.js, verify you are using `require('prelude-ls')`. If using a bundler, check its configuration for module resolution. -
Property 'map' does not exist on type 'typeof import("prelude-ls")'.cause This TypeScript error indicates that the type definitions for `prelude-ls` are either missing or incomplete, preventing the TypeScript compiler from recognizing exported functions.fixInstall the unofficial type definitions: `npm install --save-dev @types/prelude-ls`. If the issue persists for specific functions, you may need to manually augment the type declarations in a local `.d.ts` file.
Warnings
- breaking The `prelude-ls` library is no longer actively maintained, with its last publish occurring approximately six years ago. This means it may not receive updates for new JavaScript features, security vulnerabilities, or compatibility with newer Node.js versions.
- gotcha `prelude-ls` is a CommonJS-only module and does not support ES module `import` syntax natively. Attempting to use `import * as P from 'prelude-ls';` will result in module resolution errors in environments requiring ESM.
- gotcha The library does not ship with built-in TypeScript type definitions. While an unofficial `@types/prelude-ls` package exists, its accuracy and maintenance status may vary.
- gotcha Many functions in `prelude-ls` are curried by default, which can lead to unexpected partial application if not used correctly, especially for developers unfamiliar with this functional programming concept.
- gotcha The main exported object of `prelude-ls` has many functions directly on it (e.g., `P.map`). However, some functions with the same name behave differently for different data structures (e.g., `List.map` vs `Obj.map`). The top-level `map` typically refers to `List.map`.
Install
-
npm install prelude-ls -
yarn add prelude-ls -
pnpm add prelude-ls
Imports
- P
import * as P from 'prelude-ls';
const P = require('prelude-ls'); - map
import { map } from 'prelude-ls';const { map } = require('prelude-ls'); - Obj.map
const { Obj } = require('prelude-ls'); const objectMap = Obj.map;
Quickstart
const P = require('prelude-ls');
// Example 1: Basic list mapping with a curried function
const addTwo = P.map(x => x + 2);
const numbers = [1, 2, 3, 4, 5];
const mappedNumbers = addTwo(numbers);
console.log('Mapped numbers:', mappedNumbers); // Expected: [3, 4, 5, 6, 7]
// Example 2: Filtering even numbers, demonstrating currying
const isEven = x => x % 2 === 0;
const filterEven = P.filter(isEven);
const evenNumbers = filterEven(numbers);
console.log('Even numbers:', evenNumbers); // Expected: [2, 4]
// Example 3: Folding (reducing) a list
const sum = P.fold((acc, x) => acc + x, 0);
const total = sum(numbers);
console.log('Sum of numbers:', total); // Expected: 15
// Example 4: Using an object-specific function
const data = { a: 1, b: 2, c: 3 };
const objMapValues = P.Obj.map(x => x * 10);
const mappedObject = objMapValues(data);
console.log('Mapped object:', mappedObject); // Expected: { a: 10, b: 20, c: 30}