High order functions assignation

Viewed 307

I'm learning about High Order functions in Swift (like .map .filter .reduce...) and generics types.

Here is my function :

func max<T: Comparable>(_ array: [T]) -> T {
    var max = 0 as! T

    for value in array {
        if value > max { max = value }
    }

    return max
}

How can I replace my for loop with high order function to get the same result ?

Im looking to do something like this (or better) :

max = array.map { $0 > max ? $0 : max }
3 Answers
Related