Jonathan Roewen wrote:
>> 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
> I'm not sure about the name, as it's a fold that filters & maps
> elements in a single step.
Google seems to like the name ;)
Well, alright, it's not _exactly_ the same thing...
> 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
To me this seems like "overspecifying the solution" since fold_left
_must_ by definition work in a particular way, and you're also
(implicitly) specifying the associativity of the operator, you have an
explicit accumulator adding noise, etc. I wanted the "map_reduce" name
to convey that I don't _care_ how the solution is arrived at, I just
want the solution ;)
In particular, when reading the code above the reader may think that it
is crucial that the operations happen in exactly the right order when it
actually doesn't. (Of course, I'm generalizing to complex operators and
mapping functions since pretty much everyone knows the properties of ||
> are more complicated cases in real life programming, but imo, I think
> they're a little nicer, and probably more efficient than your
>> 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.
- Women... can't live with 'em... can't shoot 'em.
Al Bundy / Married With Children