Skip to content

Repository files navigation

js-pairs

github action status

Зачем это нужно

Пара это простейшая структура из двух значений. Библиотека нужна курсам, где пары строятся на замыканиях, а не на объектах языка: cons возвращает функцию, которая помнит два значения, а car и cdr её спрашивают.

Смысл упражнения в том, что структура данных получается из одних функций. Никакого «контейнера» внутри нет, хранение обеспечивает область видимости.

Install

npm install @hexlet/pairs

Usage example

import {
  cons, car, cdr, toString, isPair,
} from '@hexlet/pairs';

const pair = cons(3, 5);

isPair(pair); // true

car(pair); // 3
cdr(pair); // 5

toString(pair); // (3, 5)

TypeScript

Full type definitions ship with the package:

import { cons, car } from '@hexlet/pairs'
import type { Pair } from '@hexlet/pairs'

const pair: Pair<number, string> = cons(1, 'hexlet')
const left = car(pair) // type: number

For more information, see the Full Documentation

Development

  • npm test — run Vitest
  • npm run lint — ESLint (JS + TS)
  • npm run typecheck — strict TypeScript diagnostics
  • npm run build — emit ESM artifacts to dist/

Hexlet Ltd. logo

This repository is created and maintained by the team and the community of Hexlet, an educational project. Read more about Hexlet.

Releases

Packages

Used by

Contributors

Languages