From: Jonathan R. <jon...@gm...> - 2006-08-30 10:39:13
|
> fold_reduce? Do you mean fold? If not, then what does the fold_reduce > you're thinking of look like? Oops, sorry. fold is reduce :-) And I meant no, I don't think it's too specialised. I'm not sure about the name, as it's a fold that filters & maps elements in a single step. And in terms of readability and thinking about it some more, I don't see why something like the following aren't suitable: let a_total = List.fold_left (fun acc -> function `A x -> acc + x | _ -> acc) 0 list;; let c_max = List.fold_left (fun acc -> function `C x -> max acc x | _ -> acc) min_int list;; let b = List.fold_left (fun acc -> function `B x -> acc || x | _ -> acc) false list;; They all convey exactly what they're trying to achieve. Perhaps there are more complicated cases in real life programming, but imo, I think they're a little nicer, and probably more efficient than your implementation. > There is also precedent > for this kind of thing -- see ExtList.filter_map. I think this is different, as the naive approach of filtering, then mapping can be considerably slower than combining the two. Jonathan |