Created
September 6, 2019 07:43
-
-
Save haifengkao/803515f79f2f502a48660ebfc1d2729e to your computer and use it in GitHub Desktop.
kotlin applicative example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yanbin.fpworkshop.part4 | |
import arrow.core.Try | |
import arrow.core.Tuple2 | |
import arrow.core.flatten | |
import java.time.LocalDate | |
interface Validation { | |
fun validateAccountNo(no: String): Try<String> | |
fun validateAccountName(name: String): Try<String> | |
fun validateOpenDate(date: LocalDate): Try<LocalDate> | |
} | |
fun createAccount(validation: Validation, no: String, name: String, date: LocalDate): Try<Account> { | |
return validation.validateAccountNo(no).flatMap { vNo -> | |
validation.validateAccountName(name).flatMap { vName -> | |
validation.validateOpenDate(date).flatMap { vDate -> | |
AccountService.open(vNo, vName, vDate) | |
} | |
} | |
} | |
} | |
fun createAccountAp(validation: Validation, no: String, name: String, date: LocalDate): Try<Account> { | |
val openFun = { no:String -> {name:String -> {date: LocalDate -> AccountService.open(no, name, date)}} } | |
val openAp = validation.validateAccountNo(no).map { openFun(it) } | |
val openAp2 = validation.validateAccountName(name).ap(openAp) | |
val openAp3 = validation.validateOpenDate(date).ap(openAp2).flatten() | |
return openAp3 | |
} | |
fun main(args: Array<String>) { | |
val application = AccountApplication() | |
val account1 = Account("1", "Eric", Balance(400)) | |
val account2 = Account("2", "John", Balance(100)) | |
val transferResult = application.transferAp(account1, account2, 500) | |
println(transferResult) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment