Clump是一个Scala库,通过一个优雅和高效的方式将来自多个源的数据组合在一起。
例如:
import io.getclump.Clump// Creates sources using the batched interfacesval tracksSource = Clump.source(tracksService.fetch _)(_.id)val usersSource = Clump.source(usersService.fetch _)(_.id)def renderTrackPosts(userId: Long) = { // Defines the clump val clump: Clump[List[EnrichedTrack]] = enrichedTrackPosts(userId) // Triggers execution val future: Future[Option[List[EnrichedTrack]]] = clump.get // Renders the response future.map { case Some(trackPosts) => render.json(trackPosts) case None => render.notFound }}// Composes a clump with the user's track postsdef enrichedTrackPosts(userId: Long) = for { trackPosts <- Clump.future(timelineService.fetchTrackPosts(userId)) enrichedTracks <- Clump.traverse(trackPosts)(enrichedTrack(_)) } yield { enrichedTracks }// Composes an enriched track clumpdef enrichedTrack(trackId: Long) = for { track <- tracksSource.get(trackId) creator <- usersSource.get(track.creatorId) } yield { new EnrichedTrack(track, creator) }
评论