Skip to content

Latest commit

 

History

History
35 lines (31 loc) · 1.32 KB

2. filter.md

File metadata and controls

35 lines (31 loc) · 1.32 KB

날짜: 2023-02-08 14:03

주제: 각 요소 접근 후 역할에 맞는 값 반환


메모:

What is filter

  • filter는 컨테이너 내부의 값을 걸러서 추출하는 역할을 하는 고차함수이다.
  • Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.
Declaration
func filter(_ isIncluded: (Self.Element) throws -> Bool ) rethrows -> [Self.Element]
parameters
isIncluded
  • A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the returned array.
  • filter 함수의 매개변수로 전달되는 함수의 반환 타입은 Bool 타입이다. 즉, 새로운 컨테이너에 포함될 항목이라면 true를, 포함하지 않는다면 false를 반환 해준다.
Return Value
  • An array of the elements that isIncluded allowed.
Details
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let shortNames = cast.filter { $0.count < 5 }
print(shortNames)
// Prints "["Kim", "Karl"]"

출처(참고문헌)

연결문서

  • [[2. 고차함수 활용하기]]

Tag

  • #Swift/Higher-Oder-Function/filter