Skip to content
loading...

re: Daily Challenge #187 - Most Sales VIEW POST

FULL DISCUSSION
 

OCaml, with Core as stdlib:

let highestSales products amounts prices =
  List.map3_exn products amounts prices (fun prod a p -> prod, a * p) 
  |> List.max_elt ~compare:(fun (_, p1) (_, p2) -> Int.compare p1 p2)
  |> Option.map ~f:fst

This will return None in case all inputs lists are empty, but raise an exception in case they aren't of equal length. That's easy to fix though, as Core defines an appropriate result type for pattern matching against:

let highestSales products amounts prices =
  List.map3 products amounts prices (fun prod a p -> (prod, a * p))
  |> function
  | List.Or_unequal_lengths.Ok l ->
      List.max_elt l ~compare:(fun (_, p1) (_, p2) -> Int.compare p1 p2)
      |> Option.map ~f:fst
  | Unequal_lengths ->
      None
code of conduct - report abuse