All Downloads are FREE. Search and download functionalities are using the official Maven repository.

arrow.data.SortedMapKW.kt Maven / Gradle / Ivy

There is a newer version: 0.8.2
Show newest version
package arrow.data

import arrow.*
import arrow.core.Eval
import arrow.core.Option
import arrow.core.Tuple2
import arrow.typeclasses.Applicative
import arrow.typeclasses.Foldable
import java.util.*

@higherkind
data class SortedMapKW, B>(val map: SortedMap) : SortedMapKWKind, SortedMapKWKindedJ, SortedMap by map {

    fun  map(f: (B) -> C): SortedMapKW =
            this.map.map { it.key to f(it.value) }.toMap().toSortedMap().k()

    fun  map2(fc: SortedMapKW, f: (B, C) -> Z): SortedMapKW =
            if (fc.isEmpty()) sortedMapOf().k()
            else this.map.flatMap { (a, b) ->
                fc.getOption(a).map { Tuple2(a, f(b, it)) }.k().asIterable()
            }.k()

    fun  map2Eval(fc: Eval>, f: (B, C) -> Z): Eval> =
            if (fc.value().isEmpty()) Eval.now(sortedMapOf().k())
            else fc.map { c -> this.map2(c, f) }

    fun  ap(ff: SortedMapKW C>): SortedMapKW =
            ff.flatMap { this.map(it) }

    fun  ap2(f: SortedMapKW Z>, fc: SortedMapKW): SortedMap =
            f.map.flatMap { (k, f) ->
                this.flatMap { a -> fc.flatMap { c -> sortedMapOf(k to f(a, c)).k() } }
                        .getOption(k).map { Tuple2(k, it) }.k().asIterable()
            }.k()

    fun  flatMap(f: (B) -> SortedMapKW): SortedMapKW =
            this.map.flatMap { (k, v) ->
                f(v).getOption(k).map { Tuple2(k, it) }.k().asIterable()
            }.k()

    fun  foldRight(c: Eval, f: (B, Eval) -> Eval): Eval =
            this.map.values.iterator().iterateRight(c)(f)

    fun  foldLeft(c: C, f: (C, B) -> C): C = this.map.values.fold(c, f)

    fun  foldLeft(c: SortedMapKW, f: (SortedMapKW, Tuple2) -> SortedMapKW): SortedMapKW =
            this.map.foldLeft(c) { m: SortedMap, (a, b) -> f(m.k(), Tuple2(a, b)) }.k()

    fun  traverse(f: (B) -> HK, GA: Applicative): HK> =
        (Foldable.iterateRight(this.map.iterator(), Eval.always { GA.pure(sortedMapOf().k()) }))({
            kv, lbuf ->
            GA.map2Eval(f(kv.value), lbuf) { (mapOf(kv.key to it.a).k() + it.b).toSortedMap().k() }
        }).value()

    companion object
}

fun , B> SortedMap.k(): SortedMapKW = SortedMapKW(this)

fun , B> Option>.k(): SortedMapKW = this.fold(
        { sortedMapOf().k() },
        { mapEntry -> sortedMapOf(mapEntry.a to mapEntry.b).k() }
)

fun , B> List>.k(): SortedMapKW =
        this.map { it.key to it.value }.toMap().toSortedMap().k()

fun  SortedMap.getOption(k: A): Option = Option.fromNullable(this[k])

fun , B> SortedMapKW.updated(k: A, value: B): SortedMapKW {
    val sortedMutableMap = this.toSortedMap()
    sortedMutableMap.put(k, value)

    return sortedMutableMap.k()
}

fun  SortedMap.foldLeft(b: SortedMap, f: (SortedMap, Map.Entry) -> SortedMap): SortedMap {
    var result = b
    this.forEach { result = f(result, it) }
    return result
}

fun , B, C> SortedMap.foldRight(b: SortedMap, f: (Map.Entry, SortedMap) -> SortedMap): SortedMap =
        this.entries.reversed().k().map.foldLeft(b) { x: SortedMap, y -> f(y, x) }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy