Option

김형태·2024년 2월 5일
0

fp-ts

목록 보기
4/4

Option overview

type Option<A> = None | Some<A>

Option<A> 는 type이 A인 선택적(optional)인 값의 컨테이너입니다. 만약 타입 A인 값이 존재한다면, Option<A>는 타입이 A인 값을 담고 있는 Some<A>의 인스턴스가 되고, 값이 없다면 None의 인스턴스가 됩니다.

Option은 1개 또는 0개의 요소를 가지고 있는 컬렉션 또는 폴더블(foldable)한 구조로 볼 수 있습니다. Option을 실패할 수도 있는 계산을 나타내는 effect로도 볼 수 있습니다.

Example

import * as O from 'fp-ts/Option'
import { pipe } from 'fp-ts/function'

const double = (n: number): number => n * 2

export const imperative = (as: ReadonlyArray<number>): string => {
  const head = (as: ReadonlyArray<number>): number => {
    if (as.length === 0) {
      throw new Error()
    }
    return as[0]
  }
  const inverse = (n: number): number => {
    if (n === 0) {
      throw new Error()
    }
    return 1 / n
  }
  try {
    return `Result is ${inverse(double(head(as)))}`
  } catch (e) {
    return 'no result'
  }
}

export const functional = (as: ReadonlyArray<number>): string => {
  const head = <A>(as: ReadonlyArray<A>): O.Option<A> => (as.length === 0 ? O.none : O.some(as[0]))
  const inverse = (n: number): O.Option<number> => (n === 0 ? O.none : O.some(1 / n))
  return pipe(
    as,
    head,
    O.map(double),
    O.flatMap(inverse),
    O.match(
      () => 'no result', // onNone handler
      (head) => `Result is ${head}` // onSome handler
    )
  )
}

assert.deepStrictEqual(imperative([1, 2, 3]), functional([1, 2, 3]))
assert.deepStrictEqual(imperative([]), functional([]))
assert.deepStrictEqual(imperative([0]), functional([0]))

combinators

tap

계산을 순차적으로 조합하여 한 계산의 반환 값을 사용하여 다음 계산을 결정하고 첫 번째 계산의 결과만 유지합니다.

Signature

export declare const tap: {
  <A, _>(self: Option<A>, f: (a: A) => Option<_>): Option<A>
  <A, _>(f: (a: A) => Option<_>): (self: Option<A>) => Option<A>
}

v2.15.0에서 추가

A를 인자로 받아 Option<_>을 반환하는 함수를 인자로 받아 Option<A>를 반환

tapEither

계산을 순차적으로 조합하여 한 계산의 반환 값을 사용하여 다음 계산을 결정하고 첫 번째 계산의 결과만 유지합니다.

Signature

export declare const tapEither: {
  <A, E, _>(f: (a: A) => Either<E, _>): (self: Option<A>) => Option<A>
  <A, E, _>(self: Option<A>, f: (a: A) => Either<E, _>): Option<A>
}

A를 인자로 받아 Either<E, _>을 반환하는 함수를 인자로 받아 Option<A>를 반환

Example

import { pipe } from 'fp-ts/function'
import * as O from 'fp-ts/Option'
import * as E from 'fp-ts/Either'

const compute = (value: number) =>
  pipe(
    O.of(value),
    O.tapEither((value) => (value > 0 ? E.right('ok') : E.left('error')))
  )

assert.deepStrictEqual(compute(1), O.of(1))
assert.deepStrictEqual(compute(-42), O.none)

v2.16.0에서 추가

constructors

getLeft

가능한 경우 EitherLeft 값을 반환

Signature

export declare const getLeft: <E, A>(ma: Either<E, A>) => Option<E>

Example

import { getLeft, none, some } from 'fp-ts/Option'
import { right, left } from 'fp-ts/Either'

assert.deepStrictEqual(getLeft(right(1)), none)
assert.deepStrictEqual(getLeft(left('a')), some('a'))

v2.0.0에서 추가

getRight

가능한 경우 EitherRight 값을 반환

Signature

export declare const getRight: <E, A>(ma: Either<E, A>) => Option<A>

Example

import { getLeft, none, some } from 'fp-ts/Option'
import { right, left } from 'fp-ts/Either'

assert.deepStrictEqual(getLeft(right(1)), none)
assert.deepStrictEqual(getLeft(left('a')), some('a'))

v2.0.0에서 추가

none

None은 생성자를 갖지 않는 대신, 값으로 직접 사용할 수 있습니다. none은 누락된 값을 나타냅니다.

Signature

export declare const none: Option<never>

v2.0.0에서 추가

of

Signature

export declare const of: <A>(a: A) => Option<A>

v2.7.0에서 추가

some

Some을 생성합니다. 존재하는 선택적인 값을 나타냅니다.

Signature

export declare const some: <A>(a: A) => Option<A>

v2.0.0에서 추가

profile
steady

0개의 댓글