From fa66c1ddcf29b0fbfd5d4794be9bf5f4edbbf7e0 Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 26 Nov 2019 16:02:32 +0100 Subject: [PATCH] wip --- .../moe/kageru/spektacles/Reductions.kt | 5 ++ .../moe/kageru/spektacles/Spektacles.kt | 46 +++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/moe/kageru/spektacles/Reductions.kt b/src/main/kotlin/moe/kageru/spektacles/Reductions.kt index 5e52a0c..d52a8a9 100644 --- a/src/main/kotlin/moe/kageru/spektacles/Reductions.kt +++ b/src/main/kotlin/moe/kageru/spektacles/Reductions.kt @@ -1,5 +1,7 @@ package moe.kageru.spektacles +import arrow.Kind +import arrow.typeclasses.Functor import java.time.Month fun ParsedLines.heatMapByHour(): SortedCounter = heatMapBy { it.time.hour } @@ -13,3 +15,6 @@ private fun >ParsedLines.heatMapBy(op: (ParsedLine) -> T): Sort .asIterable() .toSortedCounter() } + +inline fun Kind>.deepMap(noinline op: (A) -> R, AP: Functor): Kind> = + AP.run { this@deepMap.map { nested: Kind -> nested.map(op) } } diff --git a/src/main/kotlin/moe/kageru/spektacles/Spektacles.kt b/src/main/kotlin/moe/kageru/spektacles/Spektacles.kt index 09dbb49..b001883 100644 --- a/src/main/kotlin/moe/kageru/spektacles/Spektacles.kt +++ b/src/main/kotlin/moe/kageru/spektacles/Spektacles.kt @@ -1,5 +1,10 @@ package moe.kageru.spektacles +import arrow.Kind +import arrow.core.* +import arrow.core.extensions.mapk.functor.functor +import arrow.typeclasses.Functor +import moe.kageru.spektacles.Node.Leaf import java.time.Month import kotlin.system.measureTimeMillis @@ -13,9 +18,42 @@ fun main() { fun spektacle() { IoHandler.readFile("test.log") .parse() - .filterUsers("kageru_") - .filterModes(UserMode.OP) - .filterMonths(Month.APRIL) - .heatMapByHour() + .filterUsers("kageru_").toList().k() + .let, Node>(::Leaf) + //.grouped { it.time.year } + //.mapValues { it.value.groupBy { it.time.month } }.k() + //.ap(mapOf( + //1 to { lines: Map> -> lines }, + //2 to {lines -> lines.filterKeys { it < Month.APRIL }} + //).k()) + //.let { it } + //.filterModes(UserMode.OP) + //.filterMonths(Month.APRIL) + //.heatMapByHour() + //.deepMap { it.message } .let(::println) } + +// https://arrow-kt.io/docs/recursion/intro/ + +sealed class Node { + class Leaf(val values: List): Node() + class Intersection(val children: Map>): Node() + + fun map(op: (T) -> R): Node { + return when (this) { + is Leaf -> Leaf(values.map(op)) + is Intersection<*, *> -> Intersection(children.mapValues { it.value.map(op)}) + } + } +} + +/* +private fun ListK.grouped(op: (A) -> K): ListK>> { + return this.groupBy(op).map { it.key toT it.value.k() }.k() +} + +private fun MapK.deepMap(AP: Functor>, op: (A) -> R): Kind { + val a = AP.run { this@deepMap.mapValues { op(it.value) } } +} + */