logo       

Re: A useful list operator: msg#00280

lang.scala

Subject: Re: A useful list operator



On 1/23/07, Iulian Dragos <iulian.dragos@xxxxxxx> wrote:
David Pollak wrote:
> It would be helpful to have a list operator that iterated over list
> elements, performed a calculation and return the calculation of the
> first non-None:

I think this can be expressed in terms of 'find':

xs.find(f) match {
case Some(x) => g(x)
case None => None
}



where 'f' is a predicate used to find the first element you want, and
'g' is the mapping function. In your example you combined the two
functions, but I kind of like the 'find' solution. Do you have some
concrete example where 'first' would be clearer?

The inconvenience of using find here is that if you choose f to be {x => !usefulTransform(x).isEmpty}, then, in order to get the result of usefulTransform(x) afterwards, you have to apply it again. One could fudge, and assign the result as a side effect to some variable in scope, though, but then using find is not so elegant after all.

I would agree with Iulian if usefulTransform was a PartialFunction and had a method isDefinedAt!

The List object (not the trait) is full of functions that serve to get efficiency which could be easily implemented otherwise ( e.g. mapConserve), so IMHO one more function of this sort wouldn't hurt.

cheers,
B



--
Burak Emir
Research Assistant / PhD Candidate
Programming Methods Group
EPFL, 1015 Lausanne, Switzerland
http://lamp.epfl.ch/~emir
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise