Merging elements in a scala list
Posted
by scompt.com
on Stack Overflow
See other posts from Stack Overflow
or by scompt.com
Published on 2010-03-11T15:31:25Z
Indexed on
2010/03/11
17:59 UTC
Read the original article
Hit count: 122
I'm trying to port the following Java snippet to Scala. It takes a list of MyColor
objects and merges all of the ones that are within a delta of each other. It seems like a problem that could be solved elegantly using some of Scala's functional bits. Any tips?
List<MyColor> mergedColors = ...;
MyColor lastColor = null;
for(Color aColor : lotsOfColors) {
if(lastColor != null) {
if(lastColor.diff(aColor) < delta) {
lastColor.merge(aColor);
continue;
}
}
lastColor = aColor;
mergedColors.add(aColor);
}
© Stack Overflow or respective owner