Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import java.util.function.Consumer
  2.  
  3. data class User(val name: String, val password: String, val age: Int, var organizationId: Int?)
  4.  
  5. fun addUser(name: String, password: String, age: Int): Result<User, UserError> {
  6.  
  7. val user = User(name, password, age, 0)
  8.  
  9. val userValidator: Validator = compose(compose(validateAge, nameValidator), validatePassword)
  10.  
  11. return user.let(::addUserToDB).let(::Success)
  12. }
  13.  
  14. fun compose(one: Validator, two: Validator): Validator = { t: User ->
  15. val result = one(t)
  16.  
  17. when (result) {
  18. is Success -> two(t)
  19. else -> result
  20. }
  21. }
  22.  
  23. fun addUserToDB(user: User): User = user
  24.  
  25. val nameValidator: Validator = { user: User ->
  26. user.takeIf { it.name.isBlank().not() }
  27. ?.let { Success<User, UserError>(it) }
  28. ?: Failure(UserError.BLANK_NAME)
  29. }
  30.  
  31. val validatePassword: Validator = { user: User ->
  32. user.takeIf { user.password.length >= 10 }
  33. ?.let { Success<User, UserError>(it) }
  34. ?: Failure(UserError.PASSWORD_TOO_LONG)
  35. }
  36.  
  37. val validateAge: Validator = { user: User ->
  38. user.takeIf { it.age < 18 }
  39. ?.let { Success<User, UserError>(it) }
  40. ?: Failure(UserError.ADULT)
  41. }
  42.  
  43. fun main(args: Array<String>) {
  44. val addUser = addUser("Víctor", "is tonto", 25)
  45.  
  46. val map = addUser
  47. .map({ it })
  48. .flatMap({ it: User -> Organization() })
  49.  
  50. println(map)
  51.  
  52. // addUser.map { it.name }.ifSuccess<String, UserError>(Consumer { println(it) })
  53. // addUser.map { it.name }.ifFailure(::println)
  54. }
  55.  
  56. fun toOrganization(id: Int): Result<Organization, Error> {
  57. return Success(Organization())
  58. }
  59.  
  60. class DBError {
  61.  
  62. }
  63.  
  64. class Organization {
  65.  
  66. }
  67.  
  68. typealias Validator = (U: User) -> Result<User, UserError>
  69.  
  70. sealed class Result<S, E>
  71.  
  72. data class Success<S, E>(val value: S) : Result<S, E>()
  73.  
  74. data class Failure<S, E>(val error: E) : Result<S, E>()
  75.  
  76. fun <S, E, R> Result<S, E>.map(transform: (S) -> R): Result<R, E> = when (this) {
  77. is Success -> Success(transform(this.value))
  78. is Failure -> Failure(this.error)
  79. }
  80.  
  81. fun <U, R> Result<U, UserError>.flatMap(transform: (U) -> R): Result<R, UserError> = when (this) {
  82. is Success -> this.map(transform)
  83. is Failure -> Failure(UserError.BLANK_NAME)
  84. }
  85.  
  86. fun <S, E> Result<S, E>.ifSuccess(operation: Consumer<S>) {
  87. if (this is Success) operation.accept(this.value)
  88. }
  89.  
  90. fun <S, E> Result<S, E>.ifFailure(operation: (err: E) -> (Unit)) {
  91. if (this is Failure) operation(this.error)
  92. }
  93.  
  94. enum class UserError {
  95. BLANK_NAME, PASSWORD_TOO_LONG, ADULT
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement