Skip to content
김태헌 edited this page Sep 16, 2024 · 7 revisions

예시 1: 배열에서 요소 가져오기

import { at } from 'mori-ts';

const array = [1, 2, 3];

// 인덱스 0의 요소를 가져옵니다.
const firstElement = at(0, array);
console.log(firstElement); // 출력: 1

// 인덱스 -1의 요소를 가져옵니다 (배열의 끝에서 첫 번째 요소).
const lastElement = at(-1, array);
console.log(lastElement); // 출력: 3

예시 2: 파이프라인과 함께 사용하기

import { at, pipe, map, filter, toAsync } from 'mori-ts';

const array = [1, 2, 3];

// 요소들을 두 배로 만든 후, 2보다 큰 요소들 중 첫 번째 요소를 가져옵니다.
const result = pipe(
  array,
  map(x => x * 2),
  filter(x => x > 2),
  at(0)
); // 출력: 4

// 비동기 반복자에서 요소들을 두 배로 만든 후, 2보다 큰 요소들 중 마지막 요소를 가져옵니다.
const asyncResult = await pipe(
  [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)],
  toAsync,
  map(x => x * 2),
  at(-1)
); // 출력: 6

테스트 코드 링크

https://github.com/gangnamssal/mori-ts/blob/main/src/core/at/at.spec.ts

Clone this wiki locally