Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift Standard Library의 map, filter, reduce, compactMap, flatMap에 대하여 설명하시오. #5

Open
Taehyeon-Kim opened this issue Aug 29, 2021 · 3 comments
Labels

Comments

@Taehyeon-Kim
Copy link
Contributor

No description provided.

@Taehyeon-Kim
Copy link
Contributor Author

  • map : 각 요소를 하나씩 특정 형태로 mapping해서 반환
  • filter : 특정 조건에 따라 조건이 참인 경우에만 반환
  • reduce: 내부의 모든 요소를 클로저의 연산 결과로 합쳐서 반환, 클로저가 각 요소를 전달받아 다시 클로저로 반환해주면서 순환
  • compactMap : 배열에 nil이 있는 경우 제외하고 반환
  • flatMap : 2차원 배열을 1차원 배열로 변환해서 반환

@hyesuuou
Copy link
Member

hyesuuou commented Sep 5, 2021

  • map: map은 데이터 변형을 위해 사용합니다. map은 기존 데이터를 변형하여 새로운 컨테이너를 만들며, 이때 기존의 데이터는 변형되지 않습니다.
  • filter: filter는 기존의 컨테이너에서 기준에 맞는 특정 값을 추출하여 새로운 컨테이너를 만듭니다.
  • reduce: reduce는 기존 컨테이너 내부에 있는 값들을 결합하여 새로운 컨테이너를 만듭니다.
  • compactMap: compactMap은 컨테이너의 값 중에서 nil인 값을 제외한 데이터들을 포함해 새로운 컨테이너를 만듭니다.
  • flatMap: flatMap은 nil인 값을 제외할 뿐 아니라, n차원 배열을 n-1차원배열로 만듭니다.

@heerucan
Copy link

heerucan commented Sep 8, 2021

모두 스위프트 표준 라이브러리에서 사용되는 고차함수입니다.
map은 컨테이너 내부의 기존 데이터를 변형하여 새로운 컨테이너를 생성해서 반환하는 함수
compactMap을 통해서 옵셔널 바인딩 및 nil 제거해 반환하는 함수
flatmap은 2차원 배열을 1차원 배열로 반환하는 함수
filter는 컨테이너 내부의 값을 걸러서 새로운 컨테이너로 추출해서 반환하는 함수
reduce는 컨테이너 내부의 값을 하나로 통합해 새로운 컨테이너로 반환하는 함수

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants