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
// ①マップを生成 | |
val groupMap: Map[UserId, Seq[Group]] = | |
// ②マッピング数分回る | |
groupMemberSeq.groupBy(_.userId) map { case (userId, members) => | |
( | |
userId, | |
members.flatMap(member => // ③そのユーザーのマッピング数分回る | |
groupSeq.find(_.id == member.groupId) // ④Vectorのfindは実質定数 | |
) | |
) |
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
val startTime = System.currentTimeMillis | |
val groupMap: Map[UserId, Seq[Group]] = | |
groupMemberSeq.groupBy(_.userId) map { case (userId, members) => | |
( | |
userId, | |
members.flatMap(member => | |
groupSeq.find(_.id == member.groupId) | |
) | |
) | |
} |
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
val result: Seq[UserView] = userSeq.map(user => { // ①ユーザー人数分回る | |
val groupIdSeq: Seq[GroupId] = | |
groupMemberSeq | |
.filter(_.userId == user.id) // ②マッピング数分回る | |
.map(_.groupId) | |
val userGroups: Seq[Group] = | |
groupSeq.filter(group => // ③グループ数分回る | |
groupIdSeq.contains(group.id) | |
) | |
UserView( |
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
// データを10,000件用意 | |
val userSeq: Seq[User] = | |
(1L to 10000L).map(id => User(UserId(id), "name" + id.toString)) | |
val groupSeq: Seq[Group] = | |
(1L to 10000L).map(id => Group(GroupId(id), "name" + id.toString)) | |
val groupMemberSeq: Seq[GroupMember] = | |
(1L to 10000L).map(id => GroupMember(GroupMemberId(id), UserId(id), GroupId(id))) | |
... |
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
// データを10件用意 | |
val userSeq: Seq[User] = | |
(1L to 10L).map(id => User(UserId(id), "name" + id.toString)) | |
val groupSeq: Seq[Group] = | |
(1L to 10L).map(id => Group(GroupId(id), "name" + id.toString)) | |
val groupMemberSeq: Seq[GroupMember] = | |
(1L to 10L).map(id => GroupMember(GroupMemberId(id), UserId(id), GroupId(id))) | |
val startTime = System.currentTimeMillis | |
val result: Seq[UserView] = ... | |
println((System.currentTimeMillis - startTime).toString + "ミリ秒") |
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
val userSeq: Seq[User] = UserRepository.findAll(); | |
val groupSeq: Seq[Group] = GroupRepository.findAll(); | |
val groupMemberSeq: Seq[GroupMember] = GroupMembersRepository.findAll(); | |
val result: Seq[UserView] = userSeq.map(user => { // ① | |
val groupIdSeq: Seq[GroupId] = | |
groupMemberSeq | |
.filter(_.userId == user.id) // ② | |
.map(_.groupId) | |
val userGroups: Seq[Group] = // ③ | |
groupSeq.filter(group => |
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
case class UserView( | |
id: UserId, | |
name: String, | |
groups: Seq[Group] | |
) | |
val result: Seq[UserView] = ??? |
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
case class UserId(value: Long) | |
case class User ( | |
id: UserId, | |
name: String | |
) | |
case class GroupId(value: Long) | |
case class Group ( | |
id: GroupId, | |
name: String | |
) |
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 scala.math.abs | |
// 気温リスト1~12月 | |
val temperatureSeq:Seq[Int] = | |
Seq(4, 5, 11, 17, 19, 22, 28, 28, 22, 19, 14, 7) | |
def diffTemperature2(tSeq: Seq[Int]) = { | |
tSeq.sliding(2).foldLeft(Seq.empty[Int])((totalSeq, numSeq) => | |
totalSeq :+ abs(numSeq.head - numSeq.last) | |
) | |
} | |
diffTemperature2(temperatureSeq) |
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 scala.math.abs | |
// 気温リスト1~12月 | |
val temperatureSeq:Seq[Int] = | |
Seq(4, 5, 11, 17, 19, 22, 28, 28, 22, 19, 14, 7) | |
def diffTemperature1(tSeq: Seq[Int]) = { | |
@scala.annotation.tailrec | |
def f(diffSeq:Seq[Int], tSeq: Seq[Int]): Seq[Int] = { | |
tSeq match { | |
case head +: second +: tail => | |
f(diffSeq :+ abs(head - second), second +: tail) |
NewerOlder