Skip to content

Iterator와 AsyncIterator 기반의 함수형 프로그래밍 라이브러리

License

Notifications You must be signed in to change notification settings

gangnamssal/mori-ts

Repository files navigation

mori-ts

mori-ts는 JavaScript 및 TypeScript 개발자를 위한 함수형 프로그래밍 라이브러리입니다. 이 라이브러리는 동기 및 비동기 iterable 객체를 처리하는 데 필요한 다양한 유틸리티 함수를 제공합니다.

mori-ts is a functional programming library for JavaScript and TypeScript developers. This library provides various utility functions necessary for handling synchronous and asynchronous iterable objects.

Index

1.Install

2.Main Function

3.Concept

3.Core

4.License

Install

npm

npm install mori-ts

yarn

yarn add mori-ts

pnpm

pnpm install mori-ts

Main Function

  • 고차 함수 지원 (Support for Higher-Order Functions) : 함수들을 조합하여 복잡한 로직을 간단하게 구현할 수 있습니다.

  • 지연 평가 (Lazy Evaluation) : 계산을 필요할 때까지 미루어 성능을 최적화합니다.

  • 다양한 iterable 처리 (Handling Various Iterables) : 배열, 객체, 비동기 iterable 등 다양한 데이터 구조를 지원합니다.

Concept

1. 단일 함수로 사용 (Used as a Single Function)

모든 함수는 단독으로 사용할 수 있습니다.

import { map, filter, toArray } from 'mori-ts';

const arr = [1, 2, 3, 4, 5];

const mappedResult = toArray(map(a => a * 2, arr));

const filteredResult = toArray(filter(a => a % 2 === 0, arr));

2. pipe 라인으로 사용 (Used in a Pipeline)

모든 함수는 pipe 라인과 사용할 수 있습니다.

import { pipe, map, range, filter, toArray } from 'mori-ts';

const res = pipe(
  range(1, 10),
  map(a => a * 2),
  filter(a => a % 2),
  toArray,
);

Core

at

  • 주어진 인덱스에 해당하는 요소를 반환합니다.

  • 인덱스가 음수일 경우, 배열의 끝에서부터 요소를 반환합니다.

  • 동기 및 비동기 iterable 모두를 지원합니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/at

chunk

compact

concat

concurrent

  • 비동기 작업을 병렬로 처리할 수 있도록 도와줍니다.

  • 이 함수는 비동기 iterable을 처리할 때 동시에 실행할 최대 작업 수를 제한하여 병목 현상을 줄이고 성능을 향상시킵니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/concurrent

curry

delay

  • 주어진 시간(ms) 동안 대기한 후 지정된 값을 반환하거나 기본값인 undefined를 반환합니다.

  • 주로 비동기 작업 시 특정 시간 동안 대기해야 할 때 유용하게 사용됩니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/delay

delayEach

  • 주어진 지연 시간 동안 각 요소를 대기하면서 제공된 iterable을 반환하는 함수입니다.

  • 이 함수는 동기 및 비동기 iterable 모두를 지원하며, 비동기 작업에서 각 요소의 처리를 일정 시간 간격으로 지연시킬 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/delayEach

delayIter

  • 주어진 지연 시간(ms) 동안 각 요소의 반환을 대기하며 iterable을 생성하는 함수입니다.

  • 이 함수는 동기와 비동기 iterable 모두를 지원합니다. 이를 통해 각 요소가 일정한 시간 간격으로 처리되도록 제어할 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/delayIter

drop

each

every

  • 모든 요소가 특정 조건을 만족하는지 확인하는 함수입니다.

  • 조건을 만족하면 true를, 하나라도 만족하지 않으면 false를 반환합니다.

  • 동기 및 비동기 iterable 모두에서 작동할 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/every

filter

  • 주어진 조건을 만족하는 요소만 포함된 iterable을 반환합니다.

  • 조건을 만족하지 않는 요소는 제외됩니다.

  • 동기 및 비동기 iterable 모두에서 사용될 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/filter

find

  • 주어진 조건을 만족하는 첫 번째 요소를 찾습니다.

  • 조건을 만족하는 요소가 발견되면 해당 요소를 반환하고, 조건을 만족하는 요소가 없으면 undefined를 반환합니다.

  • 동기 및 비동기 iterable 모두에서 사용될 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/find

flat

  • 중첩된 iterable을 평평하게 만들어 단일 레벨의 iterable로 변환합니다.

  • 깊이의 제어 없이 배열을 단일 레벨로 펼칩니다.

  • 동기 및 비동기 iterable 모두에서 사용될 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/flat

flatMap

  • iterable의 각 요소에 대해 제공된 함수를 적용하고, 결과로 얻어진 배열을 평평하게(flatten) 만들어 반환합니다.

  • 각 요소를 변환하여 여러 값을 생성한 뒤, 이 값을 단일 레벨로 펼쳐줍니다.

  • 동기 및 비동기 iterable 모두에서 사용될 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/flatMap

join

length

map

pipe

  • 여러 개의 함수를 연결하여 데이터를 처리합니다.

  • 이 함수는 입력값을 첫 번째 함수에 전달하고, 각 함수의 출력값을 다음 함수의 입력값으로 사용하는 방식으로 작동합니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/pipe

range

reduce

  • iterable 객체의 요소를 하나의 값으로 축소합니다.

  • 누산기(accumulator)와 현재 값(current value)을 기반으로 최종 결과를 계산합니다.

  • 동기 및 비동기 iterable 모두에서 사용될 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/reduce

reverse

  • iterable 객체의 요소를 역순으로 반환합니다.

  • 동기 및 비동기 iterable 모두에서 사용될 수 있습니다.

  • 문자열, Map, Set 등 다양한 iterable 객체를 지원합니다.

  • pipe 함수와 함께 사용하여 다양한 조합의 함수 체인을 만들 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/reverse

some

  • 주어진 조건을 만족하는 요소가 iterable 객체에 존재하는지를 검사합니다.

  • 조건이 참인 요소가 하나라도 있으면 true를 반환하고, 그렇지 않으면 false를 반환합니다.

  • 동기 및 비동기 iterable 모두에서 사용될 수 있습니다.

  • 예시(Example) : https://github.com/gangnamssal/mori-ts/wiki/some

take

toArray

toAsync

toIterValue

zip

License

This project is licensed under the MIT License - see the LICENSE.md file for details.