MarioAriasC / funKTionale

Functional constructs for Kotlin
914 stars 71 forks source link

Mapping EDSL for Either #26

Open lukashaertel opened 7 years ago

lukashaertel commented 7 years ago

I've been doing some experiments with Either myself and what I came up with has IMO a nice way of mapping either choice in a concise fashion, you might want to take a look at it and see if this is something for funktionale-either. It appears to be what Either is doing in fold but expanded by infix operations.

Relevant code starts here: https://gist.github.com/lukashaertel/02b8dd4a5ad8c691bc783eb7b88fba9b#file-choice-kt-L37

And here's an example of how it's applied:

    // A method handling that choice type
    val f = { u: Choice<Number, String> ->
        // Map either side accordingly
        val d = u onLeft {
            it.toDouble() / 2
        } onRight {
            it.length.toDouble()
        }

        println(d)
    }
MarioAriasC commented 6 years ago

I'll have a look, sorry for the late response