Home 함수형 프로그래밍 - map
Post
Cancel

함수형 프로그래밍 - map

map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as O from "./option";

export const curry2 =
  <A, B, C>(f: (a: A, b: B) => C) =>
  (a: A) =>
  (b: B): C =>
    f(a, b);

export const flip =
  <A, B, C>(f: (a: A, b: B) => C) =>
  (b: B, a: A): C =>
    f(a, b);

// Array<A> == A[]
// map :: (Array<A>, (A => B)) => Array<B>
export const map = <A, B>(array: Array<A>, f: (a: A) => B): Array<B> => {
  const result: Array<B> = [];
  for (const value of array) {
    result.push(f(value));
  }
  return result;
};

export const main = () => {
  const numbers = [1, 2, 3];
  const isEven = (x: number) => x % 2 === 0;

  map(numbers, isEven);

  // curriedMap :: Array<A> => ((A => B) => Array<B>)
  const curriedMap = curry2(map);
  curriedMap(numbers)(isEven);

  // map :: Array<A> ~> (A => B) => Array<B>
  // 인스턴스로서 메서드를 호출
  numbers.map(isEven);

  // map_ :: (A => B) => (Array<A> => Array<B>)
  const map_ = curry2(flip(map));
  map_(isEven)(numbers);

  // isEven :: number => boolean
  // mapIsEven :: Array<number> => Array<boolean>
  const mapIsEven = map_(isEven);

  isEven(42);
  isEven(7);

  mapIsEven(numbers);
  mapIsEven([]);
  mapIsEven([42]);

  const omap = curry2(flip(O.map));
  // optionIsEven :: Option<number> => Option<boolean>
  const optionIsEven = omap(isEven);
  optionIsEven(O.some(42));
  optionIsEven(O.none());
};
This post is licensed under CC BY 4.0 by the author.

함수형 프로그래밍 - Partial, Currying

쿠버네티스 - exr1 - POD