4 ms·
Aren't I then computing the value of total_len for my largest object numerous times? That was why I went for the map, it made some sense to pre-compute the leng
by ax 15y ago
Aren't I then computing the value of total_len for my largest object numerous times? That was why I went for the map, it made some sense to pre-compute the lengths and then find the largest one. Certainly I could implement some caching mechanism but that would come with some implementation complexity?
- Raphael_Amiard 15y agoLike in a procedural language, you can precompute and keep the results. It's still more concise. I use Ocaml's tuples here to make a pair of the length/object but you could use a struct/object/array/whatever: let precomp = List.map lst (fun el -> ((length el), el)) in let get_max (l1, el1) (l2, el2) = el1 if l1 > l2 else el2 in List.reduce lst get_max