Created
December 4, 2011 04:28
-
-
Save xuwei-k/1429171 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryDependencies ++= Seq( | |
"org.functionaljava" % "functionaljava" % "3.0" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fj.*; | |
import fj.data.List; | |
import fj.function.Integers; | |
public final class Main{ | |
static final class Data { | |
Data(final String code,final String name,final int value){ | |
this.code = code; | |
this.name = name; | |
this.value = value; | |
} | |
final String code; | |
final String name; | |
final int value; | |
@Override public final String toString(){ | |
return code + " " + name + " " + value; | |
} | |
} | |
static final List<Data> dataList = List.list( | |
new Data("A01","hoge",100), | |
new Data("A01","piyo",200), | |
new Data("A02","hoge",300), | |
new Data("A03","hoge",400), | |
new Data("A03","piyo",500) | |
); | |
static final List<Data> summary(final List<Data> list) { | |
return list.group( | |
Equal.equal( | |
new F<Data,F<Data,Boolean>>(){ | |
@Override public F<Data,Boolean> f(final Data a){ | |
return new F<Data,Boolean>(){ | |
@Override public Boolean f(final Data b){ | |
return a.code.equals(b.code); | |
} | |
}; | |
} | |
} | |
) | |
).map( | |
new F<List<Data>,Data>(){ | |
@Override public Data f(final List<Data> a){ | |
final Integer s = Integers.sum( | |
a.map( | |
new F<Data,Integer>(){ | |
@Override public Integer f(final Data b){ | |
return b.value; | |
} | |
} | |
) | |
); | |
return new Data(a.head().code,a.head().name,s); | |
}; | |
} | |
); | |
} | |
public static void main(String[] args){ | |
summary(dataList).foreach( | |
new F<Data,Unit>(){ | |
@Override public Unit f(final Data a){ | |
System.out.println(a); | |
return Unit.unit(); | |
} | |
} | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> case class Data(code:String,name:String,value:Int) | |
defined class Data | |
scala> val dataList = List( | |
| Data("A01","hoge",100), | |
| Data("A01","piyo",200), | |
| Data("A02","hoge",300), | |
| Data("A03","hoge",400), | |
| Data("A03","piyo",500) | |
| ) | |
dataList: List[Data] = List(Data(A01,hoge,100), Data(A01,piyo,200), Data(A02,hoge,300), Data(A03,hoge,400), Data(A03,piyo,500)) | |
scala> dataList.groupBy(_.code).map{case (_,v) => v.head.copy(value = v.map{_.value}.sum) } | |
res0: scala.collection.immutable.Map[String,Int] = Map(A03 -> 900, A01 -> 300, A02 -> 300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment