Hello I’m working on an hobby oss project, ‘parti...
# functional
j
Hello I’m working on an hobby oss project, ‘partial-function-kt’, which is a Kotlin analogue of Scala’s PartialFunction. Would you kindly be willing to share your thoughts, suggestions, or any feedback on this implementation? Or, if you have any good precedents, could you please share them with me? https://github.com/jsoizo/partial-function-kt
d
Interesting concept. I personally don't like using null as a result in these cases, but it would probably take considerable rework to implement safely without nulls.
Stylistically, I'd also consider making
andThen
and
orElse
infix functions.
I would also consider creating a single reusable class for use in the builder functions:
Copy code
class SimplePartialFunction<A, B>(
    val isDefinedAtLambda: (A) -> Boolean,
    val applyLambda: (A) -> B
) : PartialFunction<A, B> {
   override fun isDefinedAt(a: A): Boolean = isDefinedAtLambda(a)
   override fun apply(a: A) = applyLambda(a)
}

fun partialFunction(isDefinedAt: (A) -> Boolean, apply: (A) -> B): PartialFunction<A,B> = 
    SimplePartialFunction(isDefinedAt, apply)
I think I'd change the names of the extensions as well. I think you should use
Iterable.mapDefined
and
Iterable.firstDefined
for clearer meaning. Don't forget to add support for
Sequence
😉
BTW, if you're willing, I also have a small project I'd like feedback on. Posted here
j
@Daniel Pitts Thanks for the good feedback. Some of the feedback I have received will be incorporated into the code.