Created
September 13, 2018 12:11
-
-
Save ilaborie/c8d08390db2489475767d45aeea90256 to your computer and use it in GitHub Desktop.
Micro stat collector for a DevfestToulouse survey
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 java.io.File | |
typealias Talk = String | |
typealias Counter<T> = Map<T, Int> | |
operator fun <T> Counter<T>.plus(value: T): Counter<T> { | |
val current = this.getOrDefault(value, 0) | |
return this + (value to (current + 1)) | |
} | |
operator fun <T> Counter<T>.plus(values: List<T>): Counter<T> = | |
values.fold(this) { acc, value -> acc + value } | |
fun <T> Counter<T>.display(): String = | |
this.toList() | |
.sortedByDescending { it.second } | |
.joinToString(separator = "\n") { (talk, count) -> "[$count] $talk" } | |
data class LineInfo(val count: Int = 0, | |
val talks: Counter<Talk> = emptyMap(), | |
val quickies: Counter<Talk> = emptyMap(), | |
val meal: Counter<String> = emptyMap()) { | |
operator fun plus(line: List<String>): LineInfo = | |
LineInfo( | |
count = count + 1, | |
talks = talks + line[1].split("/, "), | |
quickies = quickies + line[2].split("/, "), | |
meal = meal + line[3].split("/, ") | |
) | |
override fun toString(): String = """ | |
|---- | |
|Count: $count | |
|----- | |
|Meal: | |
|${meal.display().prependIndent("\t")} | |
|----- | |
|Talks: | |
|${talks.display().prependIndent("\t")} | |
|----- | |
|Quickies: | |
|${quickies.display().prependIndent("\t")} | |
|""".trimMargin("|") | |
} | |
const val defaultFileName = "./Devfest Toulouse 2018 - Sessions - V2 (Responses) - Form responses 1.tsv" | |
fun main(args: Array<String>): Unit = | |
File(args.firstOrNull() ?: defaultFileName) | |
.readLines() | |
.drop(1) | |
.map { it.split('\t') } | |
.fold(LineInfo()) { acc, line -> acc + line } | |
.run { println(this) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment