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

kotlens.Prism.kt Maven / Gradle / Ivy

package kotlens

data class Prism(val getOption: (S) -> A?, val reverseGet: (A) -> S) {
    operator fun invoke(a: A) = reverseGet(a)
    fun isMatching(s: S): Boolean = getOption(s) != null
    fun modify(ƒ: (A) -> A): (S) -> S = { s -> modifyOption(ƒ)(s) ?: s }
    fun modifyOption(ƒ: (A) -> A): (S) -> S? = { s -> getOption(s)?.let { a -> reverseGet(ƒ(a)) } }
    infix fun  compose(other: Prism): Prism = Prism(
            getOption = { s -> getOption(s)?.let { a -> other.getOption(a) } },
            reverseGet = { b -> reverseGet(other.reverseGet(b)) }
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy