Hey guys, I hope it's alright to ask noob question...
# arrow
a
Hey guys, I hope it's alright to ask noob questions here, I come from scala and have used cats / haskell previously, but I am not very good at category theory stuff and etc I have this ktor app I'm writing, and my login/register code is pretty gnarly (keep in mind first time ever using kotlin, first time using ktor, etc), I was hoping someone could help me make it more functional and nice, I know Eithers are used for error handling a lot but when I tried to use them with Cats the documentation i found at the time just wasn't enough to help me understand it. I've been using java/scala for more than 5 years and I know the jvm, general syntax, and my problem domain here pretty well, it's just the fp part and maybe some kotlin specific syntax I need help with. Now, for examples of my problem, I have this register endpoint
Copy code
post("/register") {
            val newUser = call.receive<NewUser>()
            val user = userService.addUser(newUser)
            if (user != null) {
                call.respond(OutFacingUser(user))
            } else {
                call.respond("Failed to register")
            }
        }
where i call a nullable function, and try to recieve an object from json I have not ensured exists. I do a null check and either respond w/ error text or convert the user to an "OutFacingUser" which is just User with the password hash and some other stuff stripped off it. additionally, the addUser function in my user service is pretty nasty as well
Copy code
fun addUser(newUser: NewUser): User? {
        val nameNotTaken = transaction {
            User.find { Users.username eq newUser.username }.none()
        }
        val emailNotTaken = transaction {
            User.find { Users.email eq newUser.email }.none()
        }

        if (emailNotTaken) { // I could have combined these ifs but I wanted to throw exceptions saying which one was the issue initially and separating the blocks was the easiest way in my mind
            if (nameNotTaken) {
                return transaction {
                    User.new {
                        username = newUser.username
                        email = newUser.email
                        passwordHash = authenticationService.hashPassword(newUser.password)
                        key = ""
                        createdAt = DateTime()
                    }
                }
            }
        }
        return null
    }
I'm sure there's got to be some nice way to handle the Exposed db stuff with arrow, and I'm sure that some category theory fp magic could help get rid of my nested if and possible null return. Now, kotlin doesn't have the same pattern matching magic scala does I think? In scala I could make a naive improvement by using Optionals and matching for the response which removes the nulls here, but it still doesn't solve the problem of better error handling. Anyway, I just wanted to give some context and my thoughts with my problem so I can help you help me and show that I'll take you seriously and respect your input ­čÖé thanks