Skip to content

ktomek/funKtional

Repository files navigation

Build License: MIT GitHub release GitHub issues GitHub pull requests

funKtional

A lightweight Kotlin Multiplatform library providing functional programming extensions and utilities for nullable handling, type casting, flow lifting, and more.

Targets: Android · iOS · JVM

Features

  • Suspended letCo for coroutine-friendly mapping
  • orDefault and orDefaultCo for safe null defaults
  • onNull and onNullCo for executing side-effects on null
  • Reified casts: asType, asTypeOrNull
  • Null-aware match, matchCo, matchAction, matchCoAction
  • lift functions for combining up to three nullable values
  • Fully documented with KDoc and usage examples

Installation

dependencies {
    implementation("io.github.ktomek:funKtional:1.2.0")
}

Usage

// letCo example
runBlocking {
    val length = "Hello".letCo { it.length }
    println(length) // 5
}

// orDefault example
val value: String? = null
val default = value.orDefault { "default" }
println(default) // "default"

// asType / asTypeOrNull example
val any: Any = "text"
val text: String? = any.asTypeOrNull<String>()
println(text) // "text"

// lift example
val sum: Int? = 2.lift(3) { a, b -> a + b }
println(sum) // 5

Advanced Usage: Chaining Functional Extensions

runBlocking {
    val x: Int? = null
    val y: Int? = 5
    val z: Int? = 10

    // Combine nullable values, provide default, then transform
    val result = x.lift(y, z) { a, b, c -> a + b + c }
        .orDefault { 0 }
        .letCo { it * 2 }

    println(result) // (0 + 5 + 10) * 2 = 30
}

// Null-safe branching with match
val branch = "Kotlin"
    .match(
        some = { it.length },
        none = { 0 }
    )
    .let { length -> "Length is $length" }
    .also { println(it) } // "Length is 6"

Contributing

Contributions are welcome! Please open issues or submit pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages