Skip to content

Instantly share code, notes, and snippets.

@boudhayan
Created May 5, 2020 18:02
Show Gist options
  • Select an option

  • Save boudhayan/891fd76f9ae8d4ca396cc1dd2b0b8069 to your computer and use it in GitHub Desktop.

Select an option

Save boudhayan/891fd76f9ae8d4ca396cc1dd2b0b8069 to your computer and use it in GitHub Desktop.
//MARK: Custom Implmentation of map, filter, reduce, flatMap
public extension Array {
//map
func map2<T>(_ transform: (Element) -> T) -> [T] {
var mapped = [T]()
mapped.reserveCapacity(count)
for element in self {
mapped.append(transform(element))
}
return mapped
}
//filter
func filter2(_ isIncluded: (Element) -> Bool) -> [Element] {
var filtered = [Element]()
for element in self where isIncluded(element) {
filtered.append(element)
}
return filtered
}
//reduce
func reduce2<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) -> Result) -> Result {
var reduced = initialResult
for element in self {
reduced = nextPartialResult(reduced, element)
}
return reduced
}
//flatMap
func flatMap2<T>(_ transform: (Element) -> [T]) -> [T] {
var flatMapped = [T]()
for element in self {
flatMapped.append(contentsOf: transform(element))
}
return flatMapped
}
}
//MARK: map, filter using reduce
public extension Array {
func map3<T>(_ transform: (Element) -> T) -> [T] {
reduce([]) { $0 + [transform($1)] }
}
func filter3(_ isIncluded: (Element) -> Bool) -> [Element] {
reduce([]) { isIncluded($1) ? $0 + [$1] : $0 }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment