diff options
Diffstat (limited to 'sources/scala/List.scala')
-rw-r--r-- | sources/scala/List.scala | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/sources/scala/List.scala b/sources/scala/List.scala index 8c7f8d0463..7acf1e7157 100644 --- a/sources/scala/List.scala +++ b/sources/scala/List.scala @@ -188,6 +188,16 @@ object List { sb.toString() } + /** Like (xs map f), but returns xs unchanged if the f maps all elements to themselves + */ + def transform[a](xs: List[a])(f: a => a): List[a] = xs match { + case Nil => Nil + case head :: tail => + val head1 = f(head); + val tail1 = transform(tail)(f); + if (head1 == head && tail1 == tail) xs else head1 :: tail1 + } + /** Returns the list resulting from applying the given function <code>f</code> to * corresponding elements of the argument lists. * |