MaxValue<T>

// 2020.10.15๏ผš

import SwiftUI

// ๐Ÿ“ฆ MaxValue<T: FloatingPoint>
public struct MaxValue<T: FloatingPoint>: PreferenceKey {
    // value type
    public typealias Value = T?
    // default value (nil == not set) 
    public static var defaultValue: Value { nil }
    // โญ๏ธ choose max value
    public static func reduce(value: inout Value, nextValue: () -> Value) {
        // โญ๏ธ [T?] --(compactMap)--> [T] --(max)--> T?
        value = [value, nextValue()].compactMap{ $0 }.max()
    }
}

Last updated