Skip to content

Instantly share code, notes, and snippets.

@pjagielski
Created February 11, 2019 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjagielski/59f1ee15c9e9339306928e8ccb936182 to your computer and use it in GitHub Desktop.
Save pjagielski/59f1ee15c9e9339306928e8ccb936182 to your computer and use it in GitHub Desktop.
fun Iterable<ResultRow>.toArticles(): List<Article> {
return fold(mutableMapOf<ArticleId, Article>()) { map, resultRow ->
val article = resultRow.toArticle()
val tagId = resultRow.tryGet(ArticleTagTable.tagId)
val tag = tagId?.let { resultRow.toTag() }
val current = map.getOrDefault(article.id, article)
map[article.id] = current.copy(tags = current.tags + listOfNotNull(tag))
map
}.values.toList()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment