Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarvisTheAvenger/0243d0b362b7bb23f6e96384b7c807a1 to your computer and use it in GitHub Desktop.
Save JarvisTheAvenger/0243d0b362b7bb23f6e96384b7c807a1 to your computer and use it in GitHub Desktop.
// Custom Implementation of higher order functions: map, filter, reduce
extension Collection {
func customMap<T>(_ tranform: (Element) -> T) -> [T] {
var result = [T]()
for element in self {
result.append(tranform(element))
}
return result
}
func customFilter(_ isIncluded: (Element) -> Bool) -> [Element] {
var result = [Element]()
for element in self {
if isIncluded(element) {
result.append(element)
}
}
return result
}
func customReduce<Result>(initialResult: Result, nextPartialResult: (Result, Element) -> Result) -> Result {
var result = initialResult
forEach { element in
result = nextPartialResult(result, element)
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment