Created
July 22, 2023 12:35
-
-
Save JarvisTheAvenger/0243d0b362b7bb23f6e96384b7c807a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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