PartialFunction implemented in Kotlin, inspired by scala

espresso

Espresso

Posted on May 16, 2022

PartialFunction implemented in Kotlin, inspired by scala

import java.util.function.Function
import java.util.function.Predicate

/**
 * 参考scala的偏函数
 *
 * @see scala.PartialFunction
 */
abstract class PartialFunction<X, Y> : Predicate<X>, Function<X, Y> {

    fun isDefinedAt(x: X): Boolean {
        return test(x)
    }

    override fun apply(x: X): Y {
        return if (isDefinedAt(x)) {
            applyIfDefined(x)
        } else {
            throw IllegalArgumentException("Value: ($x) isn't supported by this function")
        }
    }

    abstract fun applyIfDefined(x: X): Y

    infix fun orElse(fallback: PartialFunction<X, Y>): PartialFunction<X, Y> {
        val outer: PartialFunction<X, Y> = this
        return object : PartialFunction<X, Y>() {
            override fun test(x: X): Boolean {
                return outer.test(x) || fallback.test(x)
            }

            override fun applyIfDefined(x: X): Y {
                return if (outer.isDefinedAt(x)) {
                    outer.applyIfDefined(x)
                } else {
                    fallback.apply(x)
                }
            }

            override fun apply(x: X): Y {
                return applyIfDefined(x)
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
espresso
Espresso

Posted on May 16, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related