//
//  Sequence+Max.swift
//  coffeTracker
//
//  Created by Kyryl Horbushko on 06.06.2021.
//

import Foundation

extension Sequence {
  
  func max<Result: FixedWidthInteger>(_ keyPath: KeyPath<Element, Result>) -> Result {
    self.reduce(0, {
      Swift.max($0, $1[keyPath: keyPath])
    })
  }
  
  func max<Result: FloatingPoint>(_ keyPath: KeyPath<Element, Result>) -> Result {
    self.reduce(0, {
      Swift.max($0, $1[keyPath: keyPath])
    })
  }
  
  func min<Result: FixedWidthInteger>(_ keyPath: KeyPath<Element, Result>) -> Result {
    self.reduce(0, {
      Swift.min($0, $1[keyPath: keyPath])
    })
  }
  
  func min<Result: FloatingPoint>(_ keyPath: KeyPath<Element, Result>) -> Result {
    self.reduce(0, {
      Swift.min($0, $1[keyPath: keyPath])
    })
  }
  
  func maxValue<Result: FloatingPoint>() -> Result where Self.Element == Result {
    self.reduce(0, {
      Swift.max($0, $1)
    })
  }
  
  func minValue<Result: FloatingPoint>() -> Result where Self.Element == Result {
    self.reduce(0, {
      Swift.min($0, $1)
    })
  }
}
